Skip to content

Commit 67cedef

Browse files
committed
Fix OrChainIdenticalComparisonToInArrayRule
1 parent 289ecd0 commit 67cedef

4 files changed

Lines changed: 25 additions & 1 deletion

File tree

build/PHPStan/Build/OrChainIdenticalComparisonToInArrayRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private static function isSubjectNode(Expr $node): bool
136136
*/
137137
private function getSubjectAndValue(Identical $comparison): ?array
138138
{
139-
if (self::isSubjectNode($comparison->left) && !self::isSubjectNode($comparison->left)) {
139+
if (self::isSubjectNode($comparison->left) && !self::isSubjectNode($comparison->right)) {
140140
return ['subject' => $comparison->right, 'value' => $comparison->left];
141141
}
142142

tests/PHPStan/Build/OrChainIdenticalComparisonToInArrayRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ public function testRule(): void
3838
'This chain of identical comparisons can be simplified using in_array().',
3939
17,
4040
],
41+
[
42+
'This chain of identical comparisons can be simplified using in_array().',
43+
21,
44+
],
45+
[
46+
'This chain of identical comparisons can be simplified using in_array().',
47+
25,
48+
],
4149
]);
4250
}
4351

tests/PHPStan/Build/data/or-chain-identical-comparison.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
echo 'ok';
1919
}
2020

21+
if ('foo' === $var || 'bar' === $var) {
22+
echo 'ok';
23+
}
24+
25+
if ('foo' === $var || $var === 'bar') {
26+
echo 'ok';
27+
}
28+
2129
if ($var === 'foo' || $var === 'bar' || $var2 === 'buz') {
2230
echo 'no';
2331
}

tests/PHPStan/Build/data/or-chain-identical-comparison.php.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ if (\in_array($var, ['foo', 'bar', 'buz'], true)) {
1818
echo 'ok';
1919
}
2020

21+
if (\in_array($var, ['foo', 'bar'], true)) {
22+
echo 'ok';
23+
}
24+
25+
if (\in_array($var, ['foo', 'bar'], true)) {
26+
echo 'ok';
27+
}
28+
2129
if ($var === 'foo' || $var === 'bar' || $var2 === 'buz') {
2230
echo 'no';
2331
}

0 commit comments

Comments
 (0)