-
Notifications
You must be signed in to change notification settings - Fork 650
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
Double coalescing confuses array analyser #9174
Comments
I found these snippets: https://psalm.dev/r/fa647867eb<?php
function doubleCoalesce(?string $param): string {
return explode(':', $param ?? '', 2)[1] ?? '';
}
function singleCoalesce(string $param): string {
return explode(':', $param, 2)[1] ?? '';
}
|
I’ve been biten by this bug, too. Upgrading from 5.4.0 to 5.6.0 left me with a <?php
function test(string $input) : void {
if (strpos($input, '=') === false) {
return;
}
list($key, $value) = explode('=', $input, 2); # ! here I know that explode returns
# an array of length 2, but Psalm reports an error
}
test('a=b'); |
@Boldewyn that's beyond Psalm's understanding, so you need to help it a bit: https://psalm.dev/r/3140be0736 It doesn't seem to have anything to do with the original report though. |
I found these snippets: https://psalm.dev/r/3140be0736<?php
function test(string $input) : void {
if (strpos($input, '=') === false) {
return;
}
$v = explode('=', $input, 2);
assert(count($v) === 2);
list($_key, $_value) = $v;
}
test('a=b');
|
OK, understood! Thank you! It just happens that psalm started to report these lines in versions > 5.4.0, and the snippet in the original post seemed to match somewhat the case I saw. |
@Boldewyn It seems the thing which is introduced in 5.5.0 is not the bug itself, but rather the new feature - better types of And that feature probably just exposed the already existing bug I'm reporting here. And this is also the reason why you too get the error which you didn't earlier. |
explode
analyser
I have updated the issue title and the first message description. Provided there a reproducer which does not rely on |
Hum, weird, doc says that null coalescing should suppress the error: https://psalm.dev/docs/running_psalm/issues/PossiblyUndefinedArrayOffset/ https://psalm.dev/r/97e3b85fac (I posted your snippet again for better triage) |
I found these snippets: https://psalm.dev/r/97e3b85fac<?php
/** @return list{0: string, 1?: string} */
function returnOneOrTwoVals(string $param): array {
return $param === 'two' ? [ $param, $param ] : [ $param ] ;
}
function doubleCoalesce(?string $param): string {
return returnOneOrTwoVals($param ?? '')[1] ?? '';
}
function singleCoalesce(string $param): string {
return returnOneOrTwoVals($param)[1] ?? '';
}
|
https://psalm.dev/r/fa647867ebUpd: reproducer without using
explode
: https://psalm.dev/r/97e3b85facShould report no issue in both functions.
The bug has been introduced sometime after 5.4.0.Upd: the
explode
types refinement has been introduced in 5.5.0 by #9016 which exposed the bug, but the bug itself most probably existed way before the 5.4.0.The text was updated successfully, but these errors were encountered: