Skip to content

RegexArrayShapeMatcher - Fix optional groups with PREG_UNMATCHED_AS_NULL #3229

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

Merged
merged 5 commits into from
Jul 12, 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
11 changes: 7 additions & 4 deletions src/Type/Php/RegexArrayShapeMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,13 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
$flags ?? 0,
);

return TypeCombinator::union(
new ConstantArrayType([new ConstantIntegerType(0)], [new StringType()], [0], [], true),
$combiType,
);
if (!$this->containsUnmatchedAsNull($flags ?? 0)) {
$combiType = TypeCombinator::union(
new ConstantArrayType([new ConstantIntegerType(0)], [new StringType()], [0], [], true),
$combiType,
);
}
return $combiType;
} elseif (
$wasMatched->yes()
&& $onlyTopLevelAlternationId !== null
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-11311-php72.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ function doUnmatchedAsNull(string $s): void {
assertType('array{}|array{0: string, 1?: string, 2?: string, 3?: string}', $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?: string}', $matches);
} else {
assertType('array{}', $matches);
}
assertType('array{}|array{0: string, 1?: string}', $matches);
}
12 changes: 12 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-11311.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,15 @@ function doUnmatchedAsNull(string $s): void {
}
assertType('array{}|array{string, string|null, string|null, string|null}', $matches);
}

// see https://3v4l.org/VeDob
function unmatchedAsNullWithOptionalGroup(string $s): void {
if (preg_match('/Price: (£|€)?\d+/', $s, $matches, PREG_UNMATCHED_AS_NULL)) {
// with PREG_UNMATCHED_AS_NULL the offset 1 will always exist. It is correct that it's nullable because it's optional though
assertType('array{string, string|null}', $matches);
} else {
assertType('array{}', $matches);
}
assertType('array{}|array{string, string|null}', $matches);
}

10 changes: 10 additions & 0 deletions tests/PHPStan/Analyser/nsrt/preg_match_shapes.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,13 @@ function bug11323b(string $s): void
}
assertType('array{}|array{0: string, currency: string, 1: string}', $matches);
}

function unmatchedAsNullWithMandatoryGroup(string $s): void {
if (preg_match('/Price: (?<currency>£|€)\d+/', $s, $matches, PREG_UNMATCHED_AS_NULL)) {
assertType('array{0: string, currency: string, 1: string}', $matches);
} else {
assertType('array{}', $matches);
}
assertType('array{}|array{0: string, currency: string, 1: string}', $matches);
}

Loading