Skip to content

Commit c29805d

Browse files
committed
Squiz/IncrementDecrementUsage: bug fix - code without whitespace [2]
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 the same issue as fixed in the previous commit, but now specifically for the part in the code where it is checking that a `$var = $var + 1;` like statement only has one variable in the code comprising the value being assigned. Same as before, the `$startPtr` for the `findNext()` is incremented for each `while` loop, but the initial `$startPtr` was also incremented, which meant that if there was no whitespace after an equals sign, the first relevant token was being skipped over. Fixed now. Includes tests.
1 parent 99abd2c commit c29805d

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ protected function processAssignment($phpcsFile, $stackPtr)
145145
}
146146

147147
if ($tokens[$stackPtr]['code'] === T_EQUAL) {
148-
$nextVar = ($stackPtr + 1);
149-
$previousVariable = ($stackPtr + 1);
148+
$nextVar = $stackPtr;
149+
$previousVariable = $stackPtr;
150150
$variableCount = 0;
151151
while (($nextVar = $phpcsFile->findNext(T_VARIABLE, ($nextVar + 1), $statementEnd)) !== false) {
152152
$previousVariable = $nextVar;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,6 @@ $id = $obj?->i++*10;
4343

4444
$var+=1;
4545
$var-=1;
46+
47+
$var=$var+1;
48+
$var=$var-1;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public function getErrorList()
4444
42 => 1,
4545
44 => 1,
4646
45 => 1,
47+
47 => 1,
48+
48 => 1,
4749
];
4850

4951
}//end getErrorList()

0 commit comments

Comments
 (0)