Repository navigation
Expand file tree
/
Copy pathRegularExpressionPatternRule.php
More file actions
147 lines (125 loc) · 3.39 KB
/
Copy pathRegularExpressionPatternRule.php
File metadata and controls
147 lines (125 loc) · 3.39 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
<?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\ArgumentsNormalizer;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Regex\RegexExpressionHelper;
use function in_array;
use function sprintf;
use function str_starts_with;
use function strtolower;
/**
* @implements Rule<Node\Expr\FuncCall>
*/
#[RegisteredRule(level: 0)]
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
{
if (!$node->name instanceof Node\Name) {
return [];
}
$functionName = strtolower((string) $node->name);
if (!str_starts_with($functionName, 'preg_')) {
return [];
}
$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);
$args = $functionCall->getArgs();
if (!isset($args[0])) {
return [];
}
$patternArg = ArgumentsNormalizer::getArgByPositionOrName($args, 0, 'pattern');
if ($patternArg === null) {
return [];
}
$patternNode = $patternArg->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) {
return $e->getMessage();
}
return null;
}
}