Skip to content

Commit

Permalink
Strings::matchAll() cannot return null
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 1, 2024
1 parent 14a68a9 commit 30650e6
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 10 deletions.
9 changes: 1 addition & 8 deletions src/Type/Nette/StringsMatchAllDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
use PHPStan\Reflection\MethodReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
use PHPStan\Type\NullType;
use PHPStan\Type\Php\RegexArrayShapeMatcher;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;

class StringsMatchAllDynamicReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension
{
Expand Down Expand Up @@ -48,12 +46,7 @@ public function getTypeFromStaticMethodCall(MethodReflection $methodReflection,
$flagsType = $scope->getType($flagsArg->value);
}

$arrayShape = $this->regexArrayShapeMatcher->matchAllExpr($patternArg->value, $flagsType, TrinaryLogic::createYes(), $scope);
if ($arrayShape === null) {
return null;
}

return TypeCombinator::union($arrayShape, new NullType());
return $this->regexArrayShapeMatcher->matchAllExpr($patternArg->value, $flagsType, TrinaryLogic::createYes(), $scope);
}

}
4 changes: 2 additions & 2 deletions tests/Type/Nette/data/strings-match.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ function (string $s): void {

function (string $s): void {
$result = Strings::matchAll($s, '/ab(?P<num>\d+)(?P<suffix>ab)?/', PREG_SET_ORDER);
assertType("list<array{0: string, num: numeric-string, 1: numeric-string, suffix?: non-empty-string, 2?: non-empty-string}>|null", $result);
assertType("list<array{0: string, num: numeric-string, 1: numeric-string, suffix?: non-empty-string, 2?: non-empty-string}>", $result);
};

function (string $s): void {
$result = Strings::matchAll($s, '/ab(?P<num>\d+)(?P<suffix>ab)?/', PREG_PATTERN_ORDER);
assertType("array{0: list<string>, num: list<numeric-string>, 1: list<numeric-string>, suffix: list<string>, 2: list<string>}|null", $result);
assertType("array{0: list<string>, num: list<numeric-string>, 1: list<numeric-string>, suffix: list<string>, 2: list<string>}", $result);
};

0 comments on commit 30650e6

Please sign in to comment.