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

strtok is never false when called with both the string and the token #3981

Closed
aszenz opened this issue Oct 22, 2020 · 8 comments
Closed

strtok is never false when called with both the string and the token #3981

aszenz opened this issue Oct 22, 2020 · 8 comments

Comments

@aszenz
Copy link

aszenz commented Oct 22, 2020

Just want to confirm this is correct, before creating a pr:

So strtok returns false only it reaches the end of the string and there are no more tokens to extract like

$a = "hi this is good";
strtok($a, " "); // returns "hi"
strtok(" "); // returns "this"
strtok(" ")  ; // return "is"
strtok(" ")  ; // return "good"
strtok(" ")  ; // now it returns false after reaching the end of the string

But phpstan considers strtok($a, " ") to also return false|string even though its only when called without first argument that it can return false. Is there a way to model this function correctly with phpstan's types?

https://phpstan.org/r/abe8c204-911c-477e-bceb-8854d19ce4dc

@canvural
Copy link
Contributor

If $a is an empty string it can also return false. But I guess this can be checked by PHPStan.

@aszenz
Copy link
Author

aszenz commented Oct 22, 2020

@canvural

Thanks for the info, i guess that explains the false return type.

Not sure if string length can be checked statically anyways so i think the typing is perfectly alright, this issue can be closed unless some one else has a different opinion on it

@ondrejmirtes
Copy link
Member

So if I get it correctly, strtok($nonEmptyString, ' ') should not have the false type. I can implement non-empty-string type pretty easily.

@aszenz
Copy link
Author

aszenz commented Oct 22, 2020

Hmm, which functions would return this new type non-empty-string, or would we manually have to var annotate string variables that we think are non empty.

@ondrejmirtes
Copy link
Member

There's going to be multiple ways to produce non-empty-string:

  1. PHPDoc @param type
  2. Check strlen($str) > 0 beforehand
  3. Check $str !== '' beforehand

Without these, the strtok call will still have |false which will be correct.

@aszenz
Copy link
Author

aszenz commented Oct 22, 2020

Aha, thanks for the explanation, so this narrows down the type after certain checks which is great.

I see php-stan recently added non-empty-array so I guess a non-empty-string would also be a nice feature to have.

@ondrejmirtes
Copy link
Member

Implemented by: phpstan/phpstan-src@6ef5e91

@github-actions
Copy link

github-actions bot commented Aug 8, 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 Aug 8, 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

3 participants