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
Wrong LessSpecificReturnStatement and MoreSpecificReturnType #2670
Comments
I found these snippets: https://psalm.dev/r/657911d214<?php
/**
* @psalm-return array{something: int, tags: list<string>}
*/
function testFunc() : array {
$metadata = ['something' => rand()];
// imagine a bunch of code with a bunch of checks and filters including:
$metadata['tags'] = array_filter($metadata['tags'], fn($tag) => is_string($tag));
$metadata['tags'] = array_values($metadata['tags']);
return $metadata;
}
|
I believe it's tripping up because |
I found these snippets: https://psalm.dev/r/1f8c1c193c<?php
/**
* @psalm-return array{something: int, tags: list<string>}
*/
function testFunc() : array {
$metadata = ['something' => rand(), 'tags' => ['foo', 1, 2.3]];
// imagine a bunch of code with a bunch of checks and filters including:
$metadata['tags'] = array_filter($metadata['tags'], fn($tag) => is_string($tag));
$metadata['tags'] = array_values($metadata['tags']);
return $metadata;
}
|
Sorry! The fact that it's missing is only an issue with my test code, the actual code loads a bunch of things from a yaml file, amongst which is 'tags', I even have a check to create an empty array it if it's missing |
does it make a difference in your local code if you |
Surprisingly it does! I expected it to make no difference, but it absolutely does |
Did you try on latest master? |
Pretty sure this is a dupe of #2627 – I'll release soon. |
In some code I wrote that 2 errors would show up no matter what I did, LessSpecificReturnStatement and MoreSpecificReturnType, so I noticed that psalm doesn't know when a list contains for certain only strings.
Here's some example code that shows the issue https://psalm.dev/r/657911d214
ensures without any doubt that $metadata['tags'] only contains strings.
What's interesting is that
works. However it's not ideal as it's not filtering anything, just string casting.
Maybe psalm doesn't consider array_filter?
The text was updated successfully, but these errors were encountered: