Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix
  • Loading branch information
staabm committed Aug 8, 2024
commit 0b4e5c6d3932d8d4468a23f4e413d381a1bb006a
18 changes: 12 additions & 6 deletions src/Type/Php/RegexArrayShapeMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,9 @@ private function isGroupOptional(RegexCapturingGroup $captureGroup, TrinaryLogic

private function createGroupValueType(RegexCapturingGroup $captureGroup, TrinaryLogic $wasMatched, int $flags, bool $isTrailingOptional, bool $isLastGroup, bool $matchesAll): Type
{
$groupValueType = $this->getValueType($captureGroup->getType(), $flags, $matchesAll);

if ($matchesAll) {
$groupValueType = $this->getValueType($captureGroup->getType(), $flags, $matchesAll);

if (!$isTrailingOptional && $this->containsUnmatchedAsNull($flags, $matchesAll) && !$captureGroup->isOptional()) {
$groupValueType = TypeCombinator::removeNull($groupValueType);
}
Expand All @@ -412,16 +412,22 @@ private function createGroupValueType(RegexCapturingGroup $captureGroup, Trinary
return $groupValueType;
}

if (!$isLastGroup && !$this->containsUnmatchedAsNull($flags, $matchesAll) && $captureGroup->isOptional()) {
$groupValueType = $this->getValueType(
TypeCombinator::union($captureGroup->getType(), new ConstantStringType('')),
$flags,
$matchesAll
);
} else {
$groupValueType = $this->getValueType($captureGroup->getType(), $flags, $matchesAll);
}

if ($wasMatched->yes()) {
if (!$isTrailingOptional && $this->containsUnmatchedAsNull($flags, $matchesAll) && !$captureGroup->isOptional()) {
$groupValueType = TypeCombinator::removeNull($groupValueType);
}
}

if (!$isLastGroup && !$this->containsUnmatchedAsNull($flags, $matchesAll) && $captureGroup->isOptional()) {
$groupValueType = TypeCombinator::union($groupValueType, new ConstantStringType(''));
}

return $groupValueType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function (string $s): void {
preg_replace_callback(
'/(foo)?(bar)?(baz)?/',
function ($matches) {
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);
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);
return '';
},
$s,
Expand Down