-
Notifications
You must be signed in to change notification settings - Fork 666
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
Enum values as class strings #9938
Comments
I found these snippets: https://psalm.dev/r/c71751d4fe<?php
class Cli { public function __construct() {} }
class Web { public function __construct() {} }
enum Loader: string
{
case CLI = Cli::class;
case WEB = Web::class;
}
/** @param class-string $s */
function factory(string $s): object
{
return new $s;
}
$enum = Loader::CLI;
factory($enum->value);
|
Seems to be related |
Seems like the structure that holds the value of a case was designed to only hold literals (ints or strings):
That's fine in most cases because literals are pretty much the most precise type you could end up with... except for class-string (the literal So yeah, the value property here should be transformed into an Atomic (so it can handle any type from Psalm (as long as we only have one but it should be ok) that should not be very hard given in most place, we're actually jumping hoops to extract the literal from an Atomic or the other way around |
Fixed in #10165 ( |
Basically here's working code sample:
https://psalm.dev/r/c71751d4fe
Also is it possible to create a hint for enum values to be all class-strings?:
The text was updated successfully, but these errors were encountered: