1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

qa: add test for inherited conditional templated return types

Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com>
This commit is contained in:
Maximilian Bösing 2023-09-10 23:57:02 +02:00
parent f782767438
commit 3dc00518c6
No known key found for this signature in database
GPG Key ID: 9A8988C93CEC81A3

View File

@ -885,6 +885,72 @@ class ConditionalReturnTypeTest extends TestCase
'ignored_issues' => [],
'php_version' => '7.2',
],
'ineritedConditionalTemplatedReturnType' => [
'code' => '<?php
/** @template InstanceType */
interface ContainerInterface
{
/**
* @template TRequestedInstance extends InstanceType
* @param class-string<TRequestedInstance>|string $name
* @return ($name is class-string ? TRequestedInstance : InstanceType)
*/
public function build(string $name): mixed;
}
/**
* @template InstanceType
* @template-implements ContainerInterface<InstanceType>
*/
abstract class MixedContainer implements ContainerInterface
{
/** @param InstanceType $instance */
public function __construct(private readonly mixed $instance)
{}
public function build(string $name): mixed
{
return $this->instance;
}
}
/**
* @template InstanceType of object
* @template-extends MixedContainer<InstanceType>
*/
abstract class ObjectContainer extends MixedContainer
{
public function build(string $name): object
{
return parent::build($name);
}
}
/** @template-extends ObjectContainer<stdClass> */
final class SpecificObjectContainer extends ObjectContainer
{
}
final class SpecificObject extends stdClass {}
$container = new SpecificObjectContainer(new stdClass());
$object = $container->build(SpecificObject::class);
$nonSpecificObject = $container->build("whatever");
/** @var ObjectContainer<object> $container */
$container = null;
$justObject = $container->build("whatever");
$specificObject = $container->build(stdClass::class);
',
'assertions' => [
'$object' => 'SpecificObject',
'$nonSpecificObject' => 'stdClass',
'$justObject' => 'object',
'$specificObject' => 'stdClass',
],
'ignored_issues' => [],
'php_version' => '8.1',
],
];
}
}