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

Not correct psalm return type for filter #273

Open
Legion112 opened this issue May 13, 2021 · 3 comments
Open

Not correct psalm return type for filter #273

Legion112 opened this issue May 13, 2021 · 3 comments

Comments

@Legion112
Copy link

This code is valid for plain array_filter for the psalm.
https://psalm.dev/r/31351f98b0
But if you try to do a similar thing with the collection psalm will tell it is not valid.
image

<?php
declare(strict_types=1);

require '../vendor/autoload.php';

use Doctrine\Common\Collections\ArrayCollection;


class Test {
    public int $a = 1;
}
$array = [];
$array[] = random_int(0, 1) === 1 ? new Test : null;
$array = new ArrayCollection($array);

$array = $array->filter(static fn(?Test $t):bool => $t !== null);
$array = $array->map(static fn(Test $t) => $t->a);
foreach ($array as $t) {
    echo $t;
}

@Legion112
Copy link
Author

The problem with the filter method. I think psalm annotation is not correct for this method.

@VincentLanglet
Copy link
Contributor

It's because psalm has a specific ReturnTypeProvider for array_filter.
https://github.com/vimeo/psalm/blob/4.x/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayFilterReturnTypeProvider.php

It require to do the same for Collection::filter in this repository https://github.com/psalm/psalm-plugin-doctrine/tree/2.x/src/Provider/ReturnTypeProvider

@Legion112
Copy link
Author

@VincentLanglet I guess and issue should be created there to describe what need to be done.

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

No branches or pull requests

2 participants