diff --git a/src/Type/Php/StrSplitFunctionReturnTypeExtension.php b/src/Type/Php/StrSplitFunctionReturnTypeExtension.php index 0157f68db2..192fd5733c 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; @@ -15,6 +16,7 @@ use PHPStan\Type\IntegerType; use PHPStan\Type\StringType; use PHPStan\Type\Type; +use PHPStan\Type\TypeCombinator; use PHPStan\Type\TypeUtils; final class StrSplitFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension @@ -89,7 +91,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', ], [