phpcsphp-cs-fixer

Closing parenthesis of a multi-line function call must be on a line by itself


I am getting this message from phpcs. my code is:

$userdata["expirydate"] = date("Y-m-d H:i:s", mktime(date("H"), date(
        "i"), date("s"), date("m"), date("d") - 1, date("y")));

Solution

  • Your date("i") function call spans multiple lines. When this happens, that rule is enforcing that the closing parenthesis be on a line by itself.

    If you want to adhere to that rule, you've got a a few options for reformatting your code.

    You could use the PHPCS diff report to see how PHPCS wants you to format it. In this case, using --report=diff shows:

    --- temp.php
    +++ PHP_CodeSniffer
    @@ -1,3 +1,4 @@
     <?php
     $userdata["expirydate"] = date("Y-m-d H:i:s", mktime(date("H"), date(
    -    "i"), date("s"), date("m"), date("d") - 1, date("y")));
    +    "i"
    +), date("s"), date("m"), date("d") - 1, date("y")));
    

    Which means PHPCS thinks the smallest change you could make would be to write your code like this:

    $userdata["expirydate"] = date("Y-m-d H:i:s", mktime(date("H"), date(
        "i"
    ), date("s"), date("m"), date("d") - 1, date("y")));
    

    Which is valid, but not great.

    You could put it all on one long line, which is still valid:

    $userdata["expirydate"] = date("Y-m-d H:i:s", mktime(date("H"), date("i"), date("s"), date("m"), date("d") - 1, date("y")));
    

    You could split up the main date() call to make it valid and keep the line lengths shorter:

    $userdata["expirydate"] = date(
        "Y-m-d H:i:s",
        mktime(date("H"), date("i"), date("s"), date("m"), date("d") - 1, date("y"))
    );
    

    Or you could even put every argument on a new line:

    $userdata["expirydate"] = date(
        "Y-m-d H:i:s",
        mktime(
            date("H"),
            date("i"),
            date("s"),
            date("m"),
            date("d") - 1,
            date("y")
        )
    );
    

    It really depends on which code block you find more readable, and which code block fits best with your existing coding standard.