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

Support for value-of<MyEnum> #7874

Closed
MidnightDesign opened this issue Apr 16, 2022 · 2 comments
Closed

Support for value-of<MyEnum> #7874

MidnightDesign opened this issue Apr 16, 2022 · 2 comments

Comments

@MidnightDesign
Copy link
Contributor

MidnightDesign commented Apr 16, 2022

Psalm doesn't support backed enums as a type argument for value-of:

enum ReservationState: string {
    case Pending = 'pending';
    case Confirmed = 'confirmed';
    case Cancelled = 'cancelled';
}

/**
 * @param value-of<ReservationState> $value
 */
function makeState(string $value): ReservationState {
    return ReservationState::from($value);
}

makeState(ReservationState::Pending->value); // Works
makeState('confirmed'); // Works
makeState('foo'); // Doesn't work

https://psalm.dev/r/1277567ae5

Expected:
No errors

Actual:
ERROR: InvalidDocblock - 10:11 - Untemplated value-of param ReservationState should be an array in docblock for makeState

PHPStan supports this use of value-of.

(I've searched the existing issues for this, but surprisingly, I couldn't find one. I'm sorry if I've missed it.)

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/1277567ae5
<?php

enum ReservationState: string {
    case Pending = 'pending';
    case Confirmed = 'confirmed';
    case Cancelled = 'cancelled';
}

/**
 * @param value-of<ReservationState> $value
 */
function makeState(string $value): ReservationState {
    return ReservationState::from($value);
}

makeState(ReservationState::Pending->value); // Works
makeState('confirmed'); // Works
makeState('foo'); // Doesn't work
Psalm output (using commit b5b5c20):

ERROR: InvalidDocblock - 10:11 - Untemplated value-of param ReservationState should be an array in docblock for makeState

AndrolGenhald added a commit to AndrolGenhald/psalm that referenced this issue Jul 18, 2022
orklah added a commit that referenced this issue Jul 20, 2022
Allow `value-of` to work with backed enums (fixes #7874).
@AndrolGenhald
Copy link
Collaborator

Fixed by #8283.

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

No branches or pull requests

2 participants