Skip to content

return non-empty-array from (mb_)?str_split() #632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 20, 2021
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
7 changes: 6 additions & 1 deletion src/Type/Php/StrSplitFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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();

Expand Down
10 changes: 5 additions & 5 deletions tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5864,7 +5864,7 @@ public function dataFunctions(): array
'$strSplitConstantStringWithoutDefinedSplitLength',
],
[
'array<int, string>',
'array<int, string>&nonEmpty',
'$strSplitStringWithoutDefinedSplitLength',
],
[
Expand All @@ -5884,7 +5884,7 @@ public function dataFunctions(): array
'$strSplitConstantStringWithInvalidSplitLengthType',
],
[
'array<int, string>',
'array<int, string>&nonEmpty',
'$strSplitConstantStringWithVariableStringAndConstantSplitLength',
],
[
Expand Down Expand Up @@ -9658,7 +9658,7 @@ public function dataPhp74Functions(): array
'$mbStrSplitConstantStringWithoutDefinedSplitLength',
],
[
'array<int, string>',
'array<int, string>&nonEmpty',
'$mbStrSplitStringWithoutDefinedSplitLength',
],
[
Expand All @@ -9678,7 +9678,7 @@ public function dataPhp74Functions(): array
'$mbStrSplitConstantStringWithInvalidSplitLengthType',
],
[
'array<int, string>',
'array<int, string>&nonEmpty',
'$mbStrSplitConstantStringWithVariableStringAndConstantSplitLength',
],
[
Expand Down Expand Up @@ -9734,7 +9734,7 @@ public function dataPhp74Functions(): array
'$mbStrSplitConstantStringWithInvalidSplitLengthTypeAndVariableEncoding',
],
[
'array<int, string>',
'array<int, string>&nonEmpty',
'$mbStrSplitConstantStringWithVariableStringAndConstantSplitLengthAndValidEncoding',
],
[
Expand Down