Skip to content

Commit

Permalink
mb_str_split can return an empty array
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm authored and ondrejmirtes committed Jul 10, 2022
1 parent 0c457e0 commit ef19bb7
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 26 deletions.
2 changes: 1 addition & 1 deletion build/baseline-8.0.neon
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ parameters:
path: ../src/Type/Php/MbStrlenFunctionReturnTypeExtension.php

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

Expand Down
2 changes: 1 addition & 1 deletion resources/functionMap_php74delta.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
'FFI::typeof' => ['FFI\CType', '&ptr'=>'FFI\CData'],
'FFI::type' => ['FFI\CType', 'type'=>'string'],
'get_mangled_object_vars' => ['array', 'obj'=>'object'],
'mb_str_split' => ['non-empty-array<int,string>|false', 'str'=>'string', 'split_length='=>'int', 'encoding='=>'string'],
'mb_str_split' => ['array<int,string>|false', 'str'=>'string', 'split_length='=>'int', 'encoding='=>'string'],
'password_algos' => ['array<int, string>'],
'password_hash' => ['string|false', 'password'=>'string', 'algo'=>'string|null', 'options='=>'array'],
'password_needs_rehash' => ['bool', 'hash'=>'string', 'algo'=>'string|null', 'options='=>'array'],
Expand Down
2 changes: 1 addition & 1 deletion resources/functionMap_php80delta.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
'imagescale' => ['false|object', 'im'=>'resource', 'new_width'=>'int', 'new_height='=>'int', 'method='=>'int'],
'ldap_set_rebind_proc' => ['bool', 'ldap'=>'resource', 'callback'=>'?callable'],
'mb_decode_numericentity' => ['string|false', 'string'=>'string', 'convmap'=>'array', 'encoding='=>'string'],
'mb_str_split' => ['non-empty-array<int,string>', 'str'=>'string', 'split_length='=>'positive-int', 'encoding='=>'string'],
'mb_str_split' => ['array<int,string>', 'str'=>'string', 'split_length='=>'positive-int', 'encoding='=>'string'],
'mb_strlen' => ['0|positive-int', 'str'=>'string', 'encoding='=>'string'],
'mktime' => ['int|false', 'hour'=>'int', 'minute='=>'int', 'second='=>'int', 'month='=>'int', 'day='=>'int', 'year='=>'int'],
'odbc_exec' => ['resource|false', 'connection_id'=>'resource', 'query'=>'string'],
Expand Down
15 changes: 8 additions & 7 deletions src/Type/Php/StrSplitFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,18 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
}

if (!$type instanceof ConstantStringType) {
return TypeCombinator::intersect(
new ArrayType(new IntegerType(), new StringType()),
new NonEmptyArrayType(),
);
$returnType = new ArrayType(new IntegerType(), new StringType());

return $encoding === null
? TypeCombinator::intersect($returnType, new NonEmptyArrayType())
: $returnType;
}

$stringValue = $type->getValue();

$items = $encoding !== null
? mb_str_split($stringValue, $splitLength, $encoding)
: str_split($stringValue, $splitLength);
$items = $encoding === null
? str_split($stringValue, $splitLength)
: mb_str_split($stringValue, $splitLength, $encoding);
if ($items === false) {
throw new ShouldNotHappenException();
}
Expand Down
22 changes: 11 additions & 11 deletions tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8716,15 +8716,15 @@ public function dataPhp74Functions(): array
{
return [
[
PHP_VERSION_ID < 80000 ? 'non-empty-array<int, string>|false' : 'non-empty-array<int, string>',
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
'$mbStrSplitConstantStringWithoutDefinedParameters',
],
[
'array{\'a\', \'b\', \'c\', \'d\', \'e\', \'f\'}',
'$mbStrSplitConstantStringWithoutDefinedSplitLength',
],
[
'non-empty-array<int, string>',
'array<int, string>',
'$mbStrSplitStringWithoutDefinedSplitLength',
],
[
Expand All @@ -8740,15 +8740,15 @@ public function dataPhp74Functions(): array
'$mbStrSplitConstantStringWithFailureSplitLength',
],
[
PHP_VERSION_ID < 80000 ? 'non-empty-array<int, string>|false' : 'non-empty-array<int, string>',
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
'$mbStrSplitConstantStringWithInvalidSplitLengthType',
],
[
'array{\'a\'|\'g\', \'b\'|\'h\', \'c\'|\'i\', \'d\'|\'j\', \'e\'|\'k\', \'f\'|\'l\'}',
'$mbStrSplitConstantStringWithVariableStringAndConstantSplitLength',
],
[
PHP_VERSION_ID < 80000 ? 'non-empty-array<int, string>|false' : 'non-empty-array<int, string>',
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
'$mbStrSplitConstantStringWithVariableStringAndVariableSplitLength',
],
[
Expand All @@ -8760,7 +8760,7 @@ public function dataPhp74Functions(): array
'$mbStrSplitConstantStringWithOneSplitLengthAndInvalidEncoding',
],
[
PHP_VERSION_ID < 80000 ? 'non-empty-array<int, string>|false' : 'non-empty-array<int, string>',
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
'$mbStrSplitConstantStringWithOneSplitLengthAndVariableEncoding',
],
[
Expand All @@ -8772,7 +8772,7 @@ public function dataPhp74Functions(): array
'$mbStrSplitConstantStringWithGreaterSplitLengthThanStringLengthAndInvalidEncoding',
],
[
PHP_VERSION_ID < 80000 ? 'non-empty-array<int, string>|false' : 'non-empty-array<int, string>',
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
'$mbStrSplitConstantStringWithGreaterSplitLengthThanStringLengthAndVariableEncoding',
],
[
Expand All @@ -8788,15 +8788,15 @@ public function dataPhp74Functions(): array
'$mbStrSplitConstantStringWithFailureSplitLengthAndVariableEncoding',
],
[
PHP_VERSION_ID < 80000 ? 'non-empty-array<int, string>|false' : 'non-empty-array<int, string>',
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
'$mbStrSplitConstantStringWithInvalidSplitLengthTypeAndValidEncoding',
],
[
'false',
'$mbStrSplitConstantStringWithInvalidSplitLengthTypeAndInvalidEncoding',
],
[
PHP_VERSION_ID < 80000 ? 'non-empty-array<int, string>|false' : 'non-empty-array<int, string>',
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
'$mbStrSplitConstantStringWithInvalidSplitLengthTypeAndVariableEncoding',
],
[
Expand All @@ -8808,19 +8808,19 @@ public function dataPhp74Functions(): array
'$mbStrSplitConstantStringWithVariableStringAndConstantSplitLengthAndInvalidEncoding',
],
[
PHP_VERSION_ID < 80000 ? 'non-empty-array<int, string>|false' : 'non-empty-array<int, string>',
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
'$mbStrSplitConstantStringWithVariableStringAndConstantSplitLengthAndVariableEncoding',
],
[
PHP_VERSION_ID < 80000 ? 'non-empty-array<int, string>|false' : 'non-empty-array<int, string>',
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
'$mbStrSplitConstantStringWithVariableStringAndVariableSplitLengthAndValidEncoding',
],
[
'false',
'$mbStrSplitConstantStringWithVariableStringAndVariableSplitLengthAndInvalidEncoding',
],
[
PHP_VERSION_ID < 80000 ? 'non-empty-array<int, string>|false' : 'non-empty-array<int, string>',
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
'$mbStrSplitConstantStringWithVariableStringAndVariableSplitLengthAndVariableEncoding',
],
];
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/data/bug-7580.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
function x(): string { throw new \Exception(); };
$v = x();
assertType('string', $v);
assertType('non-empty-array<int, string>', mb_str_split($v, 1));
assertType('array<int, string>', mb_str_split($v, 1));
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@ public function testBug7580(): void
'Ternary operator condition is always true.',
9,
],
[
'Ternary operator condition is always true.',
20,
],
]);
}

Expand Down

0 comments on commit ef19bb7

Please sign in to comment.