Skip to content

Commit

Permalink
Squiz/IncrementDecrementUsage: bug fix - comments in value being assi…
Browse files Browse the repository at this point in the history
…gned

As this is a sniff looking for certain functional code patterns, the sniff should disregard whitespace and comments when looking for the relevant tokens to determine whether the code under scan contains the code pattern the sniff is looking for.

The sniff, however, does not do this correctly (in multiple places).

This commit fixes one more of these issues.

In this case, if there was a comment anywhere in the value being assigned, the sniff would incorrectly disregard the assignment as one which should be examined.

Fixed now.

Includes tests.
  • Loading branch information
jrfnl committed Feb 7, 2024
1 parent 32a50de commit 982e40a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,14 @@ protected function processAssignment($phpcsFile, $stackPtr)
$statementEnd = $phpcsFile->findNext([T_SEMICOLON, T_CLOSE_PARENTHESIS, T_CLOSE_SQUARE_BRACKET, T_CLOSE_CURLY_BRACKET], $stackPtr);

// If there is anything other than variables, numbers, spaces or operators we need to return.
$noiseTokens = $phpcsFile->findNext([T_LNUMBER, T_VARIABLE, T_WHITESPACE, T_PLUS, T_MINUS, T_OPEN_PARENTHESIS], ($stackPtr + 1), $statementEnd, true);

$find = Tokens::$emptyTokens;
$find[] = T_LNUMBER;
$find[] = T_VARIABLE;
$find[] = T_PLUS;
$find[] = T_MINUS;
$find[] = T_OPEN_PARENTHESIS;

$noiseTokens = $phpcsFile->findNext($find, ($stackPtr + 1), $statementEnd, true);
if ($noiseTokens !== false) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@ $var /*comment*/ +=1;
$var
// phpcs:ignore Something
-=1;

$var += /*comment*/ 1;
$var = ( $var /*comment*/ - 1 /*comment*/);
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public function getErrorList()
48 => 1,
50 => 1,
53 => 1,
55 => 1,
56 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit 982e40a

Please sign in to comment.