From b6c67d5fff62b0ad64d1a15cf78c3545c3059139 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 20 Aug 2021 12:48:04 +0200 Subject: [PATCH 1/2] return non-empty-array from (mb_)?str_split() --- src/Type/Php/StrSplitFunctionReturnTypeExtension.php | 8 +++++++- tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php | 10 +++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Type/Php/StrSplitFunctionReturnTypeExtension.php b/src/Type/Php/StrSplitFunctionReturnTypeExtension.php index 0157f68db2..660dabb177 100644 --- a/src/Type/Php/StrSplitFunctionReturnTypeExtension.php +++ b/src/Type/Php/StrSplitFunctionReturnTypeExtension.php @@ -6,6 +6,7 @@ use PHPStan\Analyser\Scope; use PHPStan\Reflection\FunctionReflection; use PHPStan\Reflection\ParametersAcceptorSelector; +use PHPStan\Type\Accessory\NonEmptyArrayType; use PHPStan\Type\ArrayType; use PHPStan\Type\Constant\ConstantArrayType; use PHPStan\Type\Constant\ConstantBooleanType; @@ -13,8 +14,10 @@ use PHPStan\Type\Constant\ConstantStringType; use PHPStan\Type\DynamicFunctionReturnTypeExtension; use PHPStan\Type\IntegerType; +use PHPStan\Type\IntersectionType; use PHPStan\Type\StringType; use PHPStan\Type\Type; +use PHPStan\Type\TypeCombinator; use PHPStan\Type\TypeUtils; final class StrSplitFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension @@ -89,7 +92,10 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, $stringType = $scope->getType($functionCall->args[0]->value); if (!$stringType instanceof ConstantStringType) { - return new ArrayType(new IntegerType(), new StringType()); + return TypeCombinator::intersect( + new ArrayType(new IntegerType(), new StringType()), + new NonEmptyArrayType() + ); } $stringValue = $stringType->getValue(); diff --git a/tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php b/tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php index 27983a2c6a..e676106e23 100644 --- a/tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php @@ -5864,7 +5864,7 @@ public function dataFunctions(): array '$strSplitConstantStringWithoutDefinedSplitLength', ], [ - 'array', + 'array&nonEmpty', '$strSplitStringWithoutDefinedSplitLength', ], [ @@ -5884,7 +5884,7 @@ public function dataFunctions(): array '$strSplitConstantStringWithInvalidSplitLengthType', ], [ - 'array', + 'array&nonEmpty', '$strSplitConstantStringWithVariableStringAndConstantSplitLength', ], [ @@ -9658,7 +9658,7 @@ public function dataPhp74Functions(): array '$mbStrSplitConstantStringWithoutDefinedSplitLength', ], [ - 'array', + 'array&nonEmpty', '$mbStrSplitStringWithoutDefinedSplitLength', ], [ @@ -9678,7 +9678,7 @@ public function dataPhp74Functions(): array '$mbStrSplitConstantStringWithInvalidSplitLengthType', ], [ - 'array', + 'array&nonEmpty', '$mbStrSplitConstantStringWithVariableStringAndConstantSplitLength', ], [ @@ -9734,7 +9734,7 @@ public function dataPhp74Functions(): array '$mbStrSplitConstantStringWithInvalidSplitLengthTypeAndVariableEncoding', ], [ - 'array', + 'array&nonEmpty', '$mbStrSplitConstantStringWithVariableStringAndConstantSplitLengthAndValidEncoding', ], [ From 7549aecfc1ac19ed180ae3ad7142264896512af2 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 20 Aug 2021 12:52:21 +0200 Subject: [PATCH 2/2] fix CS --- src/Type/Php/StrSplitFunctionReturnTypeExtension.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Type/Php/StrSplitFunctionReturnTypeExtension.php b/src/Type/Php/StrSplitFunctionReturnTypeExtension.php index 660dabb177..192fd5733c 100644 --- a/src/Type/Php/StrSplitFunctionReturnTypeExtension.php +++ b/src/Type/Php/StrSplitFunctionReturnTypeExtension.php @@ -14,7 +14,6 @@ use PHPStan\Type\Constant\ConstantStringType; use PHPStan\Type\DynamicFunctionReturnTypeExtension; use PHPStan\Type\IntegerType; -use PHPStan\Type\IntersectionType; use PHPStan\Type\StringType; use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator;