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 #1129

Closed
phh opened this issue Feb 13, 2022 · 6 comments
Closed

Return enums as collection requires all individual cases #1129

phh opened this issue Feb 13, 2022 · 6 comments
Assignees
Labels
enhancement New feature or request

Comments

@phh
Copy link

phh commented Feb 13, 2022

  • Larastan Version: 9.0.2
  • --level used: 6
  • Pull request with failing test:

Description

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());
    }
}
@szepeviktor szepeviktor added the enhancement New feature or request label Feb 13, 2022
@phh
Copy link
Author

phh commented Feb 18, 2022

Mainly wondering if this is the expected behavior or not. If not I would love to come up with a suggestion for a fix!

@canvural
Copy link
Collaborator

Hi,

I don't understand the issue.

I kinda would expect this return type would work:

What is the error you are getting?

@phh
Copy link
Author

phh commented Feb 21, 2022

Hi,

I don't understand the issue.

I kinda would expect this return type would work:

What is the error you are getting?

Sorry, forgot the error. Added to the description as well:

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>

@canvural
Copy link
Collaborator

canvural commented Feb 21, 2022

Ok, this looks like a PHPStan issue. Here is a simple reproduction: https://phpstan.org/r/e7c7fd51-04fc-4f4f-a63e-3794812f3065

Feel free to create an issue there with the playground link I gave.

@phh
Copy link
Author

phh commented Feb 24, 2022

@canvural Thanks for reproducing the error in phpstan.org 🙏

@phh
Copy link
Author

phh commented Feb 28, 2022

Fixed here phpstan/phpstan#6695 / phpstan/phpstan-src@9e42896

Thank you again.

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

3 participants