From 30650e627dad1f3a72b44eb4e45501a446a4a04d Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Thu, 1 Aug 2024 23:42:43 +0200 Subject: [PATCH] `Strings::matchAll()` cannot return `null` --- .../Nette/StringsMatchAllDynamicReturnTypeExtension.php | 9 +-------- tests/Type/Nette/data/strings-match.php | 4 ++-- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/src/Type/Nette/StringsMatchAllDynamicReturnTypeExtension.php b/src/Type/Nette/StringsMatchAllDynamicReturnTypeExtension.php index 35fc240..9ca2f94 100644 --- a/src/Type/Nette/StringsMatchAllDynamicReturnTypeExtension.php +++ b/src/Type/Nette/StringsMatchAllDynamicReturnTypeExtension.php @@ -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 { @@ -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); } } diff --git a/tests/Type/Nette/data/strings-match.php b/tests/Type/Nette/data/strings-match.php index 2449ccd..4c49f28 100644 --- a/tests/Type/Nette/data/strings-match.php +++ b/tests/Type/Nette/data/strings-match.php @@ -26,10 +26,10 @@ function (string $s): void { function (string $s): void { $result = Strings::matchAll($s, '/ab(?P\d+)(?Pab)?/', PREG_SET_ORDER); - assertType("list|null", $result); + assertType("list", $result); }; function (string $s): void { $result = Strings::matchAll($s, '/ab(?P\d+)(?Pab)?/', PREG_PATTERN_ORDER); - assertType("array{0: list, num: list, 1: list, suffix: list, 2: list}|null", $result); + assertType("array{0: list, num: list, 1: list, suffix: list, 2: list}", $result); };