Skip to content

Conversation

Wosi
Copy link

@Wosi Wosi commented Nov 19, 2019

Documentation says about substr:

Returns the extracted part of string; or FALSE on failure, or an empty string.

https://www.php.net/manual/en/function.substr.php

I fixed the return value in functionMap.

@ondrejmirtes
Copy link
Member

Hi, this would be extra-annoying, substr almost never returns false so it would be annoying to force people on higher levels to always check for that. As you can see, it caused many false-positives in the build. See #1215.

@Wosi
Copy link
Author

Wosi commented Nov 20, 2019

I understand.
My problem is that I'm currently working on a legacy code base that I want to clean up to PhpStan level 7. In that code base there are some checks if substr returned false. PhpStan creates false positives for them. How should PhpStan users deal with such checks?

@ondrejmirtes
Copy link
Member

Please provide a code sample, I have no idea what message are you trying to avoid...

@ondrejmirtes
Copy link
Member

You can always persuade PHPStan with @var: https://phpstan.org/r/7bd2ed96-8e54-436d-8643-a206f62cf522

@Wosi
Copy link
Author

Wosi commented Nov 20, 2019

Maybe I can stick with a type hint like this, thanks.

I see it's annoying to check the result always against false but if you use PhpStan to avoid bad surprises and enforce defensive programming then I think PhpStan should use correct method signatures.
Maybe we need separate rules for cases like this.

@ondrejmirtes
Copy link
Member

Yes, I agree in most cases, but with gradual typing system that PHPStan it's also its job and the challenge not to annoy users with false positives, and string|false for substr would do that a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants