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
66 changes: 52 additions & 14 deletions src/Type/Php/RegexGroupParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PHPStan\Type\TypeCombinator;
use function array_key_exists;
use function count;
use function implode;
Copy link

@mvorisek mvorisek Aug 5, 2024

Choose a reason for hiding this comment

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

there is some issue - https://phpstan.org/r/78e9a062-ad8b-4a3d-a944-e671a4f17a9d - index 1 is optional, ie. the type is missing |array{string}

wider repro - https://phpstan.org/r/89288a2e-96e9-4c48-8419-3adc807bea74 - the last type is wrong too, the 1 and 2 indexes are definitely not present when index 3 is

Copy link
Contributor Author

@staabm staabm Aug 5, 2024

Choose a reason for hiding this comment

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

Index 1/2 is present but empty when 3 exists: https://3v4l.org/VgCUJ

I agree there is a constant type problem though

Copy link

Choose a reason for hiding this comment

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

the last type is wrong too, the 1 and 2 indexes are definitely not present when index 3 is

sorry, I meant empty string of course

the 2nd repro would be best fixed by "conditional types", ie. like if the inner capturing group type were dependent on the outer capturing group type

Copy link
Contributor Author

@staabm staabm Aug 6, 2024

Choose a reason for hiding this comment

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

please open issues with reproducing snippets for the known problems. there are too many open todos in already-merged PRs and I can't keep track of all of them

thank you

use function in_array;
use function is_int;
use function rtrim;
Expand Down Expand Up @@ -89,6 +90,7 @@ public function parseGroups(string $regex): ?array
$groupCombinations,
$markVerbs,
$captureOnlyNamed,
false,
);

return [$capturingGroups, $groupCombinations, $markVerbs];
Expand All @@ -111,28 +113,39 @@ private function walkRegexAst(
array &$groupCombinations,
array &$markVerbs,
bool $captureOnlyNamed,
bool $repeatedMoreThanOnce,
): void
{
$group = null;
if ($ast->getId() === '#capturing') {
$maybeConstant = !$repeatedMoreThanOnce;
if ($parentGroup !== null && $parentGroup->resetsGroupCounter()) {
$maybeConstant = false;
}

$group = new RegexCapturingGroup(
$captureGroupId++,
null,
$inAlternation ? $alternationId : null,
$inOptionalQuantification,
$parentGroup,
$this->createGroupType($ast),
$this->createGroupType($ast, $maybeConstant),
);
$parentGroup = $group;
} elseif ($ast->getId() === '#namedcapturing') {
$maybeConstant = !$repeatedMoreThanOnce;
if ($parentGroup !== null && $parentGroup->resetsGroupCounter()) {
$maybeConstant = false;
}

$name = $ast->getChild(0)->getValueValue();
$group = new RegexCapturingGroup(
$captureGroupId++,
$name,
$inAlternation ? $alternationId : null,
$inOptionalQuantification,
$parentGroup,
$this->createGroupType($ast),
$this->createGroupType($ast, $maybeConstant),
);
$parentGroup = $group;
} elseif ($ast->getId() === '#noncapturing') {
Expand All @@ -155,11 +168,15 @@ private function walkRegexAst(

$inOptionalQuantification = false;
if ($ast->getId() === '#quantification') {
[$min] = $this->getQuantificationRange($ast);
[$min, $max] = $this->getQuantificationRange($ast);

if ($min === 0) {
$inOptionalQuantification = true;
}

if ($max === null || $max > 1) {
$repeatedMoreThanOnce = true;
}
}

if ($ast->getId() === '#alternation') {
Expand Down Expand Up @@ -200,6 +217,7 @@ private function walkRegexAst(
$groupCombinations,
$markVerbs,
$captureOnlyNamed,
$repeatedMoreThanOnce,
);

if ($ast->getId() !== '#alternation') {
Expand Down Expand Up @@ -259,13 +277,18 @@ private function getQuantificationRange(TreeNode $node): array
return [$min, $max];
}

private function createGroupType(TreeNode $group): Type
private function createGroupType(TreeNode $group, bool $maybeConstant): Type
{
$isNonEmpty = TrinaryLogic::createMaybe();
$isNumeric = TrinaryLogic::createMaybe();
$inOptionalQuantification = false;
$onlyLiterals = [];

$this->walkGroupAst($group, $isNonEmpty, $isNumeric, $inOptionalQuantification, $onlyLiterals);

$this->walkGroupAst($group, $isNonEmpty, $isNumeric, $inOptionalQuantification);
if ($maybeConstant && $onlyLiterals !== null && $onlyLiterals !== []) {
return new ConstantStringType(implode('', $onlyLiterals));
}

if ($isNumeric->yes()) {
$result = new IntersectionType([new StringType(), new AccessoryNumericStringType()]);
Expand All @@ -280,7 +303,10 @@ private function createGroupType(TreeNode $group): Type
return new StringType();
}

private function walkGroupAst(TreeNode $ast, TrinaryLogic &$isNonEmpty, TrinaryLogic &$isNumeric, bool &$inOptionalQuantification): void
/**
* @param array<string>|null $onlyLiterals
*/
private function walkGroupAst(TreeNode $ast, TrinaryLogic &$isNonEmpty, TrinaryLogic &$isNumeric, bool &$inOptionalQuantification, ?array &$onlyLiterals): void
{
$children = $ast->getChildren();

Expand All @@ -289,9 +315,7 @@ private function walkGroupAst(TreeNode $ast, TrinaryLogic &$isNonEmpty, TrinaryL
&& count($children) > 0
) {
$isNonEmpty = TrinaryLogic::createYes();
}

if ($ast->getId() === '#quantification') {
} elseif ($ast->getId() === '#quantification') {
[$min] = $this->getQuantificationRange($ast);

if ($min === 0) {
Expand All @@ -301,10 +325,10 @@ private function walkGroupAst(TreeNode $ast, TrinaryLogic &$isNonEmpty, TrinaryL
$isNonEmpty = TrinaryLogic::createYes();
$inOptionalQuantification = false;
}
}

if ($ast->getId() === 'token') {
$literalValue = $this->getLiteralValue($ast);
$onlyLiterals = null;
} elseif ($ast->getId() === 'token') {
$literalValue = $this->getLiteralValue($ast, $onlyLiterals);
if ($literalValue !== null) {
if (Strings::match($literalValue, '/^\d+$/') === null) {
$isNumeric = TrinaryLogic::createNo();
Expand All @@ -315,7 +339,11 @@ private function walkGroupAst(TreeNode $ast, TrinaryLogic &$isNonEmpty, TrinaryL
if (!$inOptionalQuantification) {
$isNonEmpty = TrinaryLogic::createYes();
}
} elseif (!in_array($ast->getValueToken(), ['capturing_name'], true)) {
$onlyLiterals = null;
}
} elseif (!in_array($ast->getId(), ['#capturing', '#namedcapturing'], true)) {
$onlyLiterals = null;
}

// [^0-9] should not parse as numeric-string, and [^list-everything-but-numbers] is technically
Expand All @@ -331,11 +359,15 @@ private function walkGroupAst(TreeNode $ast, TrinaryLogic &$isNonEmpty, TrinaryL
$isNonEmpty,
$isNumeric,
$inOptionalQuantification,
$onlyLiterals,
);
}
}

private function getLiteralValue(TreeNode $node): ?string
/**
* @param array<string>|null $onlyLiterals
*/
private function getLiteralValue(TreeNode $node, ?array &$onlyLiterals): ?string
{
if ($node->getId() !== 'token') {
return null;
Expand All @@ -346,8 +378,14 @@ private function getLiteralValue(TreeNode $node): ?string
$value = $node->getValueValue();

if (in_array($token, ['literal', 'escaped_end_class'], true)) {
if (strlen($node->getValueValue()) > 1 && $value[0] === '\\') {
if (strlen($value) > 1 && $value[0] === '\\') {
return substr($value, 1);
} elseif (
$token === 'literal'
&& $onlyLiterals !== null
&& !in_array($value, ['.'], true)
) {
$onlyLiterals[] = $value;
}

return $value;
Expand Down
8 changes: 4 additions & 4 deletions tests/PHPStan/Analyser/nsrt/bug-11311-php72.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ function doFoo(string $s) {

function doUnmatchedAsNull(string $s): void {
if (preg_match('/(foo)?(bar)?(baz)?/', $s, $matches, PREG_UNMATCHED_AS_NULL)) {
assertType('array{0: string, 1?: non-empty-string, 2?: non-empty-string, 3?: non-empty-string}', $matches);
assertType("array{0: string, 1?: 'foo', 2?: 'bar', 3?: 'baz'}", $matches);
}
assertType('array{}|array{0: string, 1?: non-empty-string, 2?: non-empty-string, 3?: non-empty-string}', $matches);
assertType("array{}|array{0: string, 1?: 'foo', 2?: 'bar', 3?: 'baz'}", $matches);
}

// see https://3v4l.org/VeDob#veol
function unmatchedAsNullWithOptionalGroup(string $s): void {
if (preg_match('/Price: (£|€)?\d+/', $s, $matches, PREG_UNMATCHED_AS_NULL)) {
assertType('array{0: string, 1?: non-empty-string}', $matches);
assertType("array{0: string, 1?: non-empty-string}", $matches);
} else {
assertType('array{}', $matches);
}
assertType('array{}|array{0: string, 1?: non-empty-string}', $matches);
assertType("array{}|array{0: string, 1?: non-empty-string}", $matches);
}
8 changes: 4 additions & 4 deletions tests/PHPStan/Analyser/nsrt/bug-11311.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ function doFoo(string $s) {

function doUnmatchedAsNull(string $s): void {
if (preg_match('/(foo)?(bar)?(baz)?/', $s, $matches, PREG_UNMATCHED_AS_NULL)) {
assertType('array{string, non-empty-string|null, non-empty-string|null, non-empty-string|null}', $matches);
assertType("array{string, 'foo'|null, 'bar'|null, 'baz'|null}", $matches);
}
assertType('array{}|array{string, non-empty-string|null, non-empty-string|null, non-empty-string|null}', $matches);
assertType("array{}|array{string, 'foo'|null, 'bar'|null, 'baz'|null}", $matches);
}

// see https://3v4l.org/VeDob
Expand Down Expand Up @@ -70,13 +70,13 @@ function bug11331c(string $url):void {
class UnmatchedAsNullWithTopLevelAlternation {
function doFoo(string $s): void {
if (preg_match('/Price: (?:(£)|(€))\d+/', $s, $matches, PREG_UNMATCHED_AS_NULL)) {
assertType('array{string, non-empty-string|null, non-empty-string|null}', $matches); // could be array{0: string, 1: null, 2: non-empty-string}|array{0: string, 1: non-empty-string, 2: null}
assertType("array{string, '£'|null, '€'|null}", $matches); // could be tagged union
}
}

function doBar(string $s): void {
if (preg_match('/Price: (?:(£)|(€))?\d+/', $s, $matches, PREG_UNMATCHED_AS_NULL)) {
assertType('array{string, non-empty-string|null, non-empty-string|null}', $matches);
assertType("array{string, '£'|null, '€'|null}", $matches); // could be tagged union
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug11384.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class HelloWorld
public function sayHello(string $s): void
{
if (preg_match('{(' . Bar::VAL . ')}', $s, $m)) {
assertType('array{string, numeric-string}', $m);
assertType("array{string, '3'}", $m);
}
}
}
22 changes: 11 additions & 11 deletions tests/PHPStan/Analyser/nsrt/preg_match_all_shapes.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,65 +71,65 @@ function (string $size): void {

function (string $size): void {
preg_match_all('/a(b)(\d+)?/', $size, $matches, PREG_SET_ORDER);
assertType("list<array{0: string, 1: non-empty-string, 2?: numeric-string}>", $matches);
assertType("list<array{0: string, 1: 'b', 2?: numeric-string}>", $matches);
};

function (string $size): void {
if (preg_match_all('/ab(?P<num>\d+)(?P<suffix>ab)?/', $size, $matches)) {
assertType("array{0: list<string>, num: list<numeric-string>, 1: list<numeric-string>, suffix: list<string>, 2: list<string>}", $matches);
assertType("array{0: list<string>, num: list<numeric-string>, 1: list<numeric-string>, suffix: list<''|'ab'>, 2: list<''|'ab'>}", $matches);
}
};

function (string $size): void {
if (preg_match_all('/ab(?P<num>\d+)(?P<suffix>ab)?/', $size, $matches, PREG_UNMATCHED_AS_NULL)) {
assertType("array{0: list<string>, num: list<numeric-string>, 1: list<numeric-string>, suffix: list<non-empty-string|null>, 2: list<non-empty-string|null>}", $matches);
assertType("array{0: list<string>, num: list<numeric-string>, 1: list<numeric-string>, suffix: list<'ab'|null>, 2: list<'ab'|null>}", $matches);
}
};

function (string $size): void {
if (preg_match_all('/ab(?P<num>\d+)(?P<suffix>ab)?/', $size, $matches, PREG_SET_ORDER)) {
assertType("list<array{0: string, num: numeric-string, 1: numeric-string, suffix?: non-empty-string, 2?: non-empty-string}>", $matches);
assertType("list<array{0: string, num: numeric-string, 1: numeric-string, suffix?: 'ab', 2?: 'ab'}>", $matches);
}
};

function (string $size): void {
if (preg_match_all('/ab(?P<num>\d+)(?P<suffix>ab)?/', $size, $matches, PREG_PATTERN_ORDER)) {
assertType("array{0: list<string>, num: list<numeric-string>, 1: list<numeric-string>, suffix: list<string>, 2: list<string>}", $matches);
assertType("array{0: list<string>, num: list<numeric-string>, 1: list<numeric-string>, suffix: list<''|'ab'>, 2: list<''|'ab'>}", $matches);
}
};

function (string $size): void {
if (preg_match_all('/ab(?P<num>\d+)(?P<suffix>ab)?/', $size, $matches, PREG_UNMATCHED_AS_NULL|PREG_SET_ORDER)) {
assertType("list<array{0: string, num: numeric-string, 1: numeric-string, suffix: non-empty-string|null, 2: non-empty-string|null}>", $matches);
assertType("list<array{0: string, num: numeric-string, 1: numeric-string, suffix: 'ab'|null, 2: 'ab'|null}>", $matches);
}
};

function (string $size): void {
if (preg_match_all('/ab(?P<num>\d+)(?P<suffix>ab)?/', $size, $matches, PREG_UNMATCHED_AS_NULL|PREG_PATTERN_ORDER)) {
assertType("array{0: list<string>, num: list<numeric-string>, 1: list<numeric-string>, suffix: list<non-empty-string|null>, 2: list<non-empty-string|null>}", $matches);
assertType("array{0: list<string>, num: list<numeric-string>, 1: list<numeric-string>, suffix: list<'ab'|null>, 2: list<'ab'|null>}", $matches);
}
};

function (string $size): void {
if (preg_match_all('/ab(?P<num>\d+)(?P<suffix>ab)?/', $size, $matches, PREG_SET_ORDER|PREG_OFFSET_CAPTURE)) {
assertType("list<array{0: array{string, int<0, max>}, num: array{numeric-string, int<0, max>}, 1: array{numeric-string, int<0, max>}, suffix?: array{non-empty-string, int<0, max>}, 2?: array{non-empty-string, int<0, max>}}>", $matches);
assertType("list<array{0: array{string, int<0, max>}, num: array{numeric-string, int<0, max>}, 1: array{numeric-string, int<0, max>}, suffix?: array{'ab', int<0, max>}, 2?: array{'ab', int<0, max>}}>", $matches);
}
};

function (string $size): void {
if (preg_match_all('/ab(?P<num>\d+)(?P<suffix>ab)?/', $size, $matches, PREG_PATTERN_ORDER|PREG_OFFSET_CAPTURE)) {
assertType("array{0: list<array{string, int<0, max>}>, num: list<array{numeric-string, int<0, max>}>, 1: list<array{numeric-string, int<0, max>}>, suffix: list<''|array{non-empty-string, int<0, max>}>, 2: list<''|array{non-empty-string, int<0, max>}>}", $matches);
assertType("array{0: list<array{string, int<0, max>}>, num: list<array{numeric-string, int<0, max>}>, 1: list<array{numeric-string, int<0, max>}>, suffix: list<''|array{'ab', int<0, max>}>, 2: list<''|array{'ab', int<0, max>}>}", $matches);
}
};

function (string $size): void {
if (preg_match_all('/ab(?P<num>\d+)(?P<suffix>ab)?/', $size, $matches, PREG_UNMATCHED_AS_NULL|PREG_SET_ORDER|PREG_OFFSET_CAPTURE)) {
assertType("list<array{0: array{string|null, int<-1, max>}, num: array{numeric-string|null, int<-1, max>}, 1: array{numeric-string|null, int<-1, max>}, suffix: array{non-empty-string|null, int<-1, max>}, 2: array{non-empty-string|null, int<-1, max>}}>", $matches);
assertType("list<array{0: array{string|null, int<-1, max>}, num: array{numeric-string|null, int<-1, max>}, 1: array{numeric-string|null, int<-1, max>}, suffix: array{'ab'|null, int<-1, max>}, 2: array{'ab'|null, int<-1, max>}}>", $matches);
}
};

function (string $size): void {
if (preg_match_all('/ab(?P<num>\d+)(?P<suffix>ab)?/', $size, $matches, PREG_UNMATCHED_AS_NULL|PREG_PATTERN_ORDER|PREG_OFFSET_CAPTURE)) {
assertType("array{0: list<array{string|null, int<-1, max>}>, num: list<array{numeric-string|null, int<-1, max>}>, 1: list<array{numeric-string|null, int<-1, max>}>, suffix: list<array{non-empty-string|null, int<-1, max>}>, 2: list<array{non-empty-string|null, int<-1, max>}>}", $matches);
assertType("array{0: list<array{string|null, int<-1, max>}>, num: list<array{numeric-string|null, int<-1, max>}>, 1: list<array{numeric-string|null, int<-1, max>}>, suffix: list<array{'ab'|null, int<-1, max>}>, 2: list<array{'ab'|null, int<-1, max>}>}", $matches);
}
};
Loading
Loading