Skip to content

Commit 0ba2d4e

Browse files
committed
Added DisallowIncrementAndDecrementOperatorsSniff
1 parent 068427b commit 0ba2d4e

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ Requires use of null coalesce operator when possible.
100100

101101
Requires use of early exit.
102102

103+
#### SlevomatCodingStandard.Operators.DisallowIncrementAndDecrementOperators
104+
105+
Disallows using `++` and `--` operators.
106+
103107
### Cleaning - detecting dead code
104108

105109
#### SlevomatCodingStandard.Classes.UnusedPrivateElements 🚧
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SlevomatCodingStandard\Sniffs\Operators;
4+
5+
use SlevomatCodingStandard\Helpers\TokenHelper;
6+
7+
class DisallowIncrementAndDecrementOperatorsSniff implements \PHP_CodeSniffer\Sniffs\Sniff
8+
{
9+
10+
public const CODE_DISALLOWED_PRE_INCREMENT_OPERATOR = 'DisallowedPreIncrementOperator';
11+
public const CODE_DISALLOWED_POST_INCREMENT_OPERATOR = 'DisallowedPostIncrementOperator';
12+
public const CODE_DISALLOWED_PRE_DECREMENT_OPERATOR = 'DisallowedPreDecrementOperator';
13+
public const CODE_DISALLOWED_POST_DECREMENT_OPERATOR = 'DisallowedPostDecrementOperator';
14+
15+
/**
16+
* @return mixed[]
17+
*/
18+
public function register(): array
19+
{
20+
return [
21+
T_DEC,
22+
T_INC,
23+
];
24+
}
25+
26+
/**
27+
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
28+
* @param \PHP_CodeSniffer\Files\File $phpcsFile
29+
* @param int $operatorPointer
30+
*/
31+
public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $operatorPointer): void
32+
{
33+
$tokens = $phpcsFile->getTokens();
34+
35+
$previousPointer = TokenHelper::findPreviousEffective($phpcsFile, $operatorPointer - 1);
36+
37+
$isPostOperator = $tokens[$previousPointer]['code'] === T_VARIABLE;
38+
39+
if ($tokens[$operatorPointer]['code'] === T_INC) {
40+
if ($isPostOperator) {
41+
$code = self::CODE_DISALLOWED_POST_INCREMENT_OPERATOR;
42+
$message = 'Use of post-increment operator is disallowed.';
43+
} else {
44+
$code = self::CODE_DISALLOWED_PRE_INCREMENT_OPERATOR;
45+
$message = 'Use of pre-increment operator is disallowed.';
46+
}
47+
} else {
48+
if ($isPostOperator) {
49+
$code = self::CODE_DISALLOWED_POST_DECREMENT_OPERATOR;
50+
$message = 'Use of post-decrement operator is disallowed.';
51+
} else {
52+
$code = self::CODE_DISALLOWED_PRE_DECREMENT_OPERATOR;
53+
$message = 'Use of pre-decrement operator is disallowed.';
54+
}
55+
}
56+
57+
$phpcsFile->addError($message, $operatorPointer, $code);
58+
}
59+
60+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SlevomatCodingStandard\Sniffs\Operators;
4+
5+
class DisallowIncrementAndDecrementOperatorsSniffTest extends \SlevomatCodingStandard\Sniffs\TestCase
6+
{
7+
8+
public function testNoErrors(): void
9+
{
10+
self::assertNoSniffErrorInFile(self::checkFile(__DIR__ . '/data/disallowIncrementAndDecrementOperatorsNoErrors.php'));
11+
}
12+
13+
public function testErrors(): void
14+
{
15+
$report = self::checkFile(__DIR__ . '/data/disallowIncrementAndDecrementOperatorsErrors.php');
16+
17+
self::assertSame(4, $report->getErrorCount());
18+
19+
self::assertSniffError($report, 3, DisallowIncrementAndDecrementOperatorsSniff::CODE_DISALLOWED_POST_INCREMENT_OPERATOR);
20+
self::assertSniffError($report, 4, DisallowIncrementAndDecrementOperatorsSniff::CODE_DISALLOWED_PRE_INCREMENT_OPERATOR);
21+
self::assertSniffError($report, 6, DisallowIncrementAndDecrementOperatorsSniff::CODE_DISALLOWED_POST_DECREMENT_OPERATOR);
22+
self::assertSniffError($report, 7, DisallowIncrementAndDecrementOperatorsSniff::CODE_DISALLOWED_PRE_DECREMENT_OPERATOR);
23+
}
24+
25+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
$i++;
4+
++$j;
5+
6+
$k--;
7+
--$l;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
$i+= 1;
4+
5+
$j-= 1;

0 commit comments

Comments
 (0)