55namespace Rector \DeadCode \Rector \If_ ;
66
77use PhpParser \Node ;
8- use PhpParser \Node \Expr ;
9- use PhpParser \Node \Expr \ArrayDimFetch ;
10- use PhpParser \Node \Expr \Assign ;
118use PhpParser \Node \Expr \BinaryOp \BooleanAnd ;
12- use PhpParser \Node \Expr \CallLike ;
13- use PhpParser \Node \Expr \MethodCall ;
14- use PhpParser \Node \Expr \PropertyFetch ;
15- use PhpParser \Node \Expr \StaticCall ;
16- use PhpParser \Node \Expr \StaticPropertyFetch ;
179use PhpParser \Node \Stmt \Else_ ;
1810use PhpParser \Node \Stmt \If_ ;
1911use PhpParser \NodeVisitor ;
12+ use Rector \DeadCode \SideEffect \SideEffectNodeDetector ;
2013use Rector \EarlyReturn \NodeTransformer \ConditionInverter ;
21- use Rector \PhpParser \Node \BetterNodeFinder ;
2214use Rector \Rector \AbstractRector ;
2315use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
2416use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
@@ -30,7 +22,7 @@ final class RemoveDeadIfBlocksRector extends AbstractRector
3022{
3123 public function __construct (
3224 private readonly ConditionInverter $ conditionInverter ,
33- private readonly BetterNodeFinder $ betterNodeFinder ,
25+ private readonly SideEffectNodeDetector $ sideEffectNodeDetector ,
3426 ) {
3527 }
3628
@@ -112,7 +104,7 @@ public function refactor(Node $node): int|null|If_
112104 foreach ($ node ->elseifs as $ elseif ) {
113105 $ keep_elseifs = array_filter (
114106 $ node ->elseifs ,
115- fn ($ elseif ) => $ elseif ->stmts !== [] || $ this ->shouldSkipExpr ($ elseif ->cond )
107+ fn ($ elseif ) => $ elseif ->stmts !== [] || $ this ->sideEffectNodeDetector -> detect ($ elseif ->cond )
116108 );
117109 if (count ($ node ->elseifs ) !== count ($ keep_elseifs )) {
118110 $ node ->elseifs = $ keep_elseifs ;
@@ -131,7 +123,7 @@ public function refactor(Node $node): int|null|If_
131123 return null ;
132124 }
133125
134- if ($ this ->shouldSkipExpr ($ node ->cond )) {
126+ if ($ this ->sideEffectNodeDetector -> detect ($ node ->cond )) {
135127 return null ;
136128 }
137129
@@ -161,20 +153,4 @@ public function refactor(Node $node): int|null|If_
161153
162154 return NodeVisitor::REMOVE_NODE ;
163155 }
164-
165- private function shouldSkipExpr (Expr $ expr ): bool
166- {
167- return (bool ) $ this ->betterNodeFinder ->findInstancesOf (
168- $ expr ,
169- [
170- Assign::class,
171- ArrayDimFetch::class,
172- CallLike::class,
173- MethodCall::class,
174- PropertyFetch::class,
175- StaticCall::class,
176- StaticPropertyFetch::class,
177- ]
178- );
179- }
180156}
0 commit comments