Skip to content

Commit 99abd2c

Browse files
committed
Squiz/IncrementDecrementUsage: bug fix - code without whitespace [1]
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 just one of these issues, as reported in 325. For this particular issue, 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. Fixes 325
1 parent de3b745 commit 99abd2c

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
@@ -165,8 +165,8 @@ protected function processAssignment($phpcsFile, $stackPtr)
165165

166166
// We have only one variable, and it's the same as what is being assigned,
167167
// so we need to check what is being added or subtracted.
168-
$nextNumber = ($stackPtr + 1);
169-
$previousNumber = ($stackPtr + 1);
168+
$nextNumber = $stackPtr;
169+
$previousNumber = $stackPtr;
170170
$numberCount = 0;
171171
while (($nextNumber = $phpcsFile->findNext([T_LNUMBER], ($nextNumber + 1), $statementEnd, false)) !== false) {
172172
$previousNumber = $nextNumber;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,6 @@ $expected[($i + 1)]['sort_order'] = ($i + 1);
4040
$id = $id.($obj->i++).$id;
4141
$id = $obj?->i++.$id;
4242
$id = $obj?->i++*10;
43+
44+
$var+=1;
45+
$var-=1;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public function getErrorList()
4242
31 => 1,
4343
41 => 1,
4444
42 => 1,
45+
44 => 1,
46+
45 => 1,
4547
];
4648

4749
}//end getErrorList()

0 commit comments

Comments
 (0)