Skip to content

Commit 575f038

Browse files
committed
Support generic type aliases
@phpstan-type Foo<T> declares parameters that Foo<int> replaces. A bare alias, a wrong number of arguments and an argument outside its bound make the type unresolvable. An alias that refers to itself becomes a recursive type when its arguments repeat, and is invalid when they grow. A parameter named like a template of the class is reported as typeAlias.shadowTemplate. A global alias declares its parameters in its key in the typeAliases parameter, like 'Foo<T of int>': 'list<T>', and resolves like a local one. Amp-Thread-ID: https://ampcode.com/threads/T-01a22307-fb39-7133-a55b-a5cc3cda81e7
1 parent d8b6eb0 commit 575f038

21 files changed

Lines changed: 1638 additions & 88 deletions

‎src/DependencyInjection/ValidateIgnoredErrorsExtension.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ public function resolveTypeAlias(string $aliasName, NameScope $nameScope): ?Type
124124
return null;
125125
}
126126

127+
public function resolveTypeAliasWithArguments(string $aliasName, array $arguments, NameScope $nameScope): ?Type
128+
{
129+
return null;
130+
}
131+
127132
}),
128133
$constantResolver,
129134
new InitializerExprTypeResolver($constantResolver, $reflectionProviderProvider, new PhpVersion(PHP_VERSION_ID), new OperatorTypeSpecifyingExtensionRegistry(new DirectExtensionsCollection([])), new UnaryOperatorTypeSpecifyingExtensionRegistry(new DirectExtensionsCollection([])), new OversizedArrayBuilder(), true, $valueDependencyCollector),

‎src/PhpDoc/PhpDocNodeResolver.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
use function array_map;
4747
use function array_merge;
4848
use function array_reverse;
49+
use function array_values;
4950
use function count;
5051
use function in_array;
5152
use function method_exists;
@@ -541,7 +542,7 @@ public function resolveTypeAliasTags(PhpDocNode $phpDocNode, NameScope $nameScop
541542
foreach ($phpDocNode->getTypeAliasTagValues($tagName) as $typeAliasTagValue) {
542543
$alias = $typeAliasTagValue->alias;
543544
$typeNode = $typeAliasTagValue->type;
544-
$resolved[$alias] = new TypeAliasTag($alias, $typeNode, $nameScope);
545+
$resolved[$alias] = new TypeAliasTag($alias, $typeNode, $nameScope, array_values($typeAliasTagValue->templateTypes));
545546
}
546547
}
547548

‎src/PhpDoc/Tag/TypeAliasTag.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\PhpDoc\Tag;
44

55
use PHPStan\Analyser\NameScope;
6+
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
67
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
78
use PHPStan\Type\TypeAlias;
89

@@ -12,10 +13,14 @@
1213
final class TypeAliasTag
1314
{
1415

16+
/**
17+
* @param list<TemplateTagValueNode> $templateTagValueNodes the alias's own parameters, like the T of `Foo<T>`
18+
*/
1519
public function __construct(
1620
private string $aliasName,
1721
private TypeNode $typeNode,
1822
private NameScope $nameScope,
23+
private array $templateTagValueNodes = [],
1924
)
2025
{
2126
}
@@ -30,6 +35,8 @@ public function getTypeAlias(): TypeAlias
3035
return new TypeAlias(
3136
$this->typeNode,
3237
$this->nameScope,
38+
$this->aliasName,
39+
$this->templateTagValueNodes,
3340
);
3441
}
3542

‎src/PhpDoc/TypeNodeResolver.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,20 @@ private function resolveGenericTypeNode(GenericTypeNode $typeNode, NameScope $na
835835
}
836836

837837
$genericTypes = $this->resolveMultiple($typeNode->genericTypes, $nameScope);
838+
839+
if (
840+
!$typeNode->type instanceof NameNode
841+
&& !$nameScope->shouldBypassTypeAliases()
842+
&& (
843+
$nameScope->hasTypeAlias($typeNode->type->name)
844+
|| $this->getTypeAliasResolver()->hasTypeAlias($typeNode->type->name, null)
845+
)
846+
) {
847+
$aliasType = $this->getTypeAliasResolver()->resolveTypeAliasWithArguments($typeNode->type->name, $genericTypes, $nameScope);
848+
if ($aliasType !== null) {
849+
return $aliasType;
850+
}
851+
}
838852
$variances = array_map(
839853
static function (string $variance): TemplateTypeVariance {
840854
switch ($variance) {

‎src/Rules/Classes/LocalTypeAliasesCheck.php‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@
2727
use PHPStan\Type\TypeTraverser;
2828
use PHPStan\Type\VerbosityLevel;
2929
use function array_key_exists;
30+
use function array_keys;
3031
use function array_merge;
32+
use function explode;
3133
use function in_array;
3234
use function sprintf;
35+
use function strtolower;
36+
use function trim;
3337

3438
#[AutowiredService]
3539
final class LocalTypeAliasesCheck
@@ -138,7 +142,7 @@ public function checkInTraitDefinitionContext(ClassReflection $reflection): arra
138142
continue;
139143
}
140144

141-
if (array_key_exists($aliasName, $this->globalTypeAliases)) {
145+
if ($this->isGlobalTypeAlias($aliasName)) {
142146
$errors[] = RuleErrorBuilder::message(sprintf('Type alias %s already exists as a global type alias.', $aliasName))->identifier('typeAlias.duplicate')->build();
143147
continue;
144148
}
@@ -203,7 +207,7 @@ public function checkInTraitDefinitionContext(ClassReflection $reflection): arra
203207
continue;
204208
}
205209

206-
if (array_key_exists($aliasName, $this->globalTypeAliases)) {
210+
if ($this->isGlobalTypeAlias($aliasName)) {
207211
$errors[] = RuleErrorBuilder::message(sprintf('Type alias %s already exists as a global type alias.', $aliasName))->identifier('typeAlias.duplicate')->build();
208212
continue;
209213
}
@@ -215,7 +219,25 @@ public function checkInTraitDefinitionContext(ClassReflection $reflection): arra
215219
continue;
216220
}
217221

218-
$resolvedType = $typeAliasTag->getTypeAlias()->resolve($this->typeNodeResolver);
222+
// the alias's own parameter wins in its body, like a method's template over the class's
223+
$typeAlias = $typeAliasTag->getTypeAlias();
224+
$classTemplateTypes = $reflection->getTemplateTypeMap()->getTypes();
225+
foreach ($typeAlias->getTemplateNames() as $templateName) {
226+
if (!array_key_exists($templateName, $classTemplateTypes)) {
227+
continue;
228+
}
229+
230+
$errors[] = RuleErrorBuilder::message(sprintf(
231+
'PHPDoc tag @phpstan-type %s template %s shadows @template %s for %s %s.',
232+
$typeAlias->describe(),
233+
$templateName,
234+
$classTemplateTypes[$templateName]->describe(VerbosityLevel::typeOnly()),
235+
strtolower($reflection->getClassTypeDescription()),
236+
$reflection->getDisplayName(false),
237+
))->identifier('typeAlias.shadowTemplate')->build();
238+
}
239+
240+
$resolvedType = $typeAlias->resolve($this->typeNodeResolver);
219241
if ($this->hasErrorType($resolvedType, $aliasName, $errors)) {
220242
continue;
221243
}
@@ -406,4 +428,16 @@ private function hasErrorType(Type $type, string $aliasName, array &$errors): bo
406428
return $foundError;
407429
}
408430

431+
private function isGlobalTypeAlias(string $aliasName): bool
432+
{
433+
foreach (array_keys($this->globalTypeAliases) as $key) {
434+
// the key of a generic alias declares its parameters too, like `Foo<T>`
435+
if (trim(explode('<', $key, 2)[0]) === $aliasName) {
436+
return true;
437+
}
438+
}
439+
440+
return false;
441+
}
442+
409443
}

‎src/Testing/PHPStanTestCase.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
use PHPStan\Php\ConfiguredPhpVersionRangeHelper;
1818
use PHPStan\Php\PhpVersion;
1919
use PHPStan\PhpDoc\TypeNodeResolver;
20-
use PHPStan\PhpDoc\TypeStringResolver;
20+
use PHPStan\PhpDocParser\Lexer\Lexer;
21+
use PHPStan\PhpDocParser\Parser\TypeParser;
2122
use PHPStan\Reflection\AttributeReflectionFactory;
2223
use PHPStan\Reflection\InitializerExprTypeResolver;
2324
use PHPStan\Reflection\ReflectionProvider;
@@ -145,7 +146,8 @@ public static function createTypeAliasResolver(array $globalTypeAliases, Reflect
145146

146147
return new UsefulTypeAliasResolver(
147148
$globalTypeAliases,
148-
$container->getByType(TypeStringResolver::class),
149+
$container->getByType(Lexer::class),
150+
$container->getByType(TypeParser::class),
149151
$container->getByType(TypeNodeResolver::class),
150152
$reflectionProvider,
151153
0,

0 commit comments

Comments
 (0)