-
-
Notifications
You must be signed in to change notification settings - Fork 88
Description
Describe the bug
I believe there is a minor inconsistency in two error messages displayed by the Generic.WhiteSpace.IncrementDecrementSpacing sniff. This sniff displays the following when it finds a newline between a pre-decrement and a variable:
Expected no spaces between the decrement operator and $i; newline found
But when it finds a newline between a variable and a post-decrement operator, the message is:
Expected no spaces between $i and the decrement operator; 0 found
Code sample
--
$i;
$i
--;To reproduce
Steps to reproduce the behavior:
- Create a file called
test.phpwith the code sample above. - Run
phpcs --standard=Generic --sniffs=Generic.WhiteSpace.IncrementDecrementSpacing test.php - See the error message displayed
-----------------------------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 2 LINES
-----------------------------------------------------------------------------------------
3 | ERROR | [x] Expected no spaces between the decrement operator and $i; newline found
7 | ERROR | [x] Expected no spaces between $i and the decrement operator; 0 found
-----------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-----------------------------------------------------------------------------------------
Expected behavior
I would expect the message for the post-decrement operator to be "Expected no spaces between $i and the decrement operator; newline found" instead of "Expected no spaces between $i and the decrement operator; 0 found" (newline instead of 0).
Possible solution
I think this problem is caused by using the wrong variable in this line:
PHP_CodeSniffer/src/Standards/Generic/Sniffs/WhiteSpace/IncrementDecrementSpacingSniff.php
Line 138 in ad5421c
| if ($tokens[$stackPtr]['line'] !== $tokens[$nextNonEmpty]['line']) { |
$prevNonEmpty should be used instead of $nextNonEmpty.
I can submit a PR with a patch if it helps.
Versions (please complete the following information)
| Operating System | Ubuntu 23.04 |
| PHP version | 8.3 |
| PHP_CodeSniffer version | master |
| Standard | Generic |
| Install type | git clone |
Please confirm:
- I have searched the issue list and am not opening a duplicate issue.
- I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
- I have verified the issue still exists in the
masterbranch of PHP_CodeSniffer.