Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor RegexArrayShapeMatcher #3248

Merged
merged 1 commit into from
Jul 16, 2024
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
50 changes: 42 additions & 8 deletions src/Type/Php/RegexArrayShapeMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\Php\PhpVersion;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
Expand All @@ -24,8 +25,9 @@
use function array_reverse;
use function count;
use function in_array;
use function is_int;
use function is_string;
use function str_contains;
use function sscanf;
use const PREG_OFFSET_CAPTURE;
use const PREG_UNMATCHED_AS_NULL;

Expand Down Expand Up @@ -450,14 +452,9 @@ private function walkRegexAst(

$inOptionalQuantification = false;
if ($ast->getId() === '#quantification') {
$lastChild = $ast->getChild($ast->getChildrenNumber() - 1);
$value = $lastChild->getValue();
[$min] = $this->getQuantificationRange($ast);

if ($value['token'] === 'n_to_m' && str_contains($value['value'], '{0,')) {
$inOptionalQuantification = true;
} elseif ($value['token'] === 'zero_or_one') {
$inOptionalQuantification = true;
} elseif ($value['token'] === 'zero_or_more') {
if ($min === 0) {
$inOptionalQuantification = true;
}
}
Expand Down Expand Up @@ -500,6 +497,43 @@ private function walkRegexAst(
}
}

/** @return array{?int, ?int} */
private function getQuantificationRange(TreeNode $node): array
{
if ($node->getId() !== '#quantification') {
throw new ShouldNotHappenException();
}

$min = null;
$max = null;

$lastChild = $node->getChild($node->getChildrenNumber() - 1);
$value = $lastChild->getValue();

if ($value['token'] === 'n_to_m') {
if (sscanf($value['value'], '{%d,%d}', $n, $m) !== 2 || !is_int($n) || !is_int($m)) {
throw new ShouldNotHappenException();
}

$min = $n;
$max = $m;
} elseif ($value['token'] === 'exactly_n') {
if (sscanf($value['value'], '{%d}', $n) !== 1 || !is_int($n)) {
throw new ShouldNotHappenException();
}

$min = $n;
$max = $n;
} elseif ($value['token'] === 'zero_or_one') {
$min = 0;
$max = 1;
} elseif ($value['token'] === 'zero_or_more') {
$min = 0;
}

return [$min, $max];
}

private function getPatternType(Expr $patternExpr, Scope $scope): Type
{
if ($patternExpr instanceof Expr\BinaryOp\Concat) {
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Analyser/nsrt/preg_match_shapes.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ function doMatch(string $s): void {
assertType('array{string, string, string, string}', $matches);
}
assertType('array{}|array{string, string, string, string}', $matches);

if (preg_match('/(foo)(bar)(baz){2}/', $s, $matches)) {
assertType('array{string, string, string, string}', $matches);
}
assertType('array{}|array{string, string, string, string}', $matches);
}

function doNonCapturingGroup(string $s): void {
Expand Down
Loading