Skip to content

Method returns static instead of BackedEnum #7121

@p4veI

Description

@p4veI

Bug report

Hello I've come across this issue with a helper class that uses generics and tries to resolve a class instance of native php enum instance of BackedEnum, but phpstan keeps reporting the return type here should be static

This happened with the latest release of phpstan v1.6.0

Consider the following example:

Code snippet that reproduces the problem

<?php

declare(strict_types=1);

use function PHPStan\dumpType;

final class Foo
{
    /**
     * @see BackedEnum
     *
     * @param class-string<TEnum> $enum
     *
     * @return TEnum
     *
     * @template TEnum of BackedEnum
     */
    public static function fromIntOrNull(int $value, string $enum): BackedEnum
    {
        dumpType($enum);
        $v = $enum::from($value);
        dumpType($v);
        return $v;
    }
}

I was unable to reproduce this behaviour in phpstan playground unfortunately, but the output I'm getting is the following:

 ------ ----------------------------------------------------------------------------------------- 
  Line   src/Core/Domain/Value/Foo.php                                                            
 ------ ----------------------------------------------------------------------------------------- 
  20     Dumped type: class-string<TEnum of BackedEnum (method Foo::fromIntOrNull(), argument)>  
  22     Dumped type: static                                                                      
  23     Method Foo::fromIntOrNull() should return TEnum of BackedEnum but returns static.       
 ------ ----------------------------------------------------------------------------------------- 

Expected output

No issues reported

Did PHPStan help you today? Did it make you happy in any way?

Of course! Always helps to learn something new about PHP features ❤️

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions