/
RegularExpressionPatternRule.php
129 lines (107 loc) · 2.71 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
<?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\Type\Constant\ConstantStringType;
use function in_array;
use function sprintf;
use function str_starts_with;
use function strtolower;
/**
* @implements Rule<Node\Expr\FuncCall>
*/
class RegularExpressionPatternRule implements Rule
{
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))->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 = [];
foreach ($patternType->getConstantStrings() as $constantStringType) {
if (
!in_array($functionName, [
'preg_match',
'preg_match_all',
'preg_split',
'preg_grep',
'preg_replace',
'preg_replace_callback',
'preg_filter',
], true)
) {
continue;
}
$patternStrings[] = $constantStringType->getValue();
}
foreach ($patternType->getConstantArrays() as $constantArrayType) {
if (
in_array($functionName, [
'preg_replace',
'preg_replace_callback',
'preg_filter',
], true)
) {
foreach ($constantArrayType->getValueTypes() as $arrayKeyType) {
if (!$arrayKeyType instanceof ConstantStringType) {
continue;
}
$patternStrings[] = $arrayKeyType->getValue();
}
}
if ($functionName !== 'preg_replace_callback_array') {
continue;
}
foreach ($constantArrayType->getKeyTypes() as $arrayKeyType) {
if (!$arrayKeyType instanceof ConstantStringType) {
continue;
}
$patternStrings[] = $arrayKeyType->getValue();
}
}
return $patternStrings;
}
private function validatePattern(string $pattern): ?string
{
try {
Strings::match('', $pattern);
} catch (RegexpException $e) {
return $e->getMessage();
}
return null;
}
}