Skip to content
Draft
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
14 changes: 14 additions & 0 deletions src/PhpDoc/Tag/TemplateTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\Type;
use PHPStan\Type\TypeTraverser;

/**
* @api
Expand Down Expand Up @@ -41,4 +42,17 @@ public function getVariance(): TemplateTypeVariance
return $this->variance;
}

/**
* @param callable(Type $type, callable(Type): Type $traverse): Type $callback
*/
public function changeType(callable $callback): self
{
return new self(
$this->name,
TypeTraverser::map($this->bound, $callback),
$this->default !== null ? TypeTraverser::map($this->default, $callback) : null,
$this->variance,
);
}

}
27 changes: 20 additions & 7 deletions src/Type/FileTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use PHPStan\Reflection\ReflectionProvider\ReflectionProviderProvider;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Generic\TemplateStrictMixedType;
use PHPStan\Type\Generic\TemplateTypeFactory;
use PHPStan\Type\Generic\TemplateTypeHelper;
use PHPStan\Type\Generic\TemplateTypeMap;
Expand Down Expand Up @@ -283,13 +284,25 @@ public function getNameScope(
continue;
}

$templateTags = $this->phpDocNodeResolver->resolveTemplateTags($parent->getTemplatePhpDocNodes(), $nameScope);
$templateTypeMap = new TemplateTypeMap(array_map(static fn (TemplateTag $tag): Type => TemplateTypeFactory::fromTemplateTag($templateTypeScope, $tag), $templateTags));
$nameScope = $nameScope->withTemplateTypeMap($templateTypeMap, $templateTags);
$templateTags = $this->phpDocNodeResolver->resolveTemplateTags($parent->getTemplatePhpDocNodes(), $nameScope);
$templateTypeMap = new TemplateTypeMap(array_map(static fn (TemplateTag $tag): Type => TemplateTypeFactory::fromTemplateTag($templateTypeScope, $tag), $templateTags));
$nameScope = $nameScope->withTemplateTypeMap($templateTypeMap, $templateTags);
$templateTags = $this->phpDocNodeResolver->resolveTemplateTags($parent->getTemplatePhpDocNodes(), $nameScope);
$templatePhpDocNodes = $parent->getTemplatePhpDocNodes();
$temporaryTemplateTypeMap = new TemplateTypeMap(array_map(static fn (array $tag) => TemplateTypeFactory::create($templateTypeScope, $tag[1]->name, new StrictMixedType(), TemplateTypeVariance::createInvariant()), $templatePhpDocNodes));

$templateTags = $this->phpDocNodeResolver->resolveTemplateTags($templatePhpDocNodes, $nameScope->withTemplateTypeMap($temporaryTemplateTypeMap, []));
$temporaryTemplateTypeMap = new TemplateTypeMap(array_map(static fn (TemplateTag $tag): Type => TemplateTypeFactory::fromTemplateTag($templateTypeScope, $tag), $templateTags));
foreach ($templateTags as $k => $templateTag) {
$templateTags[$k] = $templateTag->changeType(static function (Type $type, $traverse) use ($temporaryTemplateTypeMap): Type {
if ($type instanceof TemplateStrictMixedType) {
$newType = $temporaryTemplateTypeMap->getType($type->getName());
if ($newType === null) {
throw new ShouldNotHappenException();
}

return $newType;
}

return $traverse($type);
});
}
$templateTypeMap = new TemplateTypeMap(array_map(static fn (TemplateTag $tag): Type => TemplateTypeFactory::fromTemplateTag($templateTypeScope, $tag), $templateTags));
foreach (array_keys($templateTags) as $name) {
$templateType = $templateTypeMap->getType($name);
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Generic/TemplateTypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use PHPStan\Type\ObjectShapeType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\StrictMixedType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
Expand Down Expand Up @@ -87,6 +88,10 @@ public static function create(TemplateTypeScope $scope, string $name, ?Type $bou
return new TemplateBooleanType($scope, $strategy, $variance, $name, $bound, $default);
}

if ($bound instanceof StrictMixedType && ($boundClass === StrictMixedType::class || $bound instanceof TemplateType)) {
return new TemplateStrictMixedType($scope, $strategy, $variance, $name, $bound, $default);
}

if ($bound instanceof MixedType && ($boundClass === MixedType::class || $bound instanceof TemplateType)) {
return new TemplateMixedType($scope, $strategy, $variance, $name, $bound, $default);
}
Expand Down
Loading