squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.66k stars 1.48k forks source link

Make FunctionCallSignatureSniff work when closing brace doesn't have to be placed on its own line #3848

Open Daimona opened 1 year ago

Daimona commented 1 year ago

FunctionCallSignatureSniff assumes that the closing call parentheses is placed on its own line. If that's not the case, the CloseBracketLine issue is emitted. This can clearly be disabled in a custom standard, if wanted; however, the sniff also makes the same assumption elsewhere:

if ($tokens[$nextCode]['line'] === $tokens[$closeBracket]['line']) {
    // Closing brace needs to be indented to the same level
    // as the function call.
    $inArg          = false;
    $expectedIndent = ($foundFunctionIndent + $adjustment);
} else {
    $expectedIndent = ($foundFunctionIndent + $this->indent + $adjustment);
}

This means that if you disable CloseBracketLine and run PHPCS on the following code:

function foo() {
    $this->doSomething(
        $firstArg, $secondArg, $thirdArg );
}

it will still complain that the second call line is not indented correctly. In particular, since it contains the closing parentheses, PHPCS wants it to have the same indentation as the first call line, meaning it would consider this to be correct:

function foo() {
    $this->doSomething(
    $firstArg, $secondArg, $thirdArg );
}

Therefore, I'm asking if it would be possible to make the sniff work in a custom standard that does not require the closing parentheses to be on its own line. This could perhaps be a config option to the sniff; or maybe the if branch in the conditional above could be updated to check that the closing parentheses is the only thing on that line. However, this would need to account for arrays:

function foo() {
    $this->doSomething( [
        $firstArg, $secondArg, $thirdArg
    ] ); // This is correct; even if the ) is not alone, this line should have the same indentation as the opener (
}

Thanks!