Skip to content

When building a collection from BackedEnum values, the resulting type of the values is broadened to string #12420

Closed
phpstan/phpstan-src
#6332

Description

@vctls

Bug report

I've got a custom Collection class that uses a template for its values.
When a create an instance of that collection using BackedEnum values, the type of the values is detected as string instead of an union of the specific values of that enum.
When creating a simple array from the same enum values, the exact values are retained.

I simplified the example a bit from my initial question: #11867

Code snippet that reproduces the problem

https://phpstan.org/r/66facd10-d1e6-41dd-9146-9df0c7a66496

Expected output

new Collection([Code::FOO->value,Code::BAR->value,]) should return Collection<'bar'|'foo'> but returns Collection<string>

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

We use custom collections a lot in our current project, and I would just not be possible without PhpStan generics. Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions