Skip to content

Commit 5f7c3d6

Browse files
authored
return non-empty-array from (mb_)?str_split()
1 parent fdf732c commit 5f7c3d6

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/Type/Php/StrSplitFunctionReturnTypeExtension.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\FunctionReflection;
88
use PHPStan\Reflection\ParametersAcceptorSelector;
9+
use PHPStan\Type\Accessory\NonEmptyArrayType;
910
use PHPStan\Type\ArrayType;
1011
use PHPStan\Type\Constant\ConstantArrayType;
1112
use PHPStan\Type\Constant\ConstantBooleanType;
@@ -15,6 +16,7 @@
1516
use PHPStan\Type\IntegerType;
1617
use PHPStan\Type\StringType;
1718
use PHPStan\Type\Type;
19+
use PHPStan\Type\TypeCombinator;
1820
use PHPStan\Type\TypeUtils;
1921

2022
final class StrSplitFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
@@ -89,7 +91,10 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
8991

9092
$stringType = $scope->getType($functionCall->args[0]->value);
9193
if (!$stringType instanceof ConstantStringType) {
92-
return new ArrayType(new IntegerType(), new StringType());
94+
return TypeCombinator::intersect(
95+
new ArrayType(new IntegerType(), new StringType()),
96+
new NonEmptyArrayType()
97+
);
9398
}
9499
$stringValue = $stringType->getValue();
95100

tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5888,7 +5888,7 @@ public function dataFunctions(): array
58885888
'$strSplitConstantStringWithoutDefinedSplitLength',
58895889
],
58905890
[
5891-
'array<int, string>',
5891+
'array<int, string>&nonEmpty',
58925892
'$strSplitStringWithoutDefinedSplitLength',
58935893
],
58945894
[
@@ -5908,7 +5908,7 @@ public function dataFunctions(): array
59085908
'$strSplitConstantStringWithInvalidSplitLengthType',
59095909
],
59105910
[
5911-
'array<int, string>',
5911+
'array<int, string>&nonEmpty',
59125912
'$strSplitConstantStringWithVariableStringAndConstantSplitLength',
59135913
],
59145914
[
@@ -9682,7 +9682,7 @@ public function dataPhp74Functions(): array
96829682
'$mbStrSplitConstantStringWithoutDefinedSplitLength',
96839683
],
96849684
[
9685-
'array<int, string>',
9685+
'array<int, string>&nonEmpty',
96869686
'$mbStrSplitStringWithoutDefinedSplitLength',
96879687
],
96889688
[
@@ -9702,7 +9702,7 @@ public function dataPhp74Functions(): array
97029702
'$mbStrSplitConstantStringWithInvalidSplitLengthType',
97039703
],
97049704
[
9705-
'array<int, string>',
9705+
'array<int, string>&nonEmpty',
97069706
'$mbStrSplitConstantStringWithVariableStringAndConstantSplitLength',
97079707
],
97089708
[
@@ -9758,7 +9758,7 @@ public function dataPhp74Functions(): array
97589758
'$mbStrSplitConstantStringWithInvalidSplitLengthTypeAndVariableEncoding',
97599759
],
97609760
[
9761-
'array<int, string>',
9761+
'array<int, string>&nonEmpty',
97629762
'$mbStrSplitConstantStringWithVariableStringAndConstantSplitLengthAndValidEncoding',
97639763
],
97649764
[

0 commit comments

Comments
 (0)