mirror of
https://github.com/danog/psalm.git
synced 2024-11-27 04:45:20 +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:
parent
f782767438
commit
3dc00518c6
@ -885,6 +885,72 @@ class ConditionalReturnTypeTest extends TestCase
|
|||||||
'ignored_issues' => [],
|
'ignored_issues' => [],
|
||||||
'php_version' => '7.2',
|
'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',
|
||||||
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user