Skip to content
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

PHPStan falsely detects explode() as returning empty arrays in cases where it doesn't #3961

Closed
colinodell opened this issue Oct 16, 2020 · 5 comments

Comments

@colinodell
Copy link

Bug report

The latest version of PHPStan incorrectly believes that the explode() function returns an empty array in cases where it doesn't.

Per the PHP manual:

Returns an array of strings created by splitting the string parameter on boundaries formed by the delimiter.

If delimiter is an empty string (""), explode() will return FALSE. If delimiter contains a value that is not contained in string and a negative limit is used, then an empty array will be returned, otherwise an array containing string will be returned.

Code snippet that reproduces the problem

See https://phpstan.org/r/a664b522-b2d8-4541-a071-f1e768a0f9ad

This snippet will always return a non-empty array because (per the PHP documentation):

  • If delimiter is an empty string (""), explode() will return FALSE. The string is never going to be empty
  • If delimiter contains a value that is not contained in string and a negative limit is used, then an empty array will be returned we're not providing a negative limit - we're using the default of PHP_INT_MAX
  • otherwise an array containing string will be returned - this is what the sample snippet always does

Expected output

PHPStan should not report any false errors

@colinodell colinodell changed the title PHPStan detects non-empty arrays as being empty PHPStan falsely detects explode() as returning empty arrays in cases where it doesn't Oct 16, 2020
@ondrejmirtes
Copy link
Member

The thing is that PHPStan started to understand what non-empty-list is, nothing changed about explode() per se.

We'd first have to have to implement support for non empty strings, which isn't too hard as I recently implemented support for numeric strings (phpstan/phpstan-src@1d27c61), then we would be able to know when we can be sure that explode() returns a non-empty array in its dynamic return type extension.

As this is a very complex scenario, I consider this a feature request.

@colinodell
Copy link
Author

colinodell commented Oct 16, 2020

This might be related to (or possibly a duplicate of) #3596. PHPStan didn't report the sample snippet as an error until the latest release which is why I thought it might be related to phpstan/phpstan-src@a4038b2

@ondrejmirtes
Copy link
Member

Just realized that $delimiter is supposed to be non-empty string, not the value. Since the delimiter is usually a literal string, it's already easy to implement this. Done: phpstan/phpstan-src@0069107

@colinodell
Copy link
Author

Thanks for the quick fix!

@github-actions
Copy link

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 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants