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
4 changes: 4 additions & 0 deletions src/Type/Php/RegexArrayShapeMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ private function getOnlyTopLevelAlternation(array $captureGroups): ?RegexAlterna
return null;
}

if ($captureGroup->inOptionalQuantification()) {
return null;
}

if ($alternation === null) {
$alternation = $captureGroup->getAlternation();
} elseif ($alternation->getId() !== $captureGroup->getAlternation()->getId()) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Regex/RegexCapturingGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public function isOptional(): bool
|| $this->parent !== null && $this->parent->isOptional();
}

public function inOptionalQuantification(): bool
{
return $this->inOptionalQuantification;
}

public function inOptionalAlternation(): bool
{
if (!$this->inAlternation()) {
Expand Down
15 changes: 15 additions & 0 deletions tests/PHPStan/Analyser/nsrt/preg_match_shapes.php
Original file line number Diff line number Diff line change
Expand Up @@ -753,3 +753,18 @@ function bug11622 (string $expression): void {
assertType("array{string, string}", $matches);
}
}

function bug11604 (string $string): void {
if (! preg_match('/(XX)|(YY)?ZZ/', $string, $matches)) {
return;
}

assertType("array{0: string, 1?: ''|'XX', 2?: 'YY'}", $matches);
// could be array{string, '', 'YY'}|array{string, 'XX'}|array{string}
}

function bug11604b (string $string): void {
if (preg_match('/(XX)|(YY)?(ZZ)/', $string, $matches)) {
assertType("array{0: string, 1?: ''|'XX', 2?: ''|'YY', 3?: 'ZZ'}", $matches);
}
}
Loading