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

Return enums as collection requires all individual cases #6695

Closed
phh opened this issue Feb 24, 2022 · 3 comments
Closed

Return enums as collection requires all individual cases #6695

phh opened this issue Feb 24, 2022 · 3 comments

Comments

@phh
Copy link

phh commented Feb 24, 2022

Bug report

Originally created here: larastan/larastan#1129

When adding enums to my project, I'd like to return all the cases as a collection.

I kinda would expect this return type would work:

/** @return Collection<int, Role> */

Since the returned value of Role::cases()[0] is Role.

However, it requires all possible cases set:

/** @return Collection<int, Role::Admin|Role::Supporter|Role::User> */

So I'm getting this error:

Method App\Enums\Role::toCollection() should return Illuminate\Support\Collection<int, App\Enums\Role>
    but returns Illuminate\Support\Collection<int, App\Enums\Role::Admin|App\Enums\Role::Supporter|App\Enums\Role::User>

Is this the intended behavior?

Laravel code where the issue was found

enum Role: int
{
    case Admin = 1;
    case Supporter = 2;
    case User = 3;

    /** @return Collection<int, Role:> */
    public static function toCollection(): Collection
    {
        return collect(self::cases());
    }
}

Code snippet that reproduces the problem

https://phpstan.org/r/e7c7fd51-04fc-4f4f-a63e-3794812f3065

Expected output

I was expecting that return type Enum would be enough, not the individual Enum case.

Did PHPStan help you today? Did it make you happy in any way?

Happy to use PHPStan as a part of our CI. Thank you so much for making code cleaner and my IDE happier :>

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@9e42896

@phh
Copy link
Author

phh commented Feb 28, 2022

Thank you @ondrejmirtes 🙏

@github-actions
Copy link

github-actions bot commented Apr 1, 2022

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants