Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeAlias;
use PHPStan\Type\TypehintHelper;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\VerbosityLevel;
use ReflectionClass as CoreReflectionClass;
use ReflectionException;
Expand Down Expand Up @@ -1861,12 +1862,26 @@ public function typeMapFromList(array $types): TemplateTypeMap
$className = $this->getName();
foreach ($resolvedPhpDoc->getTemplateTags() as $tag) {
$type = $types[$i] ?? $tag->getDefault() ?? $tag->getBound();
if ($type instanceof TemplateType && $type->getScope()->getClassName() === $className) {
// A template default may reference other template types of the same
// class, possibly nested inside a composite type like `TValue|null`.
// Never descend into a TemplateType's bound - bounds may be
// self-referential (`@template T of Foo<T>`).
$type = TypeTraverser::map($type, static function (Type $type, callable $traverse) use ($map, $className): Type {
if (!$type instanceof TemplateType) {
return $traverse($type);
}

if ($type->getScope()->getClassName() !== $className) {
return $type;
}

$resolved = $map[$type->getName()] ?? null;
if ($resolved !== null && !$resolved instanceof TemplateType) {
$type = $resolved;
return $resolved;
}
}

return $type;
});
$map[$tag->getName()] = $type;
$i++;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace TemplateDefaultReferringOtherNested;

use function PHPStan\Testing\assertType;

class Entity
{

}

class User extends Entity
{

}

class Post extends Entity
{

}

/**
* @template TRelated of Entity
* @template TDeclaring of Entity
* @template TBare = TRelated
* @template TResult = TRelated|null
* @template TList of array = list<TRelated>
* @template TShape of array = array{related: TRelated, declaring: TDeclaring}
*/
interface Relation
{

/** @return TBare */
public function bare(): mixed;

/** @return TResult */
public function getResults(): mixed;

/** @return TList */
public function all(): array;

/** @return TShape */
public function shape(): array;

}

/**
* @param Relation<User, Post> $defaulted
* @param Relation<User, Post, User, User> $explicit
*/
function test(Relation $defaulted, Relation $explicit): void
{
assertType('TemplateDefaultReferringOtherNested\User', $defaulted->bare());
assertType('TemplateDefaultReferringOtherNested\User|null', $defaulted->getResults());
assertType('list<TemplateDefaultReferringOtherNested\User>', $defaulted->all());
assertType('array{related: TemplateDefaultReferringOtherNested\User, declaring: TemplateDefaultReferringOtherNested\Post}', $defaulted->shape());

assertType('TemplateDefaultReferringOtherNested\User', $explicit->bare());
assertType('TemplateDefaultReferringOtherNested\User', $explicit->getResults());
}
Loading