You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've tried extending PSR-11 with a stub to create a conditional return type that returns an instance of a class name.
<?phpdeclare(strict_types=1);
namespacePsr\Container;
/** * Describes the interface of a container that exposes methods to read its entries. */interfaceContainerInterface
{
/** * @template T * @phpstan-param string|class-string<T> $id * @phpstan-return ($id is class-string<T> ? T : mixed) */publicfunctionget(string$id);
/** * @param string $id Identifier of the entry to look for. * @return bool */publicfunctionhas(string$id);
}
Contrary to expectations, this conditional return type does not work because string|class-string<T> is normalized to string. string is a super type of class-string, but in practice I think it is more convenient to keep it as a union type without normalizing it.
Did PHPStan help you today? Did it make you happy in any way?
We've written some PHPStan extensions for our internal projects, but we're very pleased with the introduction of the conditional return type as we can't keep up with the implementation of the extensions to meet the demand ❤️
The text was updated successfully, but these errors were encountered:
Feature request
I've tried extending PSR-11 with a stub to create a conditional return type that returns an instance of a class name.
https://phpstan.org/r/0cf57a6a-db47-46fa-838b-0128a1beef6d
Contrary to expectations, this conditional return type does not work because
string|class-string<T>
is normalized tostring
.string
is a super type ofclass-string
, but in practice I think it is more convenient to keep it as a union type without normalizing it.Did PHPStan help you today? Did it make you happy in any way?
We've written some PHPStan extensions for our internal projects, but we're very pleased with the introduction of the conditional return type as we can't keep up with the implementation of the extensions to meet the demand ❤️
The text was updated successfully, but these errors were encountered: