Skip to content

mb_str_split() / str_split() always returns non-empty-array #633

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 6 commits into from
Aug 23, 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
2 changes: 1 addition & 1 deletion build/baseline-8.0.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ parameters:
path: ../src/Type/Php/MbFunctionsReturnTypeExtension.php

-
message: "#^Call to function is_array\\(\\) with array\\<int, string\\> will always evaluate to true\\.$#"
message: "#^Strict comparison using \\=\\=\\= between array<int, string>&nonEmpty 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.php
Original file line number Diff line number Diff line change
Expand Up @@ -11696,7 +11696,7 @@
'str_replace' => ['string|array', 'search'=>'string|array', 'replace'=>'string|array', 'subject'=>'string|array', '&w_replace_count='=>'int'],
'str_rot13' => ['string', 'str'=>'string'],
'str_shuffle' => ['string', 'str'=>'string'],
'str_split' => ['array<int,string>|false', 'str'=>'string', 'split_length='=>'int'],
'str_split' => ['non-empty-array<int,string>|false', 'str'=>'string', 'split_length='=>'int'],
'str_word_count' => ['array<int,string>|int|false', 'string'=>'string', 'format='=>'int', 'charlist='=>'string'],
'strcasecmp' => ['int', 'str1'=>'string', 'str2'=>'string'],
'strchr' => ['string|false', 'haystack'=>'string', 'needle'=>'string', 'before_needle='=>'bool'],
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' => ['array<int,string>|false', 'str'=>'string', 'split_length='=>'int', 'encoding='=>'string'],
'mb_str_split' => ['non-empty-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
4 changes: 2 additions & 2 deletions resources/functionMap_php80delta.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
'imagerotate' => ['false|object', 'src_im'=>'resource', 'angle'=>'float', 'bgdcolor'=>'int', 'ignoretransparent='=>'int'],
'imagescale' => ['false|object', 'im'=>'resource', 'new_width'=>'int', 'new_height='=>'int', 'method='=>'int'],
'mb_decode_numericentity' => ['string|false', 'string'=>'string', 'convmap'=>'array', 'encoding='=>'string'],
'mb_str_split' => ['array<int,string>', 'str'=>'string', 'split_length='=>'positive-int', 'encoding='=>'string'],
'mb_str_split' => ['non-empty-array<int,string>', 'str'=>'string', 'split_length='=>'positive-int', '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'],
'parse_str' => ['void', 'encoded_string'=>'string', '&w_result'=>'array'],
Expand All @@ -84,7 +84,7 @@
'socket_select' => ['int|false', '&rw_read'=>'Socket[]|null', '&rw_write'=>'Socket[]|null', '&rw_except'=>'Socket[]|null', 'seconds'=>'int|null', 'microseconds='=>'int'],
'sodium_crypto_aead_chacha20poly1305_ietf_decrypt' => ['string|false', 'confidential_message'=>'string', 'public_message'=>'string', 'nonce'=>'string', 'key'=>'string'],
'str_contains' => ['bool', 'haystack'=>'string', 'needle'=>'string'],
'str_split' => ['array<int,string>', 'str'=>'string', 'split_length='=>'positive-int'],
'str_split' => ['non-empty-array<int,string>', 'str'=>'string', 'split_length='=>'positive-int'],
'str_ends_with' => ['bool', 'haystack'=>'string', 'needle'=>'string'],
'str_starts_with' => ['bool', 'haystack'=>'string', 'needle'=>'string'],
'strchr' => ['string|false', 'haystack'=>'string', 'needle'=>'string', 'before_needle='=>'bool'],
Expand Down
2 changes: 1 addition & 1 deletion src/Type/Php/StrSplitFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
$items = isset($encoding)
? mb_str_split($stringValue, $splitLength, $encoding)
: str_split($stringValue, $splitLength);
if (!is_array($items)) {
if ($items === false) {
throw new \PHPStan\ShouldNotHappenException();
}

Expand Down
26 changes: 13 additions & 13 deletions tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5880,7 +5880,7 @@ public function dataFunctions(): array
'$gettimeofdayBenevolent',
],
[
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
PHP_VERSION_ID < 80000 ? '(array<int, string>&nonEmpty)|false' : 'array<int, string>&nonEmpty',
'$strSplitConstantStringWithoutDefinedParameters',
],
[
Expand All @@ -5904,15 +5904,15 @@ public function dataFunctions(): array
'$strSplitConstantStringWithFailureSplitLength',
],
[
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
PHP_VERSION_ID < 80000 ? '(array<int, string>&nonEmpty)|false' : 'array<int, string>&nonEmpty',
'$strSplitConstantStringWithInvalidSplitLengthType',
],
[
'array<int, string>&nonEmpty',
'$strSplitConstantStringWithVariableStringAndConstantSplitLength',
],
[
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
PHP_VERSION_ID < 80000 ? '(array<int, string>&nonEmpty)|false' : 'array<int, string>&nonEmpty',
Copy link
Contributor Author

@staabm staabm Aug 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interessting.. sometimes a type including braces is produced, and sometimes without

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$joinTypes = static function (array $types) use ($level): string {
$typeNames = [];
foreach ($types as $type) {
if ($type instanceof ClosureType || $type instanceof CallableType) {
$typeNames[] = sprintf('(%s)', $type->describe($level));
} elseif ($type instanceof IntersectionType) {
$intersectionDescription = $type->describe($level);
if (strpos($intersectionDescription, '&') !== false) {
$typeNames[] = sprintf('(%s)', $type->describe($level));
} else {
$typeNames[] = $intersectionDescription;
}
} else {
$typeNames[] = $type->describe($level);
}
}
return implode('|', $typeNames);
};

'$strSplitConstantStringWithVariableStringAndVariableSplitLength',
],
// parse_url
Expand Down Expand Up @@ -9674,7 +9674,7 @@ public function dataPhp74Functions(): array
{
return [
[
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
PHP_VERSION_ID < 80000 ? '(array<int, string>&nonEmpty)|false' : 'array<int, string>&nonEmpty',
'$mbStrSplitConstantStringWithoutDefinedParameters',
],
[
Expand All @@ -9698,15 +9698,15 @@ public function dataPhp74Functions(): array
'$mbStrSplitConstantStringWithFailureSplitLength',
],
[
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
PHP_VERSION_ID < 80000 ? '(array<int, string>&nonEmpty)|false' : 'array<int, string>&nonEmpty',
'$mbStrSplitConstantStringWithInvalidSplitLengthType',
],
[
'array<int, string>&nonEmpty',
'$mbStrSplitConstantStringWithVariableStringAndConstantSplitLength',
],
[
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
PHP_VERSION_ID < 80000 ? '(array<int, string>&nonEmpty)|false' : 'array<int, string>&nonEmpty',
'$mbStrSplitConstantStringWithVariableStringAndVariableSplitLength',
],
[
Expand All @@ -9718,7 +9718,7 @@ public function dataPhp74Functions(): array
'$mbStrSplitConstantStringWithOneSplitLengthAndInvalidEncoding',
],
[
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
PHP_VERSION_ID < 80000 ? '(array<int, string>&nonEmpty)|false' : 'array<int, string>&nonEmpty',
'$mbStrSplitConstantStringWithOneSplitLengthAndVariableEncoding',
],
[
Expand All @@ -9730,7 +9730,7 @@ public function dataPhp74Functions(): array
'$mbStrSplitConstantStringWithGreaterSplitLengthThanStringLengthAndInvalidEncoding',
],
[
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
PHP_VERSION_ID < 80000 ? '(array<int, string>&nonEmpty)|false' : 'array<int, string>&nonEmpty',
'$mbStrSplitConstantStringWithGreaterSplitLengthThanStringLengthAndVariableEncoding',
],
[
Expand All @@ -9746,15 +9746,15 @@ public function dataPhp74Functions(): array
'$mbStrSplitConstantStringWithFailureSplitLengthAndVariableEncoding',
],
[
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
PHP_VERSION_ID < 80000 ? '(array<int, string>&nonEmpty)|false' : 'array<int, string>&nonEmpty',
'$mbStrSplitConstantStringWithInvalidSplitLengthTypeAndValidEncoding',
],
[
'false',
'$mbStrSplitConstantStringWithInvalidSplitLengthTypeAndInvalidEncoding',
],
[
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
PHP_VERSION_ID < 80000 ? '(array<int, string>&nonEmpty)|false' : 'array<int, string>&nonEmpty',
'$mbStrSplitConstantStringWithInvalidSplitLengthTypeAndVariableEncoding',
],
[
Expand All @@ -9766,19 +9766,19 @@ public function dataPhp74Functions(): array
'$mbStrSplitConstantStringWithVariableStringAndConstantSplitLengthAndInvalidEncoding',
],
[
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
PHP_VERSION_ID < 80000 ? '(array<int, string>&nonEmpty)|false' : 'array<int, string>&nonEmpty',
'$mbStrSplitConstantStringWithVariableStringAndConstantSplitLengthAndVariableEncoding',
],
[
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
PHP_VERSION_ID < 80000 ? '(array<int, string>&nonEmpty)|false' : 'array<int, string>&nonEmpty',
'$mbStrSplitConstantStringWithVariableStringAndVariableSplitLengthAndValidEncoding',
],
[
'false',
'$mbStrSplitConstantStringWithVariableStringAndVariableSplitLengthAndInvalidEncoding',
],
[
PHP_VERSION_ID < 80000 ? 'array<int, string>|false' : 'array<int, string>',
PHP_VERSION_ID < 80000 ? '(array<int, string>&nonEmpty)|false' : 'array<int, string>&nonEmpty',
'$mbStrSplitConstantStringWithVariableStringAndVariableSplitLengthAndVariableEncoding',
],
];
Expand Down