Skip to content

Commit 4db0991

Browse files
committed
Squiz/IncrementDecrementUsage: bug fix - comments in value being assigned
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.
1 parent 2adbc16 commit 4db0991

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

src/Standards/Squiz/Sniffs/Operators/IncrementDecrementUsageSniff.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,14 @@ protected function processAssignment($phpcsFile, $stackPtr)
129129
$statementEnd = $phpcsFile->findNext([T_SEMICOLON, T_CLOSE_PARENTHESIS, T_CLOSE_SQUARE_BRACKET, T_CLOSE_CURLY_BRACKET], $stackPtr);
130130

131131
// If there is anything other than variables, numbers, spaces or operators we need to return.
132-
$noiseTokens = $phpcsFile->findNext([T_LNUMBER, T_VARIABLE, T_WHITESPACE, T_PLUS, T_MINUS, T_OPEN_PARENTHESIS], ($stackPtr + 1), $statementEnd, true);
133-
132+
$find = Tokens::$emptyTokens;
133+
$find[] = T_LNUMBER;
134+
$find[] = T_VARIABLE;
135+
$find[] = T_PLUS;
136+
$find[] = T_MINUS;
137+
$find[] = T_OPEN_PARENTHESIS;
138+
139+
$noiseTokens = $phpcsFile->findNext($find, ($stackPtr + 1), $statementEnd, true);
134140
if ($noiseTokens !== false) {
135141
return;
136142
}

src/Standards/Squiz/Tests/Operators/IncrementDecrementUsageUnitTest.inc

+3
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,6 @@ $var /*comment*/ +=1;
5151
$var
5252
// phpcs:ignore Something
5353
-=1;
54+
55+
$var += /*comment*/ 1;
56+
$var = ( $var /*comment*/ - 1 /*comment*/);

src/Standards/Squiz/Tests/Operators/IncrementDecrementUsageUnitTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public function getErrorList()
4848
48 => 1,
4949
50 => 1,
5050
53 => 1,
51+
55 => 1,
52+
56 => 1,
5153
];
5254

5355
}//end getErrorList()

0 commit comments

Comments
 (0)