Skip to content
Open
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
37 changes: 34 additions & 3 deletions src/Rules/Regexp/RegularExpressionQuotingRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Rules\Regexp;

use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
Expand Down Expand Up @@ -76,12 +77,42 @@ public function processNode(Node $node, Scope $scope): array
if (!isset($normalizedArgs[0])) {
return [];
}
if (!$normalizedArgs[0]->value instanceof Concat) {

$patternArg = $normalizedArgs[0]->value;

// preg_replace, preg_replace_callback and preg_filter also take an array
// of patterns, which RegularExpressionPatternRule::extractPatterns()
// already reads. Each element is a pattern in its own right.
if ($patternArg instanceof Array_) {
if (
!in_array($functionReflection->getName(), [
'preg_filter',
'preg_replace',
'preg_replace_callback',
], true)
) {
return [];
}

$errors = [];
foreach ($patternArg->items as $item) {
if (!$item->value instanceof Concat) {
continue;
}

$itemDelimiters = $this->regexExpressionHelper->getPatternDelimiters($item->value, $scope);
$errors = array_merge($errors, $this->validateQuoteDelimiters($item->value, $scope, $itemDelimiters));
}

return $errors;
}

if (!$patternArg instanceof Concat) {
return [];
}

$patternDelimiters = $this->regexExpressionHelper->getPatternDelimiters($normalizedArgs[0]->value, $scope);
return $this->validateQuoteDelimiters($normalizedArgs[0]->value, $scope, $patternDelimiters);
$patternDelimiters = $this->regexExpressionHelper->getPatternDelimiters($patternArg, $scope);
return $this->validateQuoteDelimiters($patternArg, $scope, $patternDelimiters);
Comment on lines +97 to +115

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this could be de-duplicated by collecting all concats in a array first and then do the processing

}

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Regexp/RegularExpressionQuotingRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ public function testRule(): void
'Call to preg_quote() is missing delimiter parameter to be effective.',
77,
],
[
'Call to preg_quote() uses invalid delimiter / while pattern uses &.',
102,
],
[
'Call to preg_quote() uses invalid delimiter / while pattern uses &.',
103,
],
[
'Call to preg_quote() uses invalid delimiter / while pattern uses &.',
104,
],
[
'Call to preg_quote() uses invalid delimiter / while pattern uses &.',
105,
],
],
);
}
Expand Down
13 changes: 13 additions & 0 deletions tests/PHPStan/Rules/Regexp/data/preg-quote.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,16 @@ function ok(string $s): void { // ok
function notAnalyzable(string $s): void { // ok
preg_match($s. preg_quote('&oops') . 'pattern}', $s);
}

// preg_replace, preg_replace_callback and preg_filter accept an array of
// patterns. RegularExpressionPatternRule already reads that form.
function doFooArrayPatterns(string $s, callable $cb): void // errors
{
preg_replace(['&' . preg_quote('&oops', '/') . 'pattern&'], 'x', $s);
preg_filter(['&' . preg_quote('&oops', '/') . 'pattern&'], 'x', $s);
preg_replace_callback(['&' . preg_quote('&oops', '/') . 'pattern&'], $cb, $s);
preg_replace(['key' => '&' . preg_quote('&oops', '/') . 'pattern&'], 'x', $s);

// no error: delimiter matches
preg_replace(['&' . preg_quote('&oops', '&') . 'pattern&'], 'x', $s);
}
Loading