-
-
Notifications
You must be signed in to change notification settings - Fork 336
/
ConsistentPregDelimiterRector.php
261 lines (219 loc) · 7.28 KB
/
ConsistentPregDelimiterRector.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
declare(strict_types=1);
namespace Rector\CodingStyle\Rector\FuncCall;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Scalar\String_;
use PHPStan\Type\ObjectType;
use Rector\Core\Contract\PhpParser\NodePrinterInterface;
use Rector\Core\Contract\Rector\AllowEmptyConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\StringUtils;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodingStyle\Rector\FuncCall\ConsistentPregDelimiterRector\ConsistentPregDelimiterRectorTest
*/
final class ConsistentPregDelimiterRector extends AbstractRector implements AllowEmptyConfigurableRectorInterface
{
/**
* @api
* @var string
*/
public const DELIMITER = 'delimiter';
/**
* @var string
* @see https://regex101.com/r/isdgEN/1
*
* For modifiers see https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
*/
private const INNER_REGEX = '#(?<content>.*?)(?<close>[imsxeADSUXJu]*)$#s';
/**
* @var string
* @see https://regex101.com/r/nnuwUo/1
*
* For modifiers see https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
*/
private const INNER_UNICODE_REGEX = '#(?<content>.*?)(?<close>[imsxeADSUXJu]*)$#u';
/**
* @var string
* @see https://regex101.com/r/KpCzg6/1
*/
private const NEW_LINE_REGEX = '#(\\r|\\n)#';
/**
* @var string
* @see https://regex101.com/r/EyXsV6/6
*/
private const DOUBLE_QUOTED_REGEX = '#^"(?<delimiter>.{1}).{1,}\k<delimiter>[imsxeADSUXJu]*"$#';
/**
* All with pattern as 1st argument
* @var array<string, int>
*/
private const FUNCTIONS_WITH_REGEX_PATTERN = [
'preg_match' => 0,
'preg_replace_callback_array' => 0,
'preg_replace_callback' => 0,
'preg_replace' => 0,
'preg_match_all' => 0,
'preg_split' => 0,
'preg_grep' => 0,
];
/**
* All with pattern as 2st argument
* @var array<string, array<string, int>>
*/
private const STATIC_METHODS_WITH_REGEX_PATTERN = [
'Nette\Utils\Strings' => [
'match' => 1,
'matchAll' => 1,
'replace' => 1,
'split' => 1,
],
];
private string $delimiter = '#';
public function __construct(
private readonly NodePrinterInterface $nodePrinter
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Replace PREG delimiter with configured one', [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
preg_match('~value~', $value);
preg_match_all('~value~im', $value);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
preg_match('#value#', $value);
preg_match_all('#value#im', $value);
}
}
CODE_SAMPLE
,
[
self::DELIMITER => '#',
]
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [FuncCall::class, StaticCall::class];
}
/**
* @param FuncCall|StaticCall $node
*/
public function refactor(Node $node): ?Node
{
if ($node instanceof FuncCall) {
return $this->refactorFuncCall($node);
}
foreach (self::STATIC_METHODS_WITH_REGEX_PATTERN as $type => $methodsToPositions) {
if (! $this->isObjectType($node->class, new ObjectType($type))) {
continue;
}
foreach ($methodsToPositions as $method => $position) {
if (! $this->isName($node->name, $method)) {
continue;
}
if (! $node->args[$position] instanceof Arg) {
continue;
}
return $this->refactorArgument($node, $node->args[$position]);
}
}
return null;
}
public function configure(array $configuration): void
{
$this->delimiter = $configuration[self::DELIMITER] ?? (string) current($configuration);
}
private function refactorFuncCall(FuncCall $funcCall): ?FuncCall
{
foreach (self::FUNCTIONS_WITH_REGEX_PATTERN as $function => $position) {
if (! $this->isName($funcCall, $function)) {
continue;
}
if (! $funcCall->args[$position] instanceof Arg) {
continue;
}
return $this->refactorArgument($funcCall, $funcCall->args[$position]);
}
return null;
}
private function hasNewLineWithUnicodeModifier(string $string): bool
{
$matchInnerRegex = Strings::match($string, self::INNER_REGEX);
$matchInnerUnionRegex = Strings::match($string, self::INNER_UNICODE_REGEX);
if (! is_array($matchInnerRegex)) {
return false;
}
if (! is_array($matchInnerUnionRegex)) {
return false;
}
if ($matchInnerRegex === $matchInnerUnionRegex) {
return false;
}
return StringUtils::isMatch($matchInnerUnionRegex['content'], self::NEW_LINE_REGEX);
}
private function hasEscapedQuote(String_ $string): bool
{
$kind = $string->getAttribute(AttributeKey::KIND);
if ($kind === String_::KIND_DOUBLE_QUOTED && str_contains($string->value, '"')) {
return true;
}
return $kind === String_::KIND_SINGLE_QUOTED && str_contains($string->value, "'");
}
private function refactorArgument(FuncCall|StaticCall $node, Arg $arg): Node|null
{
if (! $arg->value instanceof String_) {
return null;
}
/** @var String_ $string */
$string = $arg->value;
if ($this->hasEscapedQuote($string)) {
return null;
}
if ($this->hasNewLineWithUnicodeModifier($string->value)) {
return null;
}
$string->value = Strings::replace($string->value, self::INNER_REGEX, function (array $match) use (
&$string
): string {
$printedString = $this->nodePrinter->print($string);
if (StringUtils::isMatch($printedString, self::DOUBLE_QUOTED_REGEX)) {
$string->setAttribute(AttributeKey::IS_REGULAR_PATTERN, true);
}
$innerPattern = $match['content'];
$positionDelimiter = strpos($innerPattern, $this->delimiter);
if ($positionDelimiter > 0) {
$innerPattern = str_replace($this->delimiter, '\\' . $this->delimiter, $innerPattern);
}
// change delimiter
if (strlen($innerPattern) > 2 && $innerPattern[0] === $innerPattern[strlen($innerPattern) - 1]) {
$innerPattern[0] = $this->delimiter;
$innerPattern[strlen($innerPattern) - 1] = $this->delimiter;
}
return $innerPattern . $match['close'];
});
return $node;
}
}