Skip to content

Commit c541dfe

Browse files
committed
[CodeQuality] Skip not inline comparison identical on RepeatedOrEqualToInArrayRector
1 parent 73031f7 commit c541dfe

2 files changed

Lines changed: 35 additions & 8 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\BooleanOr\RepeatedOrEqualToInArrayRector\Fixture;
4+
5+
final class SkipNotInlineIdentical
6+
{
7+
public function demo($someVariable): bool
8+
{
9+
if ($someVariable === 'a' || $someVariable === 'b' || execute(fn(): bool => $someVariable === 'c')) {
10+
return true;
11+
}
12+
13+
return false;
14+
}
15+
}

rules/CodeQuality/Rector/BooleanOr/RepeatedOrEqualToInArrayRector.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use PhpParser\Node\Expr\FuncCall;
1515
use PhpParser\Node\Name;
1616
use Rector\CodeQuality\ValueObject\ComparedExprAndValueExpr;
17-
use Rector\PhpParser\Node\BetterNodeFinder;
1817
use Rector\Rector\AbstractRector;
1918
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2019
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -24,11 +23,6 @@
2423
*/
2524
final class RepeatedOrEqualToInArrayRector extends AbstractRector
2625
{
27-
public function __construct(
28-
private readonly BetterNodeFinder $betterNodeFinder,
29-
) {
30-
}
31-
3226
public function getRuleDefinition(): RuleDefinition
3327
{
3428
return new RuleDefinition(
@@ -103,8 +97,8 @@ public function refactor(Node $node): ?FuncCall
10397

10498
$args = $this->nodeFactory->createArgs([$firstComparedExprAndValue->getComparedExpr(), $array]);
10599

106-
$identicals = $this->betterNodeFinder->findInstanceOf($node, Identical::class);
107-
$equals = $this->betterNodeFinder->findInstanceOf($node, Equal::class);
100+
$identicals = $this->findIdenticalsOrEquals($node, Identical::class);
101+
$equals = $this->findIdenticalsOrEquals($node, Equal::class);
108102

109103
if ($identicals !== [] && $equals === []) {
110104
$args[] = new Arg(new ConstFetch(new Name('true')));
@@ -113,6 +107,24 @@ public function refactor(Node $node): ?FuncCall
113107
return new FuncCall(new Name('in_array'), $args);
114108
}
115109

110+
private function findIdenticalsOrEquals(BooleanOr $booleanOr, string $type = Identical::class): array
111+
{
112+
$identicalsOrEquals = [];
113+
if ($booleanOr->left instanceof $type) {
114+
$identicalsOrEquals[] = $booleanOr->left;
115+
}
116+
117+
if ($booleanOr->right instanceof BooleanOr) {
118+
$identicalsOrEquals[] = $this->findIdenticalsOrEquals($booleanOr->right, $type);
119+
}
120+
121+
if ($booleanOr->right instanceof $type) {
122+
$identicalsOrEquals[] = $booleanOr->right;
123+
}
124+
125+
return $identicalsOrEquals;
126+
}
127+
116128
private function isEqualOrIdentical(Expr $expr): bool
117129
{
118130
if ($expr instanceof Identical) {

0 commit comments

Comments
 (0)