Skip to content

Commit

Permalink
qa: add test for inherited conditional templated return types
Browse files Browse the repository at this point in the history
Signed-off-by: Maximilian Bösing <[email protected]>
  • Loading branch information
boesing committed Sep 10, 2023
1 parent f782767 commit 3dc0051
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions tests/Template/ConditionalReturnTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,72 @@ function getSomethingElse()
'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',
],
];
}
}

0 comments on commit 3dc0051

Please sign in to comment.