-
Notifications
You must be signed in to change notification settings - Fork 504
/
Copy pathRegularExpressionPatternRule.php
147 lines (128 loc) · 3.58 KB
/
RegularExpressionPatternRule.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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Regexp;
use Nette\Utils\RegexpException;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Regex\RegexExpressionHelper;
use function in_array;
use function sprintf;
use function str_starts_with;
use function strlen;
use function strpos;
use function strtolower;
use function substr;
/**
* @implements Rule<Node\Expr\FuncCall>
*/
final class RegularExpressionPatternRule implements Rule
{
public function __construct(
private RegexExpressionHelper $regexExpressionHelper,
)
{
}
public function getNodeType(): string
{
return FuncCall::class;
}
public function processNode(Node $node, Scope $scope): array
{
$patterns = $this->extractPatterns($node, $scope);
$errors = [];
foreach ($patterns as $pattern) {
$errorMessage = $this->validatePattern($pattern);
if ($errorMessage === null) {
continue;
}
$errors[] = RuleErrorBuilder::message(sprintf('Regex pattern is invalid: %s', $errorMessage))->identifier('regexp.pattern')->build();
}
return $errors;
}
/**
* @return string[]
*/
private function extractPatterns(FuncCall $functionCall, Scope $scope): array
{
if (!$functionCall->name instanceof Node\Name) {
return [];
}
$functionName = strtolower((string) $functionCall->name);
if (!str_starts_with($functionName, 'preg_')) {
return [];
}
if (!isset($functionCall->getArgs()[0])) {
return [];
}
$patternNode = $functionCall->getArgs()[0]->value;
$patternType = $scope->getType($patternNode);
$patternStrings = [];
if (
in_array($functionName, [
'preg_match',
'preg_match_all',
'preg_split',
'preg_grep',
'preg_replace',
'preg_replace_callback',
'preg_filter',
], true)
) {
if ($patternNode instanceof Node\Expr\BinaryOp\Concat) {
$patternType = $this->regexExpressionHelper->resolvePatternConcat($patternNode, $scope);
}
foreach ($patternType->getConstantStrings() as $constantStringType) {
$patternStrings[] = $constantStringType->getValue();
}
}
if (
in_array($functionName, [
'preg_replace',
'preg_replace_callback',
'preg_filter',
], true)
) {
foreach ($patternType->getConstantArrays() as $constantArrayType) {
foreach ($constantArrayType->getValueTypes() as $arrayKeyType) {
foreach ($arrayKeyType->getConstantStrings() as $constantString) {
$patternStrings[] = $constantString->getValue();
}
}
}
}
if ($functionName === 'preg_replace_callback_array') {
foreach ($patternType->getConstantArrays() as $constantArrayType) {
foreach ($constantArrayType->getKeyTypes() as $arrayKeyType) {
foreach ($arrayKeyType->getConstantStrings() as $constantString) {
$patternStrings[] = $constantString->getValue();
}
}
}
}
return $patternStrings;
}
private function validatePattern(string $pattern): ?string
{
try {
Strings::match('', $pattern);
} catch (RegexpException $e) {
$invalidPatternMessage = $e->getMessage();
try {
Strings::match($invalidPatternMessage, '//u');
return $invalidPatternMessage;
} catch (RegexpException) {
$patternPos = strpos($invalidPatternMessage, 'pattern:');
if ($patternPos === false) {
throw new ShouldNotHappenException();
}
// strip invalid utf-8 pattern contents to keep the error message NEON parsable.
return substr($invalidPatternMessage, 0, $patternPos + strlen('pattern:') - 1);
}
}
return null;
}
}