-
Notifications
You must be signed in to change notification settings - Fork 660
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
strpos/stripos should assert non-empty-string like str_contains #9753
Comments
I found these snippets: https://psalm.dev/r/2f458027ac<?php
/**
* @var string $a
* @var string $b
*/
if ( str_contains( $a, 'foo' ) ) {
/** @psalm-trace $a */;
}
if ( strpos( $b, 'foo' ) !== false ) {
/** @psalm-trace $b */;
}
if ( stripos( $b, 'foo' ) !== false ) {
/** @psalm-trace $b */;
}
|
@var comments needed due to vimeo#9754 and vimeo#9753
seems easy to implement: psalm/stubs/CoreGenericFunctions.phpstub Line 1085 in 1a1d9c9
|
I'm not sure this is a simple fix as the return type of str(i)pos is false|int, there would need to be an assert-if-int annotation? |
Some tests would be needed but it could still work with |
Is it currently possible to do input type narrowing in a ReturnTypeProvider? |
0 is not truthy, but for things like
It could probably be possible to narrow the type from a ReturnTypeProvider but it may be hard. You do have the context so you know the type of each variable in the caller context and you can change them |
Why not just do it the other way round? assert-if-false -> if it's false, we cannot infer anything, but in the opposite case we know it's non-empty? |
assert-if-false would require changing the type to non-empty-string in the default case then widening when false which would require multiple annotations if it worked at all
Orklah's "hack" did indeed work he said it would so I've opened #9788 |
That was fixed :) |
https://psalm.dev/r/2f458027ac
The text was updated successfully, but these errors were encountered: