Repository navigation
Expand file tree
/
Copy pathIgnoredRegexValidator.php
More file actions
163 lines (134 loc) · 4.06 KB
/
Copy pathIgnoredRegexValidator.php
File metadata and controls
163 lines (134 loc) · 4.06 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
<?php declare(strict_types = 1);
namespace PHPStan\Command;
use Nette\Utils\Strings;
use PHPRegex\Parser\Exception\RegexException;
use PHPRegex\Parser\Node\AlternationNode;
use PHPRegex\Parser\Node\AnchorNode;
use PHPRegex\Parser\Node\NodeInterface;
use PHPRegex\Parser\Node\SequenceNode;
use PHPStan\PhpDoc\TypeStringResolver;
use PHPStan\PhpDocParser\Parser\ParserException;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Regex\PHPRegexReader;
use PHPStan\Type\VerbosityLevel;
use function count;
use function strrpos;
use function substr;
final class IgnoredRegexValidator
{
public function __construct(
private TypeStringResolver $typeStringResolver,
private PHPRegexReader $reader,
)
{
}
public function validate(string $regex): IgnoredRegexValidatorResult
{
$delimiter = substr($regex, 0, 1);
$regex = $this->removeDelimiters($regex);
try {
$root = $this->reader->read($regex, '', $delimiter);
} catch (RegexException) {
return new IgnoredRegexValidatorResult(ignoredTypes: [], anchorsInTheMiddle: false, allErrorsIgnored: false);
}
if (Strings::match($regex, '~(?<!\\\\)(?:\\\\\\\\)*\|\|~')) {
return new IgnoredRegexValidatorResult(ignoredTypes: [], anchorsInTheMiddle: false, allErrorsIgnored: true, wrongSequence: '||', escapedWrongSequence: '\|\|');
}
if (Strings::match($regex, '~(?<!\\\\)(?:\\\\\\\\)*\(\)~')) {
return new IgnoredRegexValidatorResult(ignoredTypes: [], anchorsInTheMiddle: false, allErrorsIgnored: true, wrongSequence: '()', escapedWrongSequence: '\(\)');
}
return new IgnoredRegexValidatorResult(
ignoredTypes: $this->getIgnoredTypes($root->pattern, $root->source ?? $regex),
anchorsInTheMiddle: $this->hasAnchorsInTheMiddle($root->pattern),
allErrorsIgnored: false,
);
}
/**
* @return array<string, string>
*/
private function getIgnoredTypes(NodeInterface $ast, string $source): array
{
if (!$ast instanceof AlternationNode) {
return [];
}
$types = [];
foreach ($ast->alternatives as $alternative) {
$text = $this->getText($alternative, $source);
if ($text === null) {
continue;
}
$matches = Strings::match($text, '#^([a-zA-Z0-9]+)[,]?\s*#');
if ($matches === null) {
continue;
}
try {
$type = $this->typeStringResolver->resolve($matches[1], null);
} catch (ParserException) {
continue;
}
if ($type instanceof ObjectType) {
continue;
}
$typeDescription = $type->describe(VerbosityLevel::typeOnly());
if ($typeDescription !== $matches[1]) {
continue;
}
$types[$typeDescription] = $text;
}
return $types;
}
private function removeDelimiters(string $regex): string
{
$delimiter = substr($regex, 0, 1);
$endDelimiterPosition = strrpos($regex, $delimiter);
if ($endDelimiterPosition === false) {
throw new ShouldNotHappenException();
}
return substr($regex, 1, $endDelimiterPosition - 1);
}
/**
* The text of a branch as it was written, escapes included: the tree
* decodes them, so each leaf is sliced from the source between its
* positions, whatever the leaf spells.
*/
private function getText(NodeInterface $node, string $source): ?string
{
if ($node instanceof SequenceNode) {
$fullText = '';
foreach ($node->children as $child) {
$text = $this->getText($child, $source);
if ($text === null) {
continue;
}
$fullText .= $text;
}
if ($fullText === '') {
return null;
}
return $fullText;
}
if ($node->getChildren() !== []) {
return null;
}
return substr($source, $node->getStartPosition(), $node->getEndPosition() - $node->getStartPosition());
}
private function hasAnchorsInTheMiddle(NodeInterface $ast): bool
{
if ($ast instanceof AnchorNode) {
return $ast->value === '$';
}
$children = $ast instanceof SequenceNode ? $ast->children : $ast->getChildren();
$childrenCount = count($children);
foreach ($children as $i => $child) {
$has = $this->hasAnchorsInTheMiddle($child);
if (
$has
&& (!$ast instanceof SequenceNode || $i !== $childrenCount - 1)
) {
return true;
}
}
return false;
}
}