Skip to content

Commit 2eecc47

Browse files
committed
RegexArrayShapeMatcher - Fix subject type for optional only-alternation case
1 parent 3576292 commit 2eecc47

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/Type/Php/RegexArrayShapeMatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
204204
}
205205

206206
if ($isOptionalAlternation && !$this->containsUnmatchedAsNull($flags ?? 0, $matchesAll)) {
207-
$combiTypes[] = new ConstantArrayType([new ConstantIntegerType(0)], [new StringType()], [0], [], true);
207+
$combiTypes[] = new ConstantArrayType([new ConstantIntegerType(0)], [$this->createSubjectValueType($flags, $matchesAll)], [0], [], true);
208208
}
209209

210210
return TypeCombinator::union(...$combiTypes);

tests/PHPStan/Analyser/nsrt/preg_match_shapes.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,3 +655,10 @@ function (string $value): void
655655
assertType("array{0: array{string, int<0, max>}, 1?: array{non-empty-string, int<0, max>}}", $matches);
656656
}
657657
};
658+
659+
function (string $value): void
660+
{
661+
if (preg_match('/^(?:(x)|(y))*$/', $value, $matches, PREG_OFFSET_CAPTURE)) {
662+
assertType("array{0: array{string, int<0, max>}, 1?: array{non-empty-string, int<0, max>}, 2?: array{non-empty-string, int<0, max>}}", $matches);
663+
}
664+
};

0 commit comments

Comments
 (0)