Skip to content

Commit b0c442a

Browse files
committed
Squiz/IncrementDecrementUsage: bug fix - comments between variable and equal sign
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 another one of these issues. In this case, if there was a comment between the `$var` and the subsequent equal sign, the sniff would incorrectly disregard the assignment as one which should be examined. Fixed now. Includes tests.
1 parent c29805d commit b0c442a

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ protected function processAssignment($phpcsFile, $stackPtr)
120120
{
121121
$tokens = $phpcsFile->getTokens();
122122

123-
$assignedVar = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
123+
$assignedVar = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
124124
// Not an assignment, return.
125125
if ($tokens[$assignedVar]['code'] !== T_VARIABLE) {
126126
return;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,8 @@ $var-=1;
4646

4747
$var=$var+1;
4848
$var=$var-1;
49+
50+
$var /*comment*/ +=1;
51+
$var
52+
// phpcs:ignore Something
53+
-=1;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public function getErrorList()
4646
45 => 1,
4747
47 => 1,
4848
48 => 1,
49+
50 => 1,
50+
53 => 1,
4951
];
5052

5153
}//end getErrorList()

0 commit comments

Comments
 (0)