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

if ($enum instanceof BackedEnum) doesn't infer BackedEnum type #10585

Closed
GromNaN opened this issue Jan 22, 2024 · 1 comment · Fixed by #10599
Closed

if ($enum instanceof BackedEnum) doesn't infer BackedEnum type #10585

GromNaN opened this issue Jan 22, 2024 · 1 comment · Fixed by #10599

Comments

@GromNaN
Copy link
Contributor

GromNaN commented Jan 22, 2024

BackedEnum always have the properties name and value. In the context of a method attribute with a type, using instanceof BackedEnum doesn't infer the type.

function fun(OperatorInterface $operator) :string|int
{
    if ($operator instanceof BackedEnum) {
        // $operator is OperatorInterface&BackedEnum
        // but psalm triggers NoInterfaceProperties error
        return $operator->value;
    }

    // ...
}

https://psalm.dev/r/48ec6feed1

Copy link

I found these snippets:

https://psalm.dev/r/48ec6feed1
<?php

interface Operator {}

enum Foo: string implements Operator
{
    case bar = 'bar';
}

function fun(Operator $operator) :string|int
{
    if ($operator instanceof BackedEnum) {
        // $operator is Operator&BackedEnum but psalm triggers NoInterfaceProperties error
        return $operator->value;
    }
    
    return '';
}

function fun1(BackedEnum $operator) :string|int
{
    return $operator->value;
}
Psalm output (using commit 3c90054):

ERROR: NoInterfaceProperties - 14:16 - Interfaces cannot have properties

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.

2 participants