Skip to content

Commit 33a8bf8

Browse files
committed
DisallowIncrementAndDecrementOperatorsSniff: Fixed error codes for post operators in some situations
1 parent a10f01f commit 33a8bf8

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

SlevomatCodingStandard/Sniffs/Operators/DisallowIncrementAndDecrementOperatorsSniff.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
use PHP_CodeSniffer\Files\File;
66
use PHP_CodeSniffer\Sniffs\Sniff;
7+
use SlevomatCodingStandard\Helpers\IdentificatorHelper;
78
use SlevomatCodingStandard\Helpers\TokenHelper;
89
use const T_DEC;
910
use const T_INC;
10-
use const T_VARIABLE;
1111

1212
class DisallowIncrementAndDecrementOperatorsSniff implements Sniff
1313
{
@@ -37,9 +37,10 @@ public function process(File $phpcsFile, $operatorPointer): void
3737
{
3838
$tokens = $phpcsFile->getTokens();
3939

40-
$previousPointer = TokenHelper::findPreviousEffective($phpcsFile, $operatorPointer - 1);
41-
42-
$isPostOperator = $tokens[$previousPointer]['code'] === T_VARIABLE;
40+
/** @var int $nextPointer */
41+
$nextPointer = TokenHelper::findNextEffective($phpcsFile, $operatorPointer + 1);
42+
$afterVariableEndPointer = IdentificatorHelper::findEndPointer($phpcsFile, $nextPointer);
43+
$isPostOperator = $afterVariableEndPointer === null;
4344

4445
if ($tokens[$operatorPointer]['code'] === T_INC) {
4546
if ($isPostOperator) {

tests/Sniffs/Operators/DisallowIncrementAndDecrementOperatorsSniffTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ public function testErrors(): void
1616
{
1717
$report = self::checkFile(__DIR__ . '/data/disallowIncrementAndDecrementOperatorsErrors.php');
1818

19-
self::assertSame(4, $report->getErrorCount());
19+
self::assertSame(6, $report->getErrorCount());
2020

2121
self::assertSniffError($report, 3, DisallowIncrementAndDecrementOperatorsSniff::CODE_DISALLOWED_POST_INCREMENT_OPERATOR);
2222
self::assertSniffError($report, 4, DisallowIncrementAndDecrementOperatorsSniff::CODE_DISALLOWED_PRE_INCREMENT_OPERATOR);
2323
self::assertSniffError($report, 6, DisallowIncrementAndDecrementOperatorsSniff::CODE_DISALLOWED_POST_DECREMENT_OPERATOR);
2424
self::assertSniffError($report, 7, DisallowIncrementAndDecrementOperatorsSniff::CODE_DISALLOWED_PRE_DECREMENT_OPERATOR);
25+
self::assertSniffError($report, 9, DisallowIncrementAndDecrementOperatorsSniff::CODE_DISALLOWED_POST_INCREMENT_OPERATOR);
26+
self::assertSniffError($report, 10, DisallowIncrementAndDecrementOperatorsSniff::CODE_DISALLOWED_POST_DECREMENT_OPERATOR);
2527
}
2628

2729
}

tests/Sniffs/Operators/data/disallowIncrementAndDecrementOperatorsErrors.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55

66
$k--;
77
--$l;
8+
9+
$a['a']++;
10+
$a['a']--;

0 commit comments

Comments
 (0)