Skip to content

Commit 27dd6aa

Browse files
staabmondrejmirtes
authored andcommitted
Fix count() narrowing on $matches
1 parent 0b3e74f commit 27dd6aa

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/Type/Php/RegexArrayShapeMatcher.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
114114
);
115115

116116
return TypeCombinator::union(
117-
new ConstantArrayType([new ConstantIntegerType(0)], [new StringType()]),
117+
new ConstantArrayType([new ConstantIntegerType(0)], [new StringType()], [0], [], true),
118118
$combiType,
119119
);
120120
} elseif (
@@ -156,7 +156,7 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
156156
}
157157

158158
if ($isOptionalAlternation) {
159-
$combiTypes[] = new ConstantArrayType([new ConstantIntegerType(0)], [new StringType()]);
159+
$combiTypes[] = new ConstantArrayType([new ConstantIntegerType(0)], [new StringType()], [0], [], true);
160160
}
161161

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

tests/PHPStan/Analyser/nsrt/preg_match_shapes.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,26 @@ function (string $size): void {
337337
assertType('array{0: string, 1: string, 2?: string, 3?: string}', $matches);
338338
};
339339

340+
341+
function bug11277a(string $value): void
342+
{
343+
if (preg_match('/^\[(.+,?)*\]$/', $value, $matches)) {
344+
assertType('array{0: string, 1?: string}', $matches);
345+
if (count($matches) === 2) {
346+
assertType('array{string, string}', $matches);
347+
}
348+
}
349+
}
350+
351+
function bug11277b(string $value): void
352+
{
353+
if (preg_match('/^(?:(.+,?)|(x))*$/', $value, $matches)) {
354+
assertType('array{0: string, 1?: string, 2?: string}', $matches);
355+
if (count($matches) === 2) {
356+
assertType('array{string, string}', $matches);
357+
}
358+
if (count($matches) === 3) {
359+
assertType('array{string, string, string}', $matches);
360+
}
361+
}
362+
}

0 commit comments

Comments
 (0)