Skip to content

Commit 0ba76d8

Browse files
committed
mb_str_split can return an empty array
1 parent abdddcb commit 0ba76d8

File tree

7 files changed

+23
-26
lines changed

7 files changed

+23
-26
lines changed

build/baseline-8.0.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ parameters:
1616
path: ../src/Type/Php/MbStrlenFunctionReturnTypeExtension.php
1717

1818
-
19-
message: "#^Strict comparison using \\=\\=\\= between non-empty-array<int, string> and false will always evaluate to false\\.$#"
19+
message: "#^Strict comparison using \\=\\=\\= between array<int, string> and false will always evaluate to false\\.$#"
2020
count: 1
2121
path: ../src/Type/Php/StrSplitFunctionReturnTypeExtension.php
2222

resources/functionMap_php74delta.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
'FFI::typeof' => ['FFI\CType', '&ptr'=>'FFI\CData'],
4040
'FFI::type' => ['FFI\CType', 'type'=>'string'],
4141
'get_mangled_object_vars' => ['array', 'obj'=>'object'],
42-
'mb_str_split' => ['non-empty-array<int,string>|false', 'str'=>'string', 'split_length='=>'int', 'encoding='=>'string'],
42+
'mb_str_split' => ['array<int,string>|false', 'str'=>'string', 'split_length='=>'int', 'encoding='=>'string'],
4343
'password_algos' => ['array<int, string>'],
4444
'password_hash' => ['string|false', 'password'=>'string', 'algo'=>'string|null', 'options='=>'array'],
4545
'password_needs_rehash' => ['bool', 'hash'=>'string', 'algo'=>'string|null', 'options='=>'array'],

resources/functionMap_php80delta.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
'imagescale' => ['false|object', 'im'=>'resource', 'new_width'=>'int', 'new_height='=>'int', 'method='=>'int'],
7575
'ldap_set_rebind_proc' => ['bool', 'ldap'=>'resource', 'callback'=>'?callable'],
7676
'mb_decode_numericentity' => ['string|false', 'string'=>'string', 'convmap'=>'array', 'encoding='=>'string'],
77-
'mb_str_split' => ['non-empty-array<int,string>', 'str'=>'string', 'split_length='=>'positive-int', 'encoding='=>'string'],
77+
'mb_str_split' => ['array<int,string>', 'str'=>'string', 'split_length='=>'positive-int', 'encoding='=>'string'],
7878
'mb_strlen' => ['0|positive-int', 'str'=>'string', 'encoding='=>'string'],
7979
'mktime' => ['int|false', 'hour'=>'int', 'minute='=>'int', 'second='=>'int', 'month='=>'int', 'day='=>'int', 'year='=>'int'],
8080
'odbc_exec' => ['resource|false', 'connection_id'=>'resource', 'query'=>'string'],

src/Type/Php/StrSplitFunctionReturnTypeExtension.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,18 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
9696
}
9797

9898
if (!$type instanceof ConstantStringType) {
99-
return TypeCombinator::intersect(
100-
new ArrayType(new IntegerType(), new StringType()),
101-
new NonEmptyArrayType(),
102-
);
99+
$returnType = new ArrayType(new IntegerType(), new StringType());
100+
101+
return $encoding === null
102+
? TypeCombinator::intersect($returnType, new NonEmptyArrayType())
103+
: $returnType;
103104
}
104105

105106
$stringValue = $type->getValue();
106107

107-
$items = $encoding !== null
108-
? mb_str_split($stringValue, $splitLength, $encoding)
109-
: str_split($stringValue, $splitLength);
108+
$items = $encoding === null
109+
? str_split($stringValue, $splitLength)
110+
: mb_str_split($stringValue, $splitLength, $encoding);
110111
if ($items === false) {
111112
throw new ShouldNotHappenException();
112113
}

tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8716,15 +8716,15 @@ public function dataPhp74Functions(): array
87168716
{
87178717
return [
87188718
[
8719-
PHP_VERSION_ID < 80000 ? 'non-empty-array<int, string>|false' : 'non-empty-array<int, string>',
8719+
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
87208720
'$mbStrSplitConstantStringWithoutDefinedParameters',
87218721
],
87228722
[
87238723
'array{\'a\', \'b\', \'c\', \'d\', \'e\', \'f\'}',
87248724
'$mbStrSplitConstantStringWithoutDefinedSplitLength',
87258725
],
87268726
[
8727-
'non-empty-array<int, string>',
8727+
'array<int, string>',
87288728
'$mbStrSplitStringWithoutDefinedSplitLength',
87298729
],
87308730
[
@@ -8740,15 +8740,15 @@ public function dataPhp74Functions(): array
87408740
'$mbStrSplitConstantStringWithFailureSplitLength',
87418741
],
87428742
[
8743-
PHP_VERSION_ID < 80000 ? 'non-empty-array<int, string>|false' : 'non-empty-array<int, string>',
8743+
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
87448744
'$mbStrSplitConstantStringWithInvalidSplitLengthType',
87458745
],
87468746
[
87478747
'array{\'a\'|\'g\', \'b\'|\'h\', \'c\'|\'i\', \'d\'|\'j\', \'e\'|\'k\', \'f\'|\'l\'}',
87488748
'$mbStrSplitConstantStringWithVariableStringAndConstantSplitLength',
87498749
],
87508750
[
8751-
PHP_VERSION_ID < 80000 ? 'non-empty-array<int, string>|false' : 'non-empty-array<int, string>',
8751+
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
87528752
'$mbStrSplitConstantStringWithVariableStringAndVariableSplitLength',
87538753
],
87548754
[
@@ -8760,7 +8760,7 @@ public function dataPhp74Functions(): array
87608760
'$mbStrSplitConstantStringWithOneSplitLengthAndInvalidEncoding',
87618761
],
87628762
[
8763-
PHP_VERSION_ID < 80000 ? 'non-empty-array<int, string>|false' : 'non-empty-array<int, string>',
8763+
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
87648764
'$mbStrSplitConstantStringWithOneSplitLengthAndVariableEncoding',
87658765
],
87668766
[
@@ -8772,7 +8772,7 @@ public function dataPhp74Functions(): array
87728772
'$mbStrSplitConstantStringWithGreaterSplitLengthThanStringLengthAndInvalidEncoding',
87738773
],
87748774
[
8775-
PHP_VERSION_ID < 80000 ? 'non-empty-array<int, string>|false' : 'non-empty-array<int, string>',
8775+
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
87768776
'$mbStrSplitConstantStringWithGreaterSplitLengthThanStringLengthAndVariableEncoding',
87778777
],
87788778
[
@@ -8788,15 +8788,15 @@ public function dataPhp74Functions(): array
87888788
'$mbStrSplitConstantStringWithFailureSplitLengthAndVariableEncoding',
87898789
],
87908790
[
8791-
PHP_VERSION_ID < 80000 ? 'non-empty-array<int, string>|false' : 'non-empty-array<int, string>',
8791+
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
87928792
'$mbStrSplitConstantStringWithInvalidSplitLengthTypeAndValidEncoding',
87938793
],
87948794
[
87958795
'false',
87968796
'$mbStrSplitConstantStringWithInvalidSplitLengthTypeAndInvalidEncoding',
87978797
],
87988798
[
8799-
PHP_VERSION_ID < 80000 ? 'non-empty-array<int, string>|false' : 'non-empty-array<int, string>',
8799+
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
88008800
'$mbStrSplitConstantStringWithInvalidSplitLengthTypeAndVariableEncoding',
88018801
],
88028802
[
@@ -8808,19 +8808,19 @@ public function dataPhp74Functions(): array
88088808
'$mbStrSplitConstantStringWithVariableStringAndConstantSplitLengthAndInvalidEncoding',
88098809
],
88108810
[
8811-
PHP_VERSION_ID < 80000 ? 'non-empty-array<int, string>|false' : 'non-empty-array<int, string>',
8811+
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
88128812
'$mbStrSplitConstantStringWithVariableStringAndConstantSplitLengthAndVariableEncoding',
88138813
],
88148814
[
8815-
PHP_VERSION_ID < 80000 ? 'non-empty-array<int, string>|false' : 'non-empty-array<int, string>',
8815+
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
88168816
'$mbStrSplitConstantStringWithVariableStringAndVariableSplitLengthAndValidEncoding',
88178817
],
88188818
[
88198819
'false',
88208820
'$mbStrSplitConstantStringWithVariableStringAndVariableSplitLengthAndInvalidEncoding',
88218821
],
88228822
[
8823-
PHP_VERSION_ID < 80000 ? 'non-empty-array<int, string>|false' : 'non-empty-array<int, string>',
8823+
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
88248824
'$mbStrSplitConstantStringWithVariableStringAndVariableSplitLengthAndVariableEncoding',
88258825
],
88268826
];

tests/PHPStan/Analyser/data/bug-7580.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
function x(): string { throw new \Exception(); };
1616
$v = x();
1717
assertType('string', $v);
18-
assertType('non-empty-array<int, string>', mb_str_split($v, 1));
18+
assertType('array<int, string>', mb_str_split($v, 1));

tests/PHPStan/Rules/Comparison/TernaryOperatorConstantConditionRuleTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,6 @@ public function testBug7580(): void
104104
'Ternary operator condition is always true.',
105105
9,
106106
],
107-
[
108-
'Ternary operator condition is always true.',
109-
20,
110-
],
111107
]);
112108
}
113109

0 commit comments

Comments
 (0)