Skip to content

Commit 0b4e5c6

Browse files
committed
fix
1 parent f596950 commit 0b4e5c6

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/Type/Php/RegexArrayShapeMatcher.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -393,9 +393,9 @@ private function isGroupOptional(RegexCapturingGroup $captureGroup, TrinaryLogic
393393

394394
private function createGroupValueType(RegexCapturingGroup $captureGroup, TrinaryLogic $wasMatched, int $flags, bool $isTrailingOptional, bool $isLastGroup, bool $matchesAll): Type
395395
{
396-
$groupValueType = $this->getValueType($captureGroup->getType(), $flags, $matchesAll);
397-
398396
if ($matchesAll) {
397+
$groupValueType = $this->getValueType($captureGroup->getType(), $flags, $matchesAll);
398+
399399
if (!$isTrailingOptional && $this->containsUnmatchedAsNull($flags, $matchesAll) && !$captureGroup->isOptional()) {
400400
$groupValueType = TypeCombinator::removeNull($groupValueType);
401401
}
@@ -412,16 +412,22 @@ private function createGroupValueType(RegexCapturingGroup $captureGroup, Trinary
412412
return $groupValueType;
413413
}
414414

415+
if (!$isLastGroup && !$this->containsUnmatchedAsNull($flags, $matchesAll) && $captureGroup->isOptional()) {
416+
$groupValueType = $this->getValueType(
417+
TypeCombinator::union($captureGroup->getType(), new ConstantStringType('')),
418+
$flags,
419+
$matchesAll
420+
);
421+
} else {
422+
$groupValueType = $this->getValueType($captureGroup->getType(), $flags, $matchesAll);
423+
}
424+
415425
if ($wasMatched->yes()) {
416426
if (!$isTrailingOptional && $this->containsUnmatchedAsNull($flags, $matchesAll) && !$captureGroup->isOptional()) {
417427
$groupValueType = TypeCombinator::removeNull($groupValueType);
418428
}
419429
}
420430

421-
if (!$isLastGroup && !$this->containsUnmatchedAsNull($flags, $matchesAll) && $captureGroup->isOptional()) {
422-
$groupValueType = TypeCombinator::union($groupValueType, new ConstantStringType(''));
423-
}
424-
425431
return $groupValueType;
426432
}
427433

tests/PHPStan/Analyser/nsrt/preg_replace_callback_shapes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function (string $s): void {
2222
preg_replace_callback(
2323
'/(foo)?(bar)?(baz)?/',
2424
function ($matches) {
25-
assertType("array{0: array{string, int<0, max>}, 1?: array{'foo', int<0, max>}, 2?: array{'bar', int<0, max>}, 3?: array{'baz', int<0, max>}}", $matches);
25+
assertType("array{0: array{string, int<0, max>}, 1?: array{''|'foo', int<0, max>}, 2?: array{''|'bar', int<0, max>}, 3?: array{'baz', int<0, max>}}", $matches);
2626
return '';
2727
},
2828
$s,

0 commit comments

Comments
 (0)