Skip to content

mb_str_split false return #4183

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

Closed
GrahamCampbell opened this issue Dec 3, 2020 · 7 comments
Closed

mb_str_split false return #4183

GrahamCampbell opened this issue Dec 3, 2020 · 7 comments

Comments

@GrahamCampbell
Copy link

GrahamCampbell commented Dec 3, 2020

Bug report

mb_str_split split may return false on error, but PHPStan no longer thinks so (but it used to not complain about this). For example, on PHP 7.4: https://3v4l.org/9ZQGj.

Code snippet that reproduces the problem

https://phpstan.org/r/e5baddbc-759f-426b-aa23-7e0a23cf25a8

Expected output

No issue.

@ThomasLandauer
Copy link
Contributor

ThomasLandauer commented Dec 3, 2020

I was just about to report the same, and already had the playground prepared :-)
https://phpstan.org/r/8b1844d5-06c0-45c5-847c-3b1c3474b448

I think the problem is coming from phpstan/phpstan-src#379

https://www.php.net/manual/en/function.mb-str-split.php#refsect1-function.mb-str-split-returnvalues says:

mb_str_split() returns an array of strings, or FALSE on failure.

@ondrejmirtes
Copy link
Member

Turns out str_split and mb_str_split are not exactly the same. /cc @VincentLanglet.

@VincentLanglet
Copy link
Contributor

I was just about to report the same, and already had the playground prepared :-)
https://phpstan.org/r/8b1844d5-06c0-45c5-847c-3b1c3474b448

Phpstan is correctly returning an error, since the value is ['a', 'b', 'c'] and will never be false.
image

mb_str_split() returns an array of strings, or FALSE on failure.

Yes, but like str_split, their are some case where we can be sure that it won't return false.

Turns out str_split and mb_str_split are not exactly the same. /cc @VincentLanglet.

I would say that when mb_str_split is used with exactly 2 parameters, it behave exactly like str_split.

If the third parameter is a contant

If the third parameter is a variable, we can't know.

It's maybe easier to use a different FunctionReturnType extension for str_split and for mb_str_split.
But as a quick fix of mb_str_split, it could behave like str_split only if the third parameter is omitted. WDYT ?

@ondrejmirtes
Copy link
Member

We can change the returned type for mb_str_split based on the third argument.

There's already some extension in PHPStan itself that checks this function (mb_list_encodings), feel free to draw inspiration from that.

@VincentLanglet
Copy link
Contributor

Will try something phpstan/phpstan-src#396

@ondrejmirtes
Copy link
Member

Fixed by: phpstan/phpstan-src#396

@github-actions
Copy link

github-actions bot commented May 4, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants