slevomat / coding-standard

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

Operators.NegationOperatorSpacing returns a false positive #951

Closed matbour closed 4 years ago

matbour commented 4 years ago

SlevomatCodingStandard.Operators.NegationOperatorSpacing returns a false positive when assigning a negative number like $a = -1 with property spacesCount set to zero.

Affected version: 6.1.5, on PHP 7.1, 7.2, 7.3, 7.4 on all OS available for GitHub Actions.

Minimal example: mathieu-bour/slevomat-negative-operator.

There is also a workflow which demonstrates the issue here: https://github.com/mathieu-bour/slevomat-negative-operator/runs/542079155

Configuration:

<rule ref="SlevomatCodingStandard.Operators.NegationOperatorSpacing">
    <properties>
        <property name="spacesCount" value="0"/>
    </properties>
</rule>

The output of $ ./vendor/bin/phpcs is the following:

FILE: ./slevomat-negative-operator/test.php
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
 2 | ERROR | [x] Expected exactly 0 space after "-", 0 found.
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

Time: 18ms; Memory: 4MB

Which seems very strange.

Moreover, the output of $ ./vendor/bin/phpcbf is the following:

PHP Fatal error:  Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: Undefined offset: 6 in ./slevomat-negative-operator/vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Operators/NegationOperatorSpacingSniff.php on line 70 in ./slevomat-negative-operator/vendor/squizlabs/php_codesniffer/src/Runner.php:606
Stack trace:
#0 ./slevomat-negative-operator/vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Operators/NegationOperatorSpacingSniff.php(70): PHP_CodeSniffer\Runner->handleErrors()
#1 ./slevomat-negative-operator/vendor/squizlabs/php_codesniffer/src/Files/File.php(496): SlevomatCodingStandard\Sniffs\Operators\NegationOperatorSpacingSniff->process()
#2 ./slevomat-negative-operator/vendor/squizlabs/php_codesniffer/src/Files/LocalFile.php(91): PHP_CodeSniffer\Files\File->process()
#3 ./slevomat-negative-operator/vendor/squizlabs/php_codesniffer/src/Fixer.php(174): PHP_CodeSni in ./slevomat-negative-operator/vendor/squizlabs/php_codesniffer/src/Runner.php on line 606

Thus, it may be directly related to PHP_CodeSniffer itself.

kukulich commented 4 years ago

@mathieu-bour Can you try dev-master please?

matbour commented 4 years ago

I just tried with c1c1cd781b and this issue seems to not be relevant anymore.

Code reference: https://github.com/mathieu-bour/slevomat-negative-operator/tree/with-dev-master

Workflow: https://github.com/mathieu-bour/slevomat-negative-operator/runs/542147459

I don't know this issue should remain open tho. Close it if you feel that is necessary, I will use the commit c1c1cd781b for now!

kukulich commented 4 years ago

Thanks, so it's duplicate to https://github.com/slevomat/coding-standard/issues/917

Fix will be part of next release on Monday.

matbour commented 4 years ago

Sorry for the duplicate, thank you very much for your work!

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.