-
Notifications
You must be signed in to change notification settings - Fork 664
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
Operand of type false is always falsy #10578
Comments
I found these snippets: https://psalm.dev/r/5921391c4a<?php
/** @var list<array{id: int, template_id: int}> $records */
$records = json_decode(file_get_contents('test.json'), true);
$processed = [];
foreach($records as $record) {
if (empty($processed[$record['template_id']])) {
$processed[$record['template_id']] = [];
}
$processed[$record['template_id']][] = $record['id'];
}
|
It looks like this behavior was introduced in 5.20 with #10502. I ran into it when trying to update from 5.18 to 5.22. |
A smaller repro that's essentially the same thing, though it results in a different error: https://psalm.dev/r/2e5b073a15 It seems like this is may be an intentional change from #10502? In my repro:
This all makes sense from Psalm's point of view. I can't see an easy way to fix it except to revert that part of #10502 and go back to typing the expression as The workaround using You could maybe make the case that |
I found these snippets: https://psalm.dev/r/2e5b073a15<?php
/** @var array<true> $a */
if (empty($a['a'])) {}
|
Another approach could be to type |
That sounds like it could be a good approach. I'll try that. |
Psalm 5.22.2 https://psalm.dev/r/7bb11edb34 <?php
/**
* @param list<ArrayIterator> $list
* @return ArrayIterator
*/
function get(array $list, int $index): ArrayIterator
{
if (empty($list[$index])) {
throw new InvalidArgumentException('Index is out of range');
}
return $list[$index];
}
|
I found these snippets: https://psalm.dev/r/7bb11edb34<?php
/**
* @param list<ArrayIterator> $list
* @return ArrayIterator
*/
function get(array $list, int $index): ArrayIterator
{
if (empty($list[$index])) {
throw new InvalidArgumentException('Index is out of range');
}
return $list[$index];
}
|
I'm playing with a fix using context's For example, one of the expressions that causes problems in the Psalm code base is:
The whole left side of the This has repercussions beyond this specific bug since it means that some types of issues inside It's a larger issue, but context management overall feels pretty problematic in the Psalm code base. I have to think there'd be many other issues like this. |
Possibly related issues or duplicates: |
These new issues have been reported in vimeo/psalm#10578
* Fix criteria mapping type * Ignore psalm issues These new issues have been reported in vimeo/psalm#10578
The following code
produces the following error
but it shouldn't. It seems the issue is that
psalm
doesn't know the data inside$records
. If$records
is defined likethere is no error reported.
Reproduce on psalm.dev: https://psalm.dev/r/5921391c4a
Workaround: replace
empty()
with! isset()
-if (empty($processed[$record['template_id']])) {
=>if (! isset($processed[$record['template_id']])) {
Edit: syntax highlighting
The text was updated successfully, but these errors were encountered: