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
24 changes: 24 additions & 0 deletions src/PhpDoc/TypeNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Generic\GenericStaticType;
use PHPStan\Type\Generic\TemplateAppliedGenericObjectType;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\Generic\TemplateTypeFactory;
use PHPStan\Type\Generic\TemplateTypeMap;
Expand Down Expand Up @@ -838,6 +839,29 @@ static function (string $variance): TemplateTypeVariance {
}
$mainTypeClassName = $mainTypeObjectClassNames[0] ?? null;

if ($mainType instanceof TemplateType && $mainTypeClassName !== null) {
if ($this->getReflectionProvider()->hasClass($mainTypeClassName)) {
$classReflection = $this->getReflectionProvider()->getClass($mainTypeClassName);
if ($classReflection->isGeneric()) {
$templateTypes = array_values($classReflection->getTemplateTypeMap()->getTypes());
for ($i = count($genericTypes), $templateTypesCount = count($templateTypes); $i < $templateTypesCount; $i++) {
$templateType = $templateTypes[$i];
if (!$templateType instanceof TemplateType || $templateType->getDefault() === null) {
continue;
}
$genericTypes[] = $templateType->getDefault();
}
}
}

return new TemplateAppliedGenericObjectType(
$mainType->getName(),
$mainTypeClassName,
$genericTypes,
variances: array_values($variances),
);
}

if ($mainTypeClassName !== null) {
if (!$this->getReflectionProvider()->hasClass($mainTypeClassName)) {
return new GenericObjectType($mainTypeClassName, $genericTypes, variances: $variances);
Expand Down
20 changes: 19 additions & 1 deletion src/Reflection/ResolvedFunctionVariantWithOriginal.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPStan\Type\ErrorType;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Generic\GenericStaticType;
use PHPStan\Type\Generic\TemplateAppliedGenericObjectType;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\Generic\TemplateTypeHelper;
use PHPStan\Type\Generic\TemplateTypeMap;
Expand All @@ -18,6 +19,7 @@
use PHPStan\Type\TypeUtils;
use function array_key_exists;
use function array_map;
use function count;

final class ResolvedFunctionVariantWithOriginal implements ResolvedFunctionVariant
{
Expand Down Expand Up @@ -242,7 +244,23 @@ private function resolveResolvableTemplateTypes(Type $type, TemplateTypeVariance
return $newType;
}

return $traverse($type);
$result = $traverse($type);

if ($result instanceof TemplateAppliedGenericObjectType) {
$resolvedType = $this->resolvedTemplateTypeMap->getType($result->getTemplateName());
if ($resolvedType !== null && !$resolvedType instanceof ErrorType) {
$resolvedClassNames = $resolvedType->getObjectClassNames();
if (count($resolvedClassNames) === 1) {
return new GenericObjectType(
$resolvedClassNames[0],
$result->getTypes(),
variances: $result->getVariances(),
);
}
}
}

return $result;
};

return TypeTraverser::map($type, function (Type $type, callable $traverse) use ($references, $objectCb): Type {
Expand Down
51 changes: 51 additions & 0 deletions src/Type/Generic/TemplateAppliedGenericObjectType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Generic;

use PHPStan\Type\Type;
use function array_values;

/**
* Represents a template type used with explicit generic args at the usage site.
* For example, K<T> where @template K of IFoo.
*
* This is distinct from TemplateGenericObjectType which represents a template
* declared with a generic bound (@template K of IFoo<T>).
*/
final class TemplateAppliedGenericObjectType extends GenericObjectType
{

/**
* @param non-empty-string $templateName
* @param list<Type> $types
* @param list<TemplateTypeVariance> $variances
*/
public function __construct(
private string $templateName,
string $className,
array $types,
?Type $subtractedType = null,
array $variances = [],
)
{
parent::__construct($className, $types, $subtractedType, variances: $variances);
}

/** @return non-empty-string */
public function getTemplateName(): string
{
return $this->templateName;
}

protected function recreate(string $className, array $types, ?Type $subtractedType, array $variances = []): GenericObjectType
{
return new self(
$this->templateName,
$className,
array_values($types),
$subtractedType,
array_values($variances),
);
}

}
62 changes: 62 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-4971.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php declare(strict_types = 1);

namespace Bug4971;

use function PHPStan\Testing\assertType;

/**
* @template T
*/
interface IFoo
{
/** @param T $v */
public function __construct($v);
}

/**
* @template T
* @implements IFoo<T>
*/
class Foo implements IFoo
{
/** @var T */
private $v; // @phpstan-ignore property.uninitializedReadonly

/**
* @param T $v
*/
public function __construct($v)
{
$this->v = $v;
}
}

/**
* @template T
* @template K of IFoo
* @param T $v
* @param class-string<K> $class
* @return K
*/
function make1($v, string $class)
{
return new $class($v);
}

/**
* @template T
* @template K of IFoo
* @param T $v
* @param class-string<K> $class
* @return K<T>
*/
function make2($v, string $class)
{
return new $class($v);
}

$obj1 = make1(1, Foo::class);
assertType('Bug4971\Foo', $obj1);

$obj2 = make2(1, Foo::class);
assertType('Bug4971\Foo<int>', $obj2);
Loading