slevomat / coding-standard

Slevomat Coding Standard for PHP_CodeSniffer provides many useful sniffs
MIT License
1.39k stars 176 forks source link

SlevomatCodingStandard.PHP.UselessParentheses.UselessParentheses false positive producing invalid syntax #868

Closed morozov closed 4 years ago

morozov commented 4 years ago

Consider the code example:

<?php

declare(strict_types=1);

(static function () : void {
    echo $_SERVER['argv'][1];
})();

(static function () : void {
    echo $_SERVER['argv'][1 + 1];
})();

Running phpcs using the Doctrine Coding Standard against this file produces the following:

$ phpcs test.php

------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
------------------------------------------------------------------------------------------------
 9 | ERROR | [x] Useless parentheses.
   |       |     (SlevomatCodingStandard.PHP.UselessParentheses.UselessParentheses)
------------------------------------------------------------------------------------------------

As you can see, the first block was not flagged as violating the standard while the second was.

phpcbf modifies the file in the following way:

<?php

declare(strict_types=1);

(static function () : void {
    echo $_SERVER['argv'][1];
})();

static function () : void {
    echo $_SERVER['argv'][1 + 1];
}();

Which makes it syntactically invalid:

$ php -l test.php

Parse error: syntax error, unexpected '(' in test.php on line 11

Errors parsing test.php
kukulich commented 4 years ago

Thank you for your report.

Fixed in https://github.com/slevomat/coding-standard/commit/a36138ecd1fe1e64f536070aaa78df5222c65152

github-actions[bot] commented 4 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.