-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathDowngradePregUnmatchedAsNullConstantRector.php
More file actions
239 lines (198 loc) · 6.76 KB
/
DowngradePregUnmatchedAsNullConstantRector.php
File metadata and controls
239 lines (198 loc) · 6.76 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp72\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\BitwiseOr;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use Rector\DowngradePhp72\NodeAnalyzer\RegexFuncAnalyzer;
use Rector\DowngradePhp72\NodeManipulator\BitwiseFlagCleaner;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradePregUnmatchedAsNullConstantRector\DowngradePregUnmatchedAsNullConstantRectorTest
*/
final class DowngradePregUnmatchedAsNullConstantRector extends AbstractRector
{
/**
* @see https://www.php.net/manual/en/function.preg-match.php
*/
private const string UNMATCHED_NULL_FLAG = 'PREG_UNMATCHED_AS_NULL';
public function __construct(
private readonly BitwiseFlagCleaner $bitwiseFlagCleaner,
private readonly RegexFuncAnalyzer $regexFuncAnalyzer,
private readonly BetterNodeFinder $betterNodeFinder,
) {
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Expression::class, ClassConst::class, If_::class];
}
/**
* @param Expression|ClassConst|If_ $node
* @return Stmt[]|ClassConst|null
*/
public function refactor(Node $node)
{
if ($node instanceof ClassConst) {
return $this->refactorClassConst($node);
}
$funcCall = $this->matchRegexFuncCall($node);
if (! $funcCall instanceof FuncCall) {
return null;
}
$args = $funcCall->getArgs();
$variable = $args[2]->value;
if (! $variable instanceof Variable) {
return null;
}
if (! isset($args[3])) {
return null;
}
$flagsExpr = $args[3]->value;
if ($flagsExpr instanceof BitwiseOr) {
$this->bitwiseFlagCleaner->cleanFuncCall($funcCall, $flagsExpr, self::UNMATCHED_NULL_FLAG);
if ($this->nodeComparator->areNodesEqual($flagsExpr, $args[3]->value)) {
return null;
}
return $this->handleEmptyStringToNullMatch($funcCall, $variable, $node);
}
if (! $flagsExpr instanceof ConstFetch) {
return null;
}
if (! $this->isName($flagsExpr, self::UNMATCHED_NULL_FLAG)) {
return null;
}
unset($funcCall->args[3]);
return $this->handleEmptyStringToNullMatch($funcCall, $variable, $node);
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Remove PREG_UNMATCHED_AS_NULL from preg_match and set null value on empty string matched on each match',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
preg_match('/(a)(b)*(c)/', 'ac', $matches, PREG_UNMATCHED_AS_NULL);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
preg_match('/(a)(b)*(c)/', 'ac', $matches);
array_walk_recursive($matches, function (&$value) {
if ($value === '') {
$value = null;
}
});
}
}
CODE_SAMPLE
),
]
);
}
private function refactorClassConst(ClassConst $classConst): ?ClassConst
{
foreach ($classConst->consts as $key => $singleClassConst) {
if (! $singleClassConst->value instanceof ConstFetch) {
continue;
}
if (! $this->isName($singleClassConst->value, self::UNMATCHED_NULL_FLAG)) {
continue;
}
$classConst->consts[$key]->value = new Int_(512);
return $classConst;
}
return null;
}
/**
* @return Stmt|Stmt[]|null
*/
private function handleEmptyStringToNullMatch(FuncCall $funcCall, Variable $variable, Node $node): Stmt|array|null
{
$variablePass = new Variable('value');
$assign = new Assign($variablePass, $this->nodeFactory->createNull());
$if = $this->createIf($variablePass, $assign);
$param = new Param($variablePass, null, null, true);
$closure = new Closure([
'params' => [$param],
'stmts' => [$if],
]);
$arguments = $this->nodeFactory->createArgs([$variable, $closure]);
$arrayWalkRecursiveFuncCall = $this->nodeFactory->createFuncCall('array_walk_recursive', $arguments);
return $this->processReplace($funcCall, $arrayWalkRecursiveFuncCall, $node);
}
/**
* @return Stmt|Stmt[]|null
*/
private function processReplace(FuncCall $funcCall, FuncCall $replaceEmptyStringToNull, Stmt $stmt): Stmt|array|null
{
if ($stmt instanceof If_ && $stmt->cond === $funcCall) {
return $this->processInIf($stmt, $replaceEmptyStringToNull);
}
return [$stmt, new Expression($replaceEmptyStringToNull)];
}
private function processInIf(If_ $if, FuncCall $funcCall): ?Stmt
{
$cond = $if->cond;
if ($cond instanceof Identical) {
return null;
}
if ($cond instanceof BooleanNot) {
return null;
}
return $this->handleNotInIdenticalAndBooleanNot($if, $funcCall);
}
private function handleNotInIdenticalAndBooleanNot(If_ $if, FuncCall $funcCall): Stmt
{
if ($if->stmts !== []) {
return $if->stmts[0];
}
$if->stmts[0] = new Expression($funcCall);
return $if;
}
private function matchRegexFuncCall(Expression|If_ $stmt): ?FuncCall
{
$funcCalls = $this->betterNodeFinder->findInstancesOf($stmt, [FuncCall::class]);
foreach ($funcCalls as $funcCall) {
if (! $this->regexFuncAnalyzer->matchRegexFuncCall($funcCall) instanceof FuncCall) {
continue;
}
return $funcCall;
}
return null;
}
private function createIf(Variable $variable, Assign $assign): If_
{
$conditionIdentical = new Identical($variable, new String_(''));
return new If_($conditionIdentical, [
'stmts' => [new Expression($assign)],
]);
}
}