55namespace Rector \Transform \Rector \FuncCall ;
66
77use PhpParser \Node ;
8+ use PhpParser \Node \Arg ;
9+ use PhpParser \Node \Expr ;
10+ use PhpParser \Node \Expr \BinaryOp \BooleanAnd ;
811use PhpParser \Node \Expr \BinaryOp \Smaller ;
912use PhpParser \Node \Expr \ConstFetch ;
1013use PhpParser \Node \Expr \FuncCall ;
14+ use PhpParser \Node \Identifier ;
1115use PhpParser \Node \Name ;
1216use PhpParser \Node \Scalar \Int_ ;
17+ use PhpParser \Node \Scalar \String_ ;
1318use PhpParser \Node \Stmt \Expression ;
1419use PhpParser \Node \Stmt \If_ ;
1520use PhpParser \NodeVisitor ;
@@ -89,9 +94,12 @@ public function refactor(Node $node): null|Node|int
8994 }
9095
9196 $ phpVersionIdConst = new ConstFetch (new Name ('PHP_VERSION_ID ' ));
92- $ if = new If_ (new Smaller ($ phpVersionIdConst , new Int_ (
93- $ wrapFuncCallWithPhpVersionIdChecker ->getPhpVersionId ()
94- )));
97+ $ if = new If_ (new BooleanAnd (
98+ new FuncCall (new Name ('function_exists ' ), [new Arg (new String_ (
99+ $ wrapFuncCallWithPhpVersionIdChecker ->getFunctionName ()
100+ ))]),
101+ new Smaller ($ phpVersionIdConst , new Int_ ($ wrapFuncCallWithPhpVersionIdChecker ->getPhpVersionId ())),
102+ ));
95103 $ if ->stmts = [$ stmt ];
96104
97105 $ node ->stmts [$ key ] = $ if ;
@@ -120,17 +128,7 @@ private function isWrappedFuncCall(StmtsAwareInterface $node): bool
120128 return false ;
121129 }
122130
123- if (! $ node ->cond instanceof Smaller) {
124- return false ;
125- }
126-
127- if (! $ node ->cond ->left instanceof ConstFetch || ! $ this ->isName ($ node ->cond ->left ->name , 'PHP_VERSION_ID ' )) {
128- return false ;
129- }
130-
131- if (! $ node ->cond ->right instanceof Int_) {
132- return false ;
133- }
131+ $ phpVersionIdComparison = $ this ->getPhpVersionIdComparison ($ node ->cond );
134132
135133 if (count ($ node ->stmts ) !== 1 ) {
136134 return false ;
@@ -145,7 +143,7 @@ private function isWrappedFuncCall(StmtsAwareInterface $node): bool
145143 foreach ($ this ->wrapFuncCallWithPhpVersionIdCheckers as $ wrapFuncCallWithPhpVersionIdChecker ) {
146144 if (
147145 $ this ->getName ($ childStmt ->expr ) !== $ wrapFuncCallWithPhpVersionIdChecker ->getFunctionName ()
148- || $ node -> cond ->right ->value !== $ wrapFuncCallWithPhpVersionIdChecker ->getPhpVersionId ()
146+ || $ phpVersionIdComparison ->right ->value !== $ wrapFuncCallWithPhpVersionIdChecker ->getPhpVersionId ()
149147 ) {
150148 continue ;
151149 }
@@ -155,4 +153,25 @@ private function isWrappedFuncCall(StmtsAwareInterface $node): bool
155153
156154 return false ;
157155 }
156+
157+ private function getPhpVersionIdComparison (Expr $ expr ): ?Smaller
158+ {
159+ if ($ expr instanceof BooleanAnd) {
160+ return $ this ->getPhpVersionIdComparison ($ expr ->left ) ?? $ this ->getPhpVersionIdComparison ($ expr ->right );
161+ }
162+
163+ if (! $ expr instanceof Smaller) {
164+ return null ;
165+ }
166+
167+ if (! $ expr ->left instanceof ConstFetch || ! $ this ->isName ($ expr ->left ->name , 'PHP_VERSION_ID ' )) {
168+ return null ;
169+ }
170+
171+ if (! $ expr ->right instanceof Int_) {
172+ return null ;
173+ }
174+
175+ return $ expr ;
176+ }
158177}
0 commit comments