Skip to content

Commit 7110f38

Browse files
committed
UselessParenthesesSniff: More checks
1 parent 83a5b05 commit 7110f38

File tree

5 files changed

+104
-58
lines changed

5 files changed

+104
-58
lines changed

SlevomatCodingStandard/Sniffs/PHP/UselessParenthesesSniff.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use const T_CLOSURE;
1212
use const T_DOLLAR;
1313
use const T_EMPTY;
14+
use const T_INLINE_THEN;
1415
use const T_ISSET;
1516
use const T_NS_SEPARATOR;
1617
use const T_OPEN_PARENTHESIS;
@@ -62,13 +63,55 @@ public function process(File $phpcsFile, $parenthesisOpenerPointer): void
6263
return;
6364
}
6465

66+
$this->checkParenthesesAroundConditionInTernaryOperator($phpcsFile, $parenthesisOpenerPointer);
6567
$this->checkParenthesesAroundVariableOrFunctionCall($phpcsFile, $parenthesisOpenerPointer);
6668
}
6769

70+
private function checkParenthesesAroundConditionInTernaryOperator(File $phpcsFile, int $parenthesisOpenerPointer): void
71+
{
72+
$tokens = $phpcsFile->getTokens();
73+
74+
$ternaryOperatorPointer = TokenHelper::findNextEffective($phpcsFile, $tokens[$parenthesisOpenerPointer]['parenthesis_closer'] + 1);
75+
76+
if ($tokens[$ternaryOperatorPointer]['code'] !== T_INLINE_THEN) {
77+
return;
78+
}
79+
80+
$contentStartPointer = TokenHelper::findNextEffective($phpcsFile, $parenthesisOpenerPointer + 1);
81+
$contentEndPointer = TokenHelper::findPreviousEffective($phpcsFile, $tokens[$parenthesisOpenerPointer]['parenthesis_closer'] - 1);
82+
83+
for ($i = $contentStartPointer; $i <= $contentEndPointer; $i++) {
84+
if ($tokens[$i]['code'] === T_INLINE_THEN) {
85+
return;
86+
}
87+
}
88+
89+
$fix = $phpcsFile->addFixableError('Useless parentheses.', $parenthesisOpenerPointer, self::CODE_USELESS_PARENTHESES);
90+
91+
if (!$fix) {
92+
return;
93+
}
94+
95+
$phpcsFile->fixer->beginChangeset();
96+
for ($i = $parenthesisOpenerPointer; $i < $contentStartPointer; $i++) {
97+
$phpcsFile->fixer->replaceToken($i, '');
98+
}
99+
for ($i = $contentEndPointer + 1; $i <= $tokens[$parenthesisOpenerPointer]['parenthesis_closer']; $i++) {
100+
$phpcsFile->fixer->replaceToken($i, '');
101+
}
102+
$phpcsFile->fixer->endChangeset();
103+
}
104+
68105
private function checkParenthesesAroundVariableOrFunctionCall(File $phpcsFile, int $parenthesisOpenerPointer): void
69106
{
70107
$tokens = $phpcsFile->getTokens();
71108

109+
$ternaryOperatorPointer = TokenHelper::findNextEffective($phpcsFile, $tokens[$parenthesisOpenerPointer]['parenthesis_closer'] + 1);
110+
111+
if ($tokens[$ternaryOperatorPointer]['code'] === T_INLINE_THEN) {
112+
return;
113+
}
114+
72115
/** @var int $contentStartPointer */
73116
$contentStartPointer = TokenHelper::findNextEffective($phpcsFile, $parenthesisOpenerPointer + 1);
74117
$notBooleanNotOperatorPointer = $contentStartPointer;

tests/Sniffs/PHP/UselessParenthesesSniffTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public function testErrors(): void
1717
{
1818
$report = self::checkFile(__DIR__ . '/data/uselessParenthesesErrors.php');
1919

20-
self::assertSame(26, $report->getErrorCount());
20+
self::assertSame(27, $report->getErrorCount());
2121

22-
foreach ([3, 5, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35] as $line) {
22+
foreach ([8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 36, 37, 38] as $line) {
2323
self::assertSniffError($report, $line, UselessParenthesesSniff::CODE_USELESS_PARENTHESES);
2424
}
2525

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
<?php
22

3-
$a = $b ? 1 : 0;
4-
5-
$c = $d ? 1 : 0;
6-
73
class Whatever
84
{
95

106
public function __construct($parameter)
117
{
12-
$x = self::$a ? 1 : 0;
13-
$x = static::$a ? 1 : 0;
14-
$x = self::$$parameter ? 1 : 0;
15-
$x = parent::${'a'} ? 1 : 0;
16-
$x = self::${'a'}[0] ? 1 : 0;
17-
$x = Anything::$a ? 1 : 0;
18-
$x = Something\Anything::$a ? 1 : 0;
19-
$x = \Something\Anything::$a ? 1 : 0;
20-
$x = self::$a::$b ? 1 : 0;
21-
$x = $this::$a ? 1 : 0;
22-
$x = $this->a ? 1 : 0;
23-
$x = $this->$$parameter ? 1 : 0;
24-
$x = $this->{'a'} ? 1 : 0;
25-
$x = $$parameter ? 1 : 0;
26-
$x = $this->${'a'}[0]->$$b[1][2]::$c[3][4][5]->{" $d"} ? 1 : 0;
27-
$x = $this->${'a'}[0]->$$b[1][2]::$c[3][4][5]->{" $d"}() ? 1 : 0;
28-
$x = $this->${'a'}[0]->$$b[1][2]::$c[3][4][5]->{" $d"}()()()() ? 1 : 0;
29-
$x = !$this->${'a'}[0]->$$b[1][2]::$c[3][4][5]->{" $d"}()()()() ? 1 : 0;
30-
$x = isset($xxx) ? 1 : 0;
31-
$x = !isset($xxx) ? 1 : 0;
32-
$x = empty($xxx) ? 1 : 0;
33-
$x = !empty($xxx) ? 1 : 0;
34-
$x = ! in_array($foo, ['bar', 'foo']) ? 1 : 0;
8+
$x = self::$a;
9+
$x = static::$a;
10+
$x = self::$$parameter;
11+
$x = parent::${'a'};
12+
$x = self::${'a'}[0];
13+
$x = Anything::$a;
14+
$x = Something\Anything::$a;
15+
$x = \Something\Anything::$a;
16+
$x = self::$a::$b;
17+
$x = $this::$a;
18+
$x = $this->a;
19+
$x = $this->$$parameter;
20+
$x = $this->{'a'};
21+
$x = $$parameter;
22+
$x = $this->${'a'}[0]->$$b[1][2]::$c[3][4][5]->{" $d"};
23+
$x = $this->${'a'}[0]->$$b[1][2]::$c[3][4][5]->{" $d"}();
24+
$x = $this->${'a'}[0]->$$b[1][2]::$c[3][4][5]->{" $d"}()()()();
25+
$x = !$this->${'a'}[0]->$$b[1][2]::$c[3][4][5]->{" $d"}()()()();
26+
$x = isset($xxx);
27+
$x = !isset($xxx);
28+
$x = empty($xxx);
29+
$x = !empty($xxx);
30+
$x = ! in_array($foo, ['bar', 'foo']);
3531
$x = intval($foo);
3632
}
3733

3834
}
35+
36+
$x = $y !== null ? true : false;
37+
$a = $b ? 1 : 0;
38+
$c = $d ? 1 : 0;
Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
<?php
22

3-
$a = ($b) ? 1 : 0;
4-
5-
$c = ( $d ) ? 1 : 0;
6-
73
class Whatever
84
{
95

106
public function __construct($parameter)
117
{
12-
$x = (self::$a) ? 1 : 0;
13-
$x = (static::$a) ? 1 : 0;
14-
$x = (self::$$parameter) ? 1 : 0;
15-
$x = (parent::${'a'}) ? 1 : 0;
16-
$x = (self::${'a'}[0]) ? 1 : 0;
17-
$x = (Anything::$a) ? 1 : 0;
18-
$x = (Something\Anything::$a) ? 1 : 0;
19-
$x = (\Something\Anything::$a) ? 1 : 0;
20-
$x = (self::$a::$b) ? 1 : 0;
21-
$x = ($this::$a) ? 1 : 0;
22-
$x = ($this->a) ? 1 : 0;
23-
$x = ($this->$$parameter) ? 1 : 0;
24-
$x = ($this->{'a'}) ? 1 : 0;
25-
$x = ($$parameter) ? 1 : 0;
26-
$x = ($this->${'a'}[0]->$$b[1][2]::$c[3][4][5]->{" $d"}) ? 1 : 0;
27-
$x = ($this->${'a'}[0]->$$b[1][2]::$c[3][4][5]->{" $d"}()) ? 1 : 0;
28-
$x = ($this->${'a'}[0]->$$b[1][2]::$c[3][4][5]->{" $d"}()()()()) ? 1 : 0;
29-
$x = (!$this->${'a'}[0]->$$b[1][2]::$c[3][4][5]->{" $d"}()()()()) ? 1 : 0;
30-
$x = (isset($xxx)) ? 1 : 0;
31-
$x = (!isset($xxx)) ? 1 : 0;
32-
$x = (empty($xxx)) ? 1 : 0;
33-
$x = (!empty($xxx)) ? 1 : 0;
34-
$x = ! (in_array($foo, ['bar', 'foo'])) ? 1 : 0;
8+
$x = (self::$a);
9+
$x = (static::$a);
10+
$x = (self::$$parameter);
11+
$x = (parent::${'a'});
12+
$x = (self::${'a'}[0]);
13+
$x = (Anything::$a);
14+
$x = (Something\Anything::$a);
15+
$x = (\Something\Anything::$a);
16+
$x = (self::$a::$b);
17+
$x = ($this::$a);
18+
$x = ($this->a);
19+
$x = ($this->$$parameter);
20+
$x = ($this->{'a'});
21+
$x = ($$parameter);
22+
$x = ($this->${'a'}[0]->$$b[1][2]::$c[3][4][5]->{" $d"});
23+
$x = ($this->${'a'}[0]->$$b[1][2]::$c[3][4][5]->{" $d"}());
24+
$x = ($this->${'a'}[0]->$$b[1][2]::$c[3][4][5]->{" $d"}()()()());
25+
$x = (!$this->${'a'}[0]->$$b[1][2]::$c[3][4][5]->{" $d"}()()()());
26+
$x = (isset($xxx));
27+
$x = (!isset($xxx));
28+
$x = (empty($xxx));
29+
$x = (!empty($xxx));
30+
$x = ! (in_array($foo, ['bar', 'foo']));
3531
$x = (intval($foo));
3632
}
3733

3834
}
35+
36+
$x = ($y !== null) ? true : false;
37+
$a = ($b) ? 1 : 0;
38+
$c = ( $d ) ? 1 : 0;

tests/Sniffs/PHP/data/uselessParenthesesNoErrors.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ function doSomething($parameter) {
1212

1313
}
1414

15-
$a = (10) ? 1 : 0;
15+
$a = 10 ? 1 : 0;
1616

17-
$b = ($c + $d) ? 1 : 0;
17+
$b = $c + $d ? 1 : 0;
1818

1919
$closure = function ($parameter) use ($inheritedVariable) {
2020

@@ -44,3 +44,6 @@ function doSomething($parameter) {
4444
new self($a);
4545
new static($b);
4646
new parent($c);
47+
48+
$z = ($a ? '0' : '1') ? '2' : '3';
49+
$zz = ($a + $b);

0 commit comments

Comments
 (0)