Skip to content

Commit d81dc8a

Browse files
committed
Add additional function_exists condition
1 parent 50b18ce commit d81dc8a

File tree

3 files changed

+48
-17
lines changed

3 files changed

+48
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Rector\Tests\Transform\Rector\FuncCall\WrapFuncCallWithPhpVersionIdCheckerRector\Fixture;
4+
5+
if (function_exists('no_op_function') && PHP_VERSION_ID < 80500) {
6+
no_op_function();
7+
}
8+
if (PHP_VERSION_ID < 80500 && function_exists('no_op_function')) {
9+
no_op_function(1, 2);
10+
}
11+
12+
?>

rules-tests/Transform/Rector/FuncCall/WrapFuncCallWithPhpVersionIdCheckerRector/Fixture/wrapped_function.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ no_op_function(1, 2);
1111

1212
namespace Rector\Tests\Transform\Rector\FuncCall\WrapFuncCallWithPhpVersionIdCheckerRector\Fixture;
1313

14-
if (PHP_VERSION_ID < 80500) {
14+
if (function_exists('no_op_function') && PHP_VERSION_ID < 80500) {
1515
no_op_function();
1616
}
17-
if (PHP_VERSION_ID < 80500) {
17+
if (function_exists('no_op_function') && PHP_VERSION_ID < 80500) {
1818
no_op_function(1, 2);
1919
}
2020

rules/Transform/Rector/FuncCall/WrapFuncCallWithPhpVersionIdCheckerRector.php

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55
namespace Rector\Transform\Rector\FuncCall;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Arg;
9+
use PhpParser\Node\Expr;
10+
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
811
use PhpParser\Node\Expr\BinaryOp\Smaller;
912
use PhpParser\Node\Expr\ConstFetch;
1013
use PhpParser\Node\Expr\FuncCall;
14+
use PhpParser\Node\Identifier;
1115
use PhpParser\Node\Name;
1216
use PhpParser\Node\Scalar\Int_;
17+
use PhpParser\Node\Scalar\String_;
1318
use PhpParser\Node\Stmt\Expression;
1419
use PhpParser\Node\Stmt\If_;
1520
use 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

Comments
 (0)