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
9 changes: 7 additions & 2 deletions src/Type/Php/RegexArrayShapeMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ private function createSubjectValueType(TrinaryLogic $wasMatched, int $flags, bo
$subjectValueType = TypeCombinator::removeNull($this->getValueType(new StringType(), $flags, $matchesAll));

if ($matchesAll) {
if (!$wasMatched->yes()) {
if (!$wasMatched->yes() && !$this->containsOffsetCapture($flags)) {
$subjectValueType = TypeCombinator::union($subjectValueType, new ConstantStringType(''));
}
if ($this->containsPatternOrder($flags)) {
Expand Down Expand Up @@ -421,6 +421,11 @@ private function createGroupValueType(RegexCapturingGroup $captureGroup, Trinary
return $groupValueType;
}

private function containsOffsetCapture(int $flags): bool
{
return ($flags & PREG_OFFSET_CAPTURE) !== 0;
}

private function containsPatternOrder(int $flags): bool
{
// If no order flag is given, PREG_PATTERN_ORDER is assumed.
Expand Down Expand Up @@ -463,7 +468,7 @@ private function getValueType(Type $baseType, int $flags, bool $matchesAll): Typ
$offsetType = IntegerRangeType::fromInterval(-1, null);
}

if (($flags & PREG_OFFSET_CAPTURE) !== 0) {
if ($this->containsOffsetCapture($flags)) {
$builder = ConstantArrayTypeBuilder::createEmpty();

$builder->setOffsetValueType(
Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Analyser/nsrt/preg_match_all_shapes.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,15 @@ function (string $size): void {
assertType("array{0: list<array{string|null, int<-1, max>}>, num: list<array{numeric-string|null, int<-1, max>}>, 1: list<array{numeric-string|null, int<-1, max>}>, suffix: list<array{'ab'|null, int<-1, max>}>, 2: list<array{'ab'|null, int<-1, max>}>}", $matches);
}
};

class Bug11457
{
public function sayHello(string $content): void
{
if (preg_match_all("~text=~mU", $content, $matches, PREG_OFFSET_CAPTURE) === 0) {
return;
}

assertType('array{list<array{string, int<0, max>}>}', $matches);
}
}
Loading