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

Enum values as class strings #9938

Closed
vlakarados opened this issue Jun 20, 2023 · 4 comments · Fixed by #10165
Closed

Enum values as class strings #9938

vlakarados opened this issue Jun 20, 2023 · 4 comments · Fixed by #10165

Comments

@vlakarados
Copy link

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?:

@psalm-github-bot
Copy link

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);
Psalm output (using commit 653ad66):

INFO: MixedMethodCall - 15:12 - Cannot call constructor on an unknown class

ERROR: ArgumentTypeCoercion - 19:9 - Argument 1 of factory expects class-string, but parent type 'Cli' provided

@vlakarados
Copy link
Author

Seems to be related
#8978

@orklah
Copy link
Collaborator

orklah commented Jun 21, 2023

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 Foo is less precise than Foo::class even if they're the same thing in the end.

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

@weirdan
Copy link
Collaborator

weirdan commented Mar 9, 2024

Fixed in #10165 (master).

@weirdan weirdan closed this as completed Mar 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants