Skip to content

Generic/IncrementDecrementSpacing: inconsistent error message #288

@rodrigoprimo

Description

@rodrigoprimo

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:

  1. Create a file called test.php with the code sample above.
  2. Run phpcs --standard=Generic --sniffs=Generic.WhiteSpace.IncrementDecrementSpacing test.php
  3. 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:

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 master branch of PHP_CodeSniffer.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions