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
32 changes: 24 additions & 8 deletions src/Type/Php/RegexArrayShapeMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPStan\Php\PhpVersion;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantIntegerType;
Expand Down Expand Up @@ -170,7 +171,7 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
if (!$this->containsUnmatchedAsNull($flags, $matchesAll)) {
// positive match has a subject but not any capturing group
$builder = ConstantArrayTypeBuilder::createEmpty();
$builder->setOffsetValueType(new ConstantIntegerType(0), $this->createSubjectValueType($subjectBaseType, $flags, $matchesAll));
$builder->setOffsetValueType(new ConstantIntegerType(0), $this->createSubjectValueType($subjectBaseType, $flags, $matchesAll, $wasMatched));

$combiType = TypeCombinator::union(
$builder->getArray(),
Expand Down Expand Up @@ -233,7 +234,7 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
) {
// positive match has a subject but not any capturing group
$builder = ConstantArrayTypeBuilder::createEmpty();
$builder->setOffsetValueType(new ConstantIntegerType(0), $this->createSubjectValueType($subjectBaseType, $flags, $matchesAll));
$builder->setOffsetValueType(new ConstantIntegerType(0), $this->createSubjectValueType($subjectBaseType, $flags, $matchesAll, $wasMatched));

$combiTypes[] = $builder->getArray();
}
Expand Down Expand Up @@ -273,7 +274,7 @@ private function buildArrayType(
// first item in matches contains the overall match.
$builder->setOffsetValueType(
$this->getKeyType(0),
$this->createSubjectValueType($subjectBaseType, $flags, $matchesAll),
$this->createSubjectValueType($subjectBaseType, $flags, $matchesAll, $wasMatched),
$this->isSubjectOptional($wasMatched, $matchesAll),
);

Expand Down Expand Up @@ -317,7 +318,12 @@ private function buildArrayType(
}

if ($matchesAll && $this->containsSetOrder($flags)) {
return new IntersectionType([new ArrayType(IntegerRangeType::createAllGreaterThanOrEqualTo(0), $builder->getArray()), new AccessoryArrayListType()]);
$accessoryTypes = [new ArrayType(IntegerRangeType::createAllGreaterThanOrEqualTo(0), $builder->getArray()), new AccessoryArrayListType()];
if ($wasMatched->yes()) {
$accessoryTypes[] = new NonEmptyArrayType();
}

return new IntersectionType($accessoryTypes);
}

if ($forceList) {
Expand All @@ -339,18 +345,23 @@ private function isSubjectOptional(TrinaryLogic $wasMatched, bool $matchesAll):
/**
* @param Type $baseType A string type (or string variant) representing the subject of the match
*/
private function createSubjectValueType(Type $baseType, int $flags, bool $matchesAll): Type
private function createSubjectValueType(Type $baseType, int $flags, bool $matchesAll, TrinaryLogic $wasMatched): Type
{
$subjectValueType = TypeCombinator::removeNull($this->getValueType($baseType, $flags, $matchesAll));

if ($matchesAll) {
$subjectValueType = TypeCombinator::removeNull($this->getValueType(new StringType(), $flags, $matchesAll));

if ($this->containsPatternOrder($flags)) {
$subjectValueType = new IntersectionType([
$accessoryTypes = [
new ArrayType(new IntegerType(), $subjectValueType),
new AccessoryArrayListType(),
]);
];
if ($wasMatched->yes()) {
$accessoryTypes[] = new NonEmptyArrayType();
}

$subjectValueType = new IntersectionType($accessoryTypes);
}
}

Expand Down Expand Up @@ -414,7 +425,12 @@ private function createGroupValueType(RegexCapturingGroup $captureGroup, Trinary
}

if ($this->containsPatternOrder($flags)) {
$groupValueType = new IntersectionType([new ArrayType(IntegerRangeType::createAllGreaterThanOrEqualTo(0), $groupValueType), new AccessoryArrayListType()]);
$accessoryTypes = [new ArrayType(IntegerRangeType::createAllGreaterThanOrEqualTo(0), $groupValueType), new AccessoryArrayListType()];
if ($wasMatched->yes()) {
$accessoryTypes[] = new NonEmptyArrayType();
}

$groupValueType = new IntersectionType($accessoryTypes);
}

return $groupValueType;
Expand Down
50 changes: 32 additions & 18 deletions tests/PHPStan/Analyser/nsrt/preg_match_all_shapes.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ function (string $size): void {

function (string $size): void {
if (preg_match_all('/ab(?P<num>\d+)?/', $size, $matches)) {
assertType("array{0: list<string>, num: list<''|decimal-int-string>, 1: list<''|decimal-int-string>}", $matches);
assertType("array{0: non-empty-list<string>, num: non-empty-list<''|decimal-int-string>, 1: non-empty-list<''|decimal-int-string>}", $matches);
} else {
assertType("array{}", $matches);
}
assertType("array{}|array{0: list<string>, num: list<''|decimal-int-string>, 1: list<''|decimal-int-string>}", $matches);
assertType("array{}|array{0: non-empty-list<string>, num: non-empty-list<''|decimal-int-string>, 1: non-empty-list<''|decimal-int-string>}", $matches);
};

function (string $size): void {
Expand All @@ -44,20 +44,20 @@ function (string $size): void {

function (string $size): void {
if (preg_match_all('/ab(?P<num>\d+)?/', $size, $matches) != false) {
assertType("array{0: list<string>, num: list<''|decimal-int-string>, 1: list<''|decimal-int-string>}", $matches);
assertType("array{0: non-empty-list<string>, num: non-empty-list<''|decimal-int-string>, 1: non-empty-list<''|decimal-int-string>}", $matches);
} else {
assertType("array{}", $matches);
}
assertType("array{}|array{0: list<string>, num: list<''|decimal-int-string>, 1: list<''|decimal-int-string>}", $matches);
assertType("array{}|array{0: non-empty-list<string>, num: non-empty-list<''|decimal-int-string>, 1: non-empty-list<''|decimal-int-string>}", $matches);
};

function (string $size): void {
if (preg_match_all('/ab(?P<num>\d+)?/', $size, $matches) == true) {
assertType("array{0: list<string>, num: list<''|decimal-int-string>, 1: list<''|decimal-int-string>}", $matches);
assertType("array{0: non-empty-list<string>, num: non-empty-list<''|decimal-int-string>, 1: non-empty-list<''|decimal-int-string>}", $matches);
} else {
assertType("array{}", $matches);
}
assertType("array{}|array{0: list<string>, num: list<''|decimal-int-string>, 1: list<''|decimal-int-string>}", $matches);
assertType("array{}|array{0: non-empty-list<string>, num: non-empty-list<''|decimal-int-string>, 1: non-empty-list<''|decimal-int-string>}", $matches);
};

function (string $size): void {
Expand All @@ -76,61 +76,61 @@ function (string $size): void {

function (string $size): void {
if (preg_match_all('/ab(?P<num>\d+)(?P<suffix>ab)?/', $size, $matches)) {
assertType("array{0: list<string>, num: list<decimal-int-string>, 1: list<decimal-int-string>, suffix: list<''|'ab'>, 2: list<''|'ab'>}", $matches);
assertType("array{0: non-empty-list<string>, num: non-empty-list<decimal-int-string>, 1: non-empty-list<decimal-int-string>, suffix: non-empty-list<''|'ab'>, 2: non-empty-list<''|'ab'>}", $matches);
}
};

function (string $size): void {
if (preg_match_all('/ab(?P<num>\d+)(?P<suffix>ab)?/', $size, $matches, PREG_UNMATCHED_AS_NULL)) {
assertType("array{0: list<string>, num: list<decimal-int-string>, 1: list<decimal-int-string>, suffix: list<'ab'|null>, 2: list<'ab'|null>}", $matches);
assertType("array{0: non-empty-list<string>, num: non-empty-list<decimal-int-string>, 1: non-empty-list<decimal-int-string>, suffix: non-empty-list<'ab'|null>, 2: non-empty-list<'ab'|null>}", $matches);
}
};

function (string $size): void {
if (preg_match_all('/ab(?P<num>\d+)(?P<suffix>ab)?/', $size, $matches, PREG_SET_ORDER)) {
assertType("list<array{0: string, num: decimal-int-string, 1: decimal-int-string, suffix?: 'ab', 2?: 'ab'}>", $matches);
assertType("non-empty-list<array{0: string, num: decimal-int-string, 1: decimal-int-string, suffix?: 'ab', 2?: 'ab'}>", $matches);
}
};

function (string $size): void {
if (preg_match_all('/ab(?P<num>\d+)(?P<suffix>ab)?/', $size, $matches, PREG_PATTERN_ORDER)) {
assertType("array{0: list<string>, num: list<decimal-int-string>, 1: list<decimal-int-string>, suffix: list<''|'ab'>, 2: list<''|'ab'>}", $matches);
assertType("array{0: non-empty-list<string>, num: non-empty-list<decimal-int-string>, 1: non-empty-list<decimal-int-string>, suffix: non-empty-list<''|'ab'>, 2: non-empty-list<''|'ab'>}", $matches);
}
};

function (string $size): void {
if (preg_match_all('/ab(?P<num>\d+)(?P<suffix>ab)?/', $size, $matches, PREG_UNMATCHED_AS_NULL|PREG_SET_ORDER)) {
assertType("list<array{0: string, num: decimal-int-string, 1: decimal-int-string, suffix: 'ab'|null, 2: 'ab'|null}>", $matches);
assertType("non-empty-list<array{0: string, num: decimal-int-string, 1: decimal-int-string, suffix: 'ab'|null, 2: 'ab'|null}>", $matches);
}
};

function (string $size): void {
if (preg_match_all('/ab(?P<num>\d+)(?P<suffix>ab)?/', $size, $matches, PREG_UNMATCHED_AS_NULL|PREG_PATTERN_ORDER)) {
assertType("array{0: list<string>, num: list<decimal-int-string>, 1: list<decimal-int-string>, suffix: list<'ab'|null>, 2: list<'ab'|null>}", $matches);
assertType("array{0: non-empty-list<string>, num: non-empty-list<decimal-int-string>, 1: non-empty-list<decimal-int-string>, suffix: non-empty-list<'ab'|null>, 2: non-empty-list<'ab'|null>}", $matches);
}
};

function (string $size): void {
if (preg_match_all('/ab(?P<num>\d+)(?P<suffix>ab)?/', $size, $matches, PREG_SET_ORDER|PREG_OFFSET_CAPTURE)) {
assertType("list<array{0: array{string, int<-1, max>}, num: array{decimal-int-string, int<-1, max>}, 1: array{decimal-int-string, int<-1, max>}, suffix?: array{'ab', int<-1, max>}, 2?: array{'ab', int<-1, max>}}>", $matches);
assertType("non-empty-list<array{0: array{string, int<-1, max>}, num: array{decimal-int-string, int<-1, max>}, 1: array{decimal-int-string, int<-1, max>}, suffix?: array{'ab', int<-1, max>}, 2?: array{'ab', int<-1, max>}}>", $matches);
}
};

function (string $size): void {
if (preg_match_all('/ab(?P<num>\d+)(?P<suffix>ab)?/', $size, $matches, PREG_PATTERN_ORDER|PREG_OFFSET_CAPTURE)) {
assertType("array{0: list<array{string, int<-1, max>}>, num: list<array{decimal-int-string, int<-1, max>}>, 1: list<array{decimal-int-string, int<-1, max>}>, suffix: list<array{''|'ab', int<-1, max>}>, 2: list<array{''|'ab', int<-1, max>}>}", $matches);
assertType("array{0: non-empty-list<array{string, int<-1, max>}>, num: non-empty-list<array{decimal-int-string, int<-1, max>}>, 1: non-empty-list<array{decimal-int-string, int<-1, max>}>, suffix: non-empty-list<array{''|'ab', int<-1, max>}>, 2: non-empty-list<array{''|'ab', int<-1, max>}>}", $matches);
}
};

function (string $size): void {
if (preg_match_all('/ab(?P<num>\d+)(?P<suffix>ab)?/', $size, $matches, PREG_UNMATCHED_AS_NULL|PREG_SET_ORDER|PREG_OFFSET_CAPTURE)) {
assertType("list<array{0: array{string|null, int<-1, max>}, num: array{decimal-int-string|null, int<-1, max>}, 1: array{decimal-int-string|null, int<-1, max>}, suffix: array{'ab'|null, int<-1, max>}, 2: array{'ab'|null, int<-1, max>}}>", $matches);
assertType("non-empty-list<array{0: array{string|null, int<-1, max>}, num: array{decimal-int-string|null, int<-1, max>}, 1: array{decimal-int-string|null, int<-1, max>}, suffix: array{'ab'|null, int<-1, max>}, 2: array{'ab'|null, int<-1, max>}}>", $matches);
}
};

function (string $size): void {
if (preg_match_all('/ab(?P<num>\d+)(?P<suffix>ab)?/', $size, $matches, PREG_UNMATCHED_AS_NULL|PREG_PATTERN_ORDER|PREG_OFFSET_CAPTURE)) {
assertType("array{0: list<array{string|null, int<-1, max>}>, num: list<array{decimal-int-string|null, int<-1, max>}>, 1: list<array{decimal-int-string|null, int<-1, max>}>, suffix: list<array{'ab'|null, int<-1, max>}>, 2: list<array{'ab'|null, int<-1, max>}>}", $matches);
assertType("array{0: non-empty-list<array{string|null, int<-1, max>}>, num: non-empty-list<array{decimal-int-string|null, int<-1, max>}>, 1: non-empty-list<array{decimal-int-string|null, int<-1, max>}>, suffix: non-empty-list<array{'ab'|null, int<-1, max>}>, 2: non-empty-list<array{'ab'|null, int<-1, max>}>}", $matches);
}
};

Expand Down Expand Up @@ -165,17 +165,31 @@ public function sayBar(string $content): void

function doFoobar(string $s): void {
if (preg_match_all('/(foo)?(bar)?(baz)?/', $s, $matches, PREG_OFFSET_CAPTURE)) {
assertType("array{list<array{string, int<-1, max>}>, list<array{''|'foo', int<-1, max>}>, list<array{''|'bar', int<-1, max>}>, list<array{''|'baz', int<-1, max>}>}", $matches);
assertType("array{non-empty-list<array{string, int<-1, max>}>, non-empty-list<array{''|'foo', int<-1, max>}>, non-empty-list<array{''|'bar', int<-1, max>}>, non-empty-list<array{''|'baz', int<-1, max>}>}", $matches);
}
}

function doFoobarNull(string $s): void {
if (preg_match_all('/(foo)?(bar)?(baz)?/', $s, $matches, PREG_OFFSET_CAPTURE|PREG_UNMATCHED_AS_NULL)) {
assertType("array{list<array{string|null, int<-1, max>}>, list<array{'foo'|null, int<-1, max>}>, list<array{'bar'|null, int<-1, max>}>, list<array{'baz'|null, int<-1, max>}>}", $matches);
assertType("array{non-empty-list<array{string|null, int<-1, max>}>, non-empty-list<array{'foo'|null, int<-1, max>}>, non-empty-list<array{'bar'|null, int<-1, max>}>, non-empty-list<array{'baz'|null, int<-1, max>}>}", $matches);
}
}
}

function bug14781(string $s): void {
if (preg_match_all('/(\d+)/', $s, $matches)) {
assertType('array{non-empty-list<string>, non-empty-list<decimal-int-string>}', $matches);
// accessing offset 0 is safe because the lists are non-empty
assertType('string', $matches[0][0]);
assertType('decimal-int-string', $matches[1][0]);
}

if (preg_match_all('/(\d+)/', $s, $setMatches, PREG_SET_ORDER)) {
assertType('non-empty-list<array{string, decimal-int-string}>', $setMatches);
assertType('array{string, decimal-int-string}', $setMatches[0]);
}
}

function bug11661(): void {
preg_match_all('/(ERR)?(.+)/', 'abc', $results, PREG_SET_ORDER);
assertType("list<array{string, ''|'ERR', non-empty-string}>", $results);
Expand Down
Loading