-
-
Notifications
You must be signed in to change notification settings - Fork 942
Closed
Description
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 ❤️
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels