-
-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathRegexMatcher.php
More file actions
108 lines (85 loc) · 2.98 KB
/
RegexMatcher.php
File metadata and controls
108 lines (85 loc) · 2.98 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
<?php
declare(strict_types=1);
namespace Rector\Php55;
use Nette\Utils\Strings;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Scalar\String_;
final readonly class RegexMatcher
{
/**
* @see https://regex101.com/r/Ok4wuE/1
*/
private const string LAST_E_REGEX = '#(\w+)?e(\w+)?$#';
/**
* @see https://regex101.com/r/2NWVwT/1
*/
private const string LETTER_SUFFIX_REGEX = '#(?<modifiers>\w+)$#';
/**
* @var string[]
* @see https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
*/
private const array ALL_MODIFIERS_VALUES = ['i', 'm', 's', 'x', 'e', 'A', 'D', 'S', 'U', 'X', 'J', 'u'];
public function resolvePatternExpressionWithoutEIfFound(Expr $expr): Concat|String_|null
{
if ($expr instanceof String_) {
$pattern = $expr->value;
$delimiter = $pattern[0];
$delimiter = match ($delimiter) {
'(' => ')',
'{' => '}',
'[' => ']',
'<' => '>',
default => $delimiter
};
$modifiers = $this->resolveModifiers((string) Strings::after($pattern, $delimiter, -1));
if (! \str_contains($modifiers, 'e')) {
return null;
}
$expr->value = $this->createPatternWithoutE($pattern, $delimiter, $modifiers);
return $expr;
}
if ($expr instanceof Concat) {
return $this->matchConcat($expr);
}
return null;
}
private function resolveModifiers(string $modifiersCandidate): string
{
$modifiers = '';
for ($modifierIndex = 0; $modifierIndex < strlen($modifiersCandidate); ++$modifierIndex) {
if (! in_array($modifiersCandidate[$modifierIndex], self::ALL_MODIFIERS_VALUES, true)) {
$modifiers = '';
continue;
}
$modifiers .= $modifiersCandidate[$modifierIndex];
}
return $modifiers;
}
private function createPatternWithoutE(string $pattern, string $delimiter, string $modifiers): string
{
$modifiersWithoutE = str_replace('e', '', $modifiers);
return Strings::before($pattern, $delimiter, -1) . $delimiter . $modifiersWithoutE;
}
private function matchConcat(Concat $concat): ?Concat
{
// cause parse error
if (! $concat->left instanceof Concat) {
return null;
}
$lastItem = $concat->right;
if (! $lastItem instanceof String_) {
return null;
}
$matches = Strings::match($lastItem->value, self::LETTER_SUFFIX_REGEX);
if (! isset($matches['modifiers'])) {
return null;
}
if (! \str_contains($matches['modifiers'], 'e')) {
return null;
}
// replace last "e" in the code
$lastItem->value = Strings::replace($lastItem->value, self::LAST_E_REGEX, '$1$2');
return $concat;
}
}