-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathWrapFuncCallWithPhpVersionIdCheckerRector.php
More file actions
184 lines (150 loc) · 5.34 KB
/
WrapFuncCallWithPhpVersionIdCheckerRector.php
File metadata and controls
184 lines (150 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
declare(strict_types=1);
namespace Rector\Transform\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\Smaller;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use PhpParser\NodeVisitor;
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use Rector\Transform\ValueObject\WrapFuncCallWithPhpVersionIdChecker;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
/**
* @see \Rector\Tests\Transform\Rector\FuncCall\WrapFuncCallWithPhpVersionIdCheckerRector\WrapFuncCallWithPhpVersionIdCheckerRectorTest
*/
final class WrapFuncCallWithPhpVersionIdCheckerRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var WrapFuncCallWithPhpVersionIdChecker[]
*/
private array $wrapFuncCallWithPhpVersionIdCheckers = [];
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Wraps function calls without assignment in a condition to check for a PHP version id',
[
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
no_op_function();
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
if (function_exists('no_op_function') && PHP_VERSION_ID < 80500) {
no_op_function();
}
CODE_SAMPLE
,
[new WrapFuncCallWithPhpVersionIdChecker('no_op_function', 80500)]
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [StmtsAwareInterface::class];
}
/**
* @param StmtsAwareInterface $node
*/
public function refactor(Node $node): null|Node|int
{
if ($node->stmts === null) {
return null;
}
if ($this->isWrappedFuncCall($node)) {
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
}
$hasChanged = false;
foreach ($node->stmts as $key => $stmt) {
if (! $stmt instanceof Expression) {
continue;
}
if (! $stmt->expr instanceof FuncCall) {
continue;
}
$funcCall = $stmt->expr;
foreach ($this->wrapFuncCallWithPhpVersionIdCheckers as $wrapFuncCallWithPhpVersionIdChecker) {
if ($this->getName($funcCall) !== $wrapFuncCallWithPhpVersionIdChecker->getFunctionName()) {
continue;
}
$phpVersionIdConst = new ConstFetch(new Name('PHP_VERSION_ID'));
$if = new If_(new BooleanAnd(
new FuncCall(new Name('function_exists'), [new Arg(new String_(
$wrapFuncCallWithPhpVersionIdChecker->getFunctionName()
))]),
new Smaller($phpVersionIdConst, new Int_($wrapFuncCallWithPhpVersionIdChecker->getPhpVersionId())),
));
$if->stmts = [$stmt];
$node->stmts[$key] = $if;
$hasChanged = true;
}
}
if ($hasChanged) {
return $node;
}
return null;
}
public function configure(array $configuration): void
{
Assert::allIsInstanceOf($configuration, WrapFuncCallWithPhpVersionIdChecker::class);
$this->wrapFuncCallWithPhpVersionIdCheckers = $configuration;
}
private function isWrappedFuncCall(StmtsAwareInterface $stmtsAware): bool
{
if (! $stmtsAware instanceof If_) {
return false;
}
$phpVersionId = $this->getPhpVersionId($stmtsAware->cond);
if (!$phpVersionId instanceof Int_) {
return false;
}
if (count($stmtsAware->stmts) !== 1) {
return false;
}
$childStmt = $stmtsAware->stmts[0];
if (! $childStmt instanceof Expression || ! $childStmt->expr instanceof FuncCall) {
return false;
}
foreach ($this->wrapFuncCallWithPhpVersionIdCheckers as $wrapFuncCallWithPhpVersionIdChecker) {
if ($this->getName($childStmt->expr) !== $wrapFuncCallWithPhpVersionIdChecker->getFunctionName()) {
continue;
}
if ($phpVersionId->value !== $wrapFuncCallWithPhpVersionIdChecker->getPhpVersionId()) {
continue;
}
return true;
}
return false;
}
private function getPhpVersionId(Expr $expr): ?Int_
{
if ($expr instanceof BooleanAnd) {
return $this->getPhpVersionId($expr->left) ?? $this->getPhpVersionId($expr->right);
}
if (! $expr instanceof Smaller) {
return null;
}
if (! $expr->left instanceof ConstFetch || ! $this->isName($expr->left->name, 'PHP_VERSION_ID')) {
return null;
}
if (! $expr->right instanceof Int_) {
return null;
}
return $expr->right;
}
}