Skip to content
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

Support reduced type inferring from Collection::filter #58

Open
ostrolucky opened this issue May 26, 2020 · 4 comments
Open

Support reduced type inferring from Collection::filter #58

ostrolucky opened this issue May 26, 2020 · 4 comments
Labels
enhancement New feature or request

Comments

@ostrolucky
Copy link

Plugin should add capability to infer reduced type from Collection::filter based on assertions in a callback, similarly as Psalm currently does for array_filter. Here is what should be supported: https://psalm.dev/r/1b5cbdc9c0

Backstory: From conversation with @muglug:

Add that to the doctrine plugin

This is the current annotation: https://github.com/doctrine/collections/blob/a4504c79efd8847cc77d10b70209ef838b10338f/lib/Doctrine/Common/Collections/Collection.php#L214-L225

there’s basically no way to annotate that in a docblock

@weirdan
Copy link
Member

weirdan commented May 26, 2020

Can you provide the example of desired behavior using array_filter?
This: https://psalm.dev/r/e24db6727c results in pretty much the same issues emitted as in your example with Collection::filter()

@ostrolucky
Copy link
Author

Hmm yeah so $stdClass !== null needs to be used explicitly, that's what I did after I refactored it into array_filter but assumed Psalm will know that (bool) object will result in non-nullable value. Anyways (bool) is different issue, here we go for array_filter inference demonstration https://psalm.dev/r/16fbce64d4

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/16fbce64d4
<?php
/**
 * @param array<array-key,stdClass|null> $in
 * @return array<array-key,stdClass>
 */
function f(array $in): array {
    return array_filter($in, function (?stdClass $elt) {
        return $elt !== null; 
    });
}
Psalm output (using commit ecb179c):

No issues!

@weirdan
Copy link
Member

weirdan commented May 26, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants