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

Difference between enum-string and class-string<UnitEnum> #11244

Open
zonuexe opened this issue Feb 5, 2025 · 1 comment
Open

Difference between enum-string and class-string<UnitEnum> #11244

zonuexe opened this issue Feb 5, 2025 · 1 comment

Comments

@zonuexe
Copy link
Contributor

zonuexe commented Feb 5, 2025

All enums implicitly inherit from UnitEnum, so calling the $enumString::cases() static method is always legal, but Psalm does not seem to behave that way: https://psalm.dev/r/ea20c1515d

/**
 * @param enum-string $s
 */
function f(string $s): void {
    $cases = $s::cases();
    var_dump($cases);
}

/**
 * @param enum-string<UnitEnum> $s
 */
function g(string $s): void {
    $cases = $s::cases();
    var_dump($cases);
}

I noticed this problem while checking the behavioral differences between PHPStan and Psalm to write a tutorial on typing in PHP.

refs #7404, phpstan/phpstan#12529, phpstan/phpstan-src#3807

Copy link

I found these snippets:

https://psalm.dev/r/ea20c1515d
<?php

/**
 * @param enum-string $s
 */
function f(string $s): void {
    $cases = $s::cases();
    var_dump($cases);
}

/**
 * @param enum-string<UnitEnum> $s
 */
function g(string $s): void {
    $cases = $s::cases();
    var_dump($cases);
}
Psalm output (using commit 5847d08):

INFO: MixedMethodCall - 7:14 - Cannot call method on an unknown class

INFO: MixedAssignment - 7:5 - Unable to determine the type that $cases is being assigned to

ERROR: ForbiddenCode - 8:5 - Unsafe var_dump

ERROR: ForbiddenCode - 16:5 - Unsafe var_dump

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant