-
-
Notifications
You must be signed in to change notification settings - Fork 688
/
EregToPregMatchRector.php
179 lines (177 loc) · 6.44 KB
/
EregToPregMatchRector.php
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
<?php
declare (strict_types=1);
namespace Rector\Php70\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Scalar\String_;
use Rector\Php70\EregToPcreTransformer;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix202501\Webmozart\Assert\Assert;
/**
* @see \Rector\Tests\Php70\Rector\FuncCall\EregToPregMatchRector\EregToPregMatchRectorTest
*/
final class EregToPregMatchRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @readonly
*/
private EregToPcreTransformer $eregToPcreTransformer;
/**
* @var array<string, string>
*/
private const OLD_NAMES_TO_NEW_ONES = ['ereg' => 'preg_match', 'eregi' => 'preg_match', 'ereg_replace' => 'preg_replace', 'eregi_replace' => 'preg_replace', 'split' => 'preg_split', 'spliti' => 'preg_split'];
public function __construct(EregToPcreTransformer $eregToPcreTransformer)
{
$this->eregToPcreTransformer = $eregToPcreTransformer;
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::NO_EREG_FUNCTION;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Changes ereg*() to preg*() calls', [new CodeSample('ereg("hi")', 'preg_match("#hi#");')]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [FuncCall::class, Assign::class];
}
/**
* @param FuncCall|Assign $node
*/
public function refactor(Node $node) : ?Node
{
if ($node instanceof FuncCall) {
return $this->refactorFuncCall($node);
}
if (!$this->isEregFuncCallWithThreeArgs($node->expr)) {
return null;
}
/** @var FuncCall $funcCall */
$funcCall = $node->expr;
$node->expr = $this->createTernaryWithStrlenOfFirstMatch($funcCall);
return $node;
}
private function shouldSkipFuncCall(FuncCall $funcCall) : bool
{
$functionName = $this->getName($funcCall);
if ($functionName === null) {
return \true;
}
if (!isset(self::OLD_NAMES_TO_NEW_ONES[$functionName])) {
return \true;
}
if ($funcCall->isFirstClassCallable()) {
return \true;
}
return !isset($funcCall->getArgs()[0]);
}
private function processStringPattern(FuncCall $funcCall, String_ $string, string $functionName) : void
{
$pattern = $string->value;
$pattern = $this->eregToPcreTransformer->transform($pattern, $this->isCaseInsensitiveFunction($functionName));
$firstArg = $funcCall->getArgs()[0];
Assert::isInstanceOf($firstArg->value, String_::class);
$firstArg->value->value = $pattern;
}
private function processVariablePattern(FuncCall $funcCall, Variable $variable, string $functionName) : void
{
$pregQuotePatternNode = $this->nodeFactory->createFuncCall('preg_quote', [new Arg($variable), new Arg(new String_('#'))]);
$startConcat = new Concat(new String_('#'), $pregQuotePatternNode);
$endDelimiter = $this->isCaseInsensitiveFunction($functionName) ? '#mi' : '#m';
$concat = new Concat($startConcat, new String_($endDelimiter));
/** @var Arg $arg */
$arg = $funcCall->args[0];
$arg->value = $concat;
}
/**
* Equivalent of:
* split(' ', 'hey Tom', 0);
* ↓
* preg_split('# #', 'hey Tom', 1);
*/
private function processSplitLimitArgument(FuncCall $funcCall, string $functionName) : void
{
if (!isset($funcCall->args[2])) {
return;
}
if (!$funcCall->args[2] instanceof Arg) {
return;
}
if (\strncmp($functionName, 'split', \strlen('split')) !== 0) {
return;
}
// 3rd argument - $limit, 0 → 1
if (!$funcCall->args[2]->value instanceof Int_) {
return;
}
/** @var Int_ $limitNumberNode */
$limitNumberNode = $funcCall->args[2]->value;
if ($limitNumberNode->value !== 0) {
return;
}
$limitNumberNode->value = 1;
}
private function createTernaryWithStrlenOfFirstMatch(FuncCall $funcCall) : Ternary
{
$thirdArg = $funcCall->getArgs()[2];
$arrayDimFetch = new ArrayDimFetch($thirdArg->value, new Int_(0));
$strlenFuncCall = $this->nodeFactory->createFuncCall('strlen', [$arrayDimFetch]);
return new Ternary($funcCall, $strlenFuncCall, $this->nodeFactory->createFalse());
}
private function isCaseInsensitiveFunction(string $functionName) : bool
{
if (\strpos($functionName, 'eregi') !== \false) {
return \true;
}
return \strpos($functionName, 'spliti') !== \false;
}
private function isEregFuncCallWithThreeArgs(Expr $expr) : bool
{
if (!$expr instanceof FuncCall) {
return \false;
}
$functionName = $this->getName($expr);
if (!\is_string($functionName)) {
return \false;
}
if (!\in_array($functionName, ['ereg', 'eregi'], \true)) {
return \false;
}
return isset($expr->getArgs()[2]);
}
private function refactorFuncCall(FuncCall $funcCall) : ?FuncCall
{
if ($this->shouldSkipFuncCall($funcCall)) {
return null;
}
/** @var string $functionName */
$functionName = $this->getName($funcCall);
$firstArg = $funcCall->getArgs()[0];
$patternExpr = $firstArg->value;
if ($patternExpr instanceof String_) {
$this->processStringPattern($funcCall, $patternExpr, $functionName);
} elseif ($patternExpr instanceof Variable) {
$this->processVariablePattern($funcCall, $patternExpr, $functionName);
}
$this->processSplitLimitArgument($funcCall, $functionName);
$funcCall->name = new Name(self::OLD_NAMES_TO_NEW_ONES[$functionName]);
return $funcCall;
}
}