1414use PhpParser \Node \Expr \FuncCall ;
1515use PhpParser \Node \Name ;
1616use Rector \CodeQuality \ValueObject \ComparedExprAndValueExpr ;
17- use Rector \PhpParser \Node \BetterNodeFinder ;
1817use Rector \Rector \AbstractRector ;
1918use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
2019use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
2423 */
2524final 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