Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Type/Php/RegexExpressionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function resolve(Expr $expr): Type
&& $expr->name instanceof Name
&& $expr->name->toLowerString() === 'preg_quote'
) {
return new ConstantStringType('');
return new ConstantStringType('.*');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of dummy-replace preg_quote($x) with '' we use .* as a placeholder, so we don't produce invalid patterns

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain the constant string meaning? AFAICT such assumption here can produce misleading assumptions/phpstan errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We replace a preg_quote with some constant value so we can finally resolve a string concatenation to a pattern at analysis time.

The preg_quoted value cannot return something which creates capturing groups or syntax errors, therefore we simplify it to improve coverage

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd probably help to have some more comments explaining these edge cases / hacks to future archeologists 😅 but PR looks good thanks

}

if ($expr instanceof Concat) {
Expand Down
24 changes: 12 additions & 12 deletions tests/PHPStan/Rules/Regexp/RegularExpressionPatternRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,27 +119,27 @@ public function testValidRegexPatternBefore73(): void
43,
],
[
'Regex pattern is invalid: Delimiter must not be alphanumeric or backslash in pattern: nok',
'Regex pattern is invalid: Delimiter must not be alphanumeric or backslash in pattern: nok.*',
57,
],
[
'Regex pattern is invalid: Delimiter must not be alphanumeric or backslash in pattern: nok',
'Regex pattern is invalid: Delimiter must not be alphanumeric or backslash in pattern: nok.*',
58,
],
[
'Regex pattern is invalid: Compilation failed: missing ) at offset 1 in pattern: ~(~',
'Regex pattern is invalid: Compilation failed: missing ) at offset 3 in pattern: ~(.*~',
59,
],
[
'Regex pattern is invalid: Delimiter must not be alphanumeric or backslash in pattern: noknono',
'Regex pattern is invalid: Delimiter must not be alphanumeric or backslash in pattern: nok.*nono',
61,
],
[
'Regex pattern is invalid: Delimiter must not be alphanumeric or backslash in pattern: noknope',
'Regex pattern is invalid: Delimiter must not be alphanumeric or backslash in pattern: nok.*nope',
62,
],
[
'Regex pattern is invalid: Compilation failed: missing ) at offset 1 in pattern: ~(~',
'Regex pattern is invalid: Compilation failed: missing ) at offset 3 in pattern: ~(.*~',
63,
],
],
Expand Down Expand Up @@ -249,27 +249,27 @@ public function testValidRegexPatternAfter73(): void
43,
],
[
sprintf('Regex pattern is invalid: Delimiter must not be %s in pattern: nok', $messagePart),
sprintf('Regex pattern is invalid: Delimiter must not be %s in pattern: nok.*', $messagePart),
57,
],
[
sprintf('Regex pattern is invalid: Delimiter must not be %s in pattern: nok', $messagePart),
sprintf('Regex pattern is invalid: Delimiter must not be %s in pattern: nok.*', $messagePart),
58,
],
[
'Regex pattern is invalid: Compilation failed: missing closing parenthesis at offset 1 in pattern: ~(~',
'Regex pattern is invalid: Compilation failed: missing closing parenthesis at offset 3 in pattern: ~(.*~',
59,
],
[
sprintf('Regex pattern is invalid: Delimiter must not be %s in pattern: noknono', $messagePart),
sprintf('Regex pattern is invalid: Delimiter must not be %s in pattern: nok.*nono', $messagePart),
61,
],
[
sprintf('Regex pattern is invalid: Delimiter must not be %s in pattern: noknope', $messagePart),
sprintf('Regex pattern is invalid: Delimiter must not be %s in pattern: nok.*nope', $messagePart),
62,
],
[
'Regex pattern is invalid: Compilation failed: missing closing parenthesis at offset 1 in pattern: ~(~',
'Regex pattern is invalid: Compilation failed: missing closing parenthesis at offset 3 in pattern: ~(.*~',
63,
],
],
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Regexp/data/valid-regex-pattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,11 @@ function doFoo(string $s) {
preg_replace('nok'. preg_quote($s).'nope', '');
preg_replace('~('. preg_quote($s, '~') .'~', '');
}

class Bug11403
{
public function sayHello(string $s): void
{
preg_replace('![' . preg_quote($s) . ']+!u', $s, $s);
}
}
Loading