Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b973b5f
first pass
shmax Apr 1, 2026
eb67c21
enforce required generic params
shmax Apr 1, 2026
6fdb88f
more tests
shmax Apr 1, 2026
b27d26e
tests for invalud usage
shmax Apr 1, 2026
4f18071
fix regression
shmax Apr 1, 2026
95abc98
add more scenarios to test fixture
shmax Apr 1, 2026
f529784
remove empty patch
shmax Apr 1, 2026
590daa6
coalesce to empty array
shmax Apr 1, 2026
b8e0c55
lint
shmax Apr 1, 2026
a94b14b
fix use function ordering
shmax Apr 1, 2026
beedafb
fix generic alias resolution: skip alias path when name resolves to a…
shmax Apr 1, 2026
e023f92
remove resolveWithDefaults: bare generic alias usage restores old Tem…
shmax Apr 1, 2026
04c8aec
fix phpstan self-analysis errors: ignore property.notFound, add null …
shmax Apr 1, 2026
0195d7d
remove ignore
shmax Apr 1, 2026
95ae9e4
add property.notFound baseline entry for PHP < 8.0 (phpdoc-parser lac…
shmax Apr 1, 2026
2fd32f3
move property.notFound baseline entry to universal phpstan-baseline.n…
shmax Apr 1, 2026
89423b3
fix generic alias bare-usage resolution and data-provider parameter s…
shmax Apr 2, 2026
6c7322d
update baseline after rebase onto 2.1.x
shmax Apr 2, 2026
2321f59
add temp patches
shmax Apr 2, 2026
5726a40
use LateResolvableType
shmax Apr 2, 2026
6426482
remove
shmax Apr 2, 2026
fb9203b
lint
shmax Apr 2, 2026
d7fe98c
fix array vs list error
shmax Apr 2, 2026
e3aebe9
remove dead code
shmax Apr 2, 2026
ef31211
fold getRawGenericTypeAliasesUsage into getNonGenericObjectTypesWithG…
shmax Apr 15, 2026
106b589
update lock hash
shmax Apr 15, 2026
45ba91b
lint
shmax Apr 15, 2026
57a2395
remove unnecessary BC coverage from generic type alias resolution
shmax Apr 15, 2026
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
Binary file removed .idea/icon.png
Binary file not shown.
12 changes: 8 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,14 @@
"patches/DependencyChecker.patch",
"patches/Resolver.patch"
],
"symfony/console": [
"patches/OutputFormatter.patch"
]
}
"symfony/console": [
"patches/OutputFormatter.patch"
],
"phpstan/phpdoc-parser": [
"patches/TypeAliasTagValueNode.patch",
"patches/PhpDocParser.patch"
]
}
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions patches/PhpDocParser.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--- src/Parser/PhpDocParser.php
+++ src/Parser/PhpDocParser.php
@@ -1067,6 +1067,21 @@
$alias = $tokens->currentTokenValue();
$tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);

+ $templateTypes = [];
+ if ($tokens->tryConsumeTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET)) {
+ do {
+ $startLine = $tokens->currentTokenLine();
+ $startIndex = $tokens->currentTokenIndex();
+ $templateTypes[] = $this->enrichWithAttributes(
+ $tokens,
+ $this->typeParser->parseTemplateTagValue($tokens),
+ $startLine,
+ $startIndex,
+ );
+ } while ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA));
+ $tokens->consumeTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET);
+ }
+
// support phan-type/psalm-type syntax
$tokens->tryConsumeTokenType(Lexer::TOKEN_EQUAL);

@@ -1087,12 +1102,13 @@
}
}

- return new Ast\PhpDoc\TypeAliasTagValueNode($alias, $type);
+ return new Ast\PhpDoc\TypeAliasTagValueNode($alias, $type, $templateTypes);
} catch (ParserException $e) {
$this->parseOptionalDescription($tokens, false);
return new Ast\PhpDoc\TypeAliasTagValueNode(
$alias,
$this->enrichWithAttributes($tokens, new Ast\Type\InvalidTypeNode($e), $startLine, $startIndex),
+ $templateTypes,
);
}
}
47 changes: 47 additions & 0 deletions patches/TypeAliasTagValueNode.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--- src/Ast/PhpDoc/TypeAliasTagValueNode.php
+++ src/Ast/PhpDoc/TypeAliasTagValueNode.php
@@ -4,6 +4,7 @@

use PHPStan\PhpDocParser\Ast\NodeAttributes;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
+use function implode;
use function trim;

class TypeAliasTagValueNode implements PhpDocTagValueNode
@@ -15,15 +16,25 @@

public TypeNode $type;

- public function __construct(string $alias, TypeNode $type)
+ /** @var TemplateTagValueNode[] */
+ public array $templateTypes;
+
+ /**
+ * @param TemplateTagValueNode[] $templateTypes
+ */
+ public function __construct(string $alias, TypeNode $type, array $templateTypes = [])
{
$this->alias = $alias;
$this->type = $type;
+ $this->templateTypes = $templateTypes;
}

public function __toString(): string
{
- return trim("{$this->alias} {$this->type}");
+ $templateTypes = $this->templateTypes !== []
+ ? '<' . implode(', ', $this->templateTypes) . '>'
+ : '';
+ return trim("{$this->alias}{$templateTypes} {$this->type}");
}

/**
@@ -31,7 +42,7 @@
*/
public static function __set_state(array $properties): self
{
- $instance = new self($properties['alias'], $properties['type']);
+ $instance = new self($properties['alias'], $properties['type'], $properties['templateTypes'] ?? []);
if (isset($properties['attributes'])) {
foreach ($properties['attributes'] as $key => $value) {
$instance->setAttribute($key, $value);
2 changes: 1 addition & 1 deletion src/PhpDoc/PhpDocNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ public function resolveTypeAliasTags(PhpDocNode $phpDocNode, NameScope $nameScop
foreach ($phpDocNode->getTypeAliasTagValues($tagName) as $typeAliasTagValue) {
$alias = $typeAliasTagValue->alias;
$typeNode = $typeAliasTagValue->type;
$resolved[$alias] = new TypeAliasTag($alias, $typeNode, $nameScope);
$resolved[$alias] = new TypeAliasTag($alias, $typeNode, $nameScope, $typeAliasTagValue->templateTypes);
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/PhpDoc/Tag/TypeAliasTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\PhpDoc\Tag;

use PHPStan\Analyser\NameScope;
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\TypeAlias;

Expand All @@ -12,10 +13,14 @@
final class TypeAliasTag
{

/**
* @param TemplateTagValueNode[] $templateTagValueNodes
*/
public function __construct(
private string $aliasName,
private TypeNode $typeNode,
private NameScope $nameScope,
private array $templateTagValueNodes = [],
)
{
}
Expand All @@ -30,6 +35,8 @@ public function getTypeAlias(): TypeAlias
return new TypeAlias(
$this->typeNode,
$this->nameScope,
$this->templateTagValueNodes,
$this->aliasName,
);
}

Expand Down
54 changes: 54 additions & 0 deletions src/PhpDoc/TypeNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
use PHPStan\Type\StringType;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeAlias;
use PHPStan\Type\TypeAliasResolver;
use PHPStan\Type\TypeAliasResolverProvider;
use PHPStan\Type\TypeCombinator;
Expand All @@ -110,6 +111,7 @@
use PHPStan\Type\ValueOfType;
use PHPStan\Type\VoidType;
use Traversable;
use function array_filter;
use function array_key_exists;
use function array_map;
use function array_values;
Expand Down Expand Up @@ -829,6 +831,23 @@ static function (string $variance): TemplateTypeVariance {
return new ErrorType();
}

// Check for a generic type alias (e.g. MyList<string>) before falling through to
// class-based generic resolution.
$genericTypeAlias = $this->findGenericTypeAlias($typeNode->type->name, $nameScope);
if ($genericTypeAlias !== null) {
$templateNodes = $genericTypeAlias->getTemplateTagValueNodes();
$totalParams = count($templateNodes);
$requiredParams = count(array_filter($templateNodes, static fn ($tvn) => $tvn->default === null));
$providedArgs = count($genericTypes);

if ($providedArgs > $totalParams || $providedArgs < $requiredParams) {
return new ErrorType();
}

$appType = $genericTypeAlias->createApplicationType($this, $genericTypes);
return $appType->isResolvable() ? $appType->resolve() : $appType;
}

$mainType = $this->resolveIdentifierTypeNode($typeNode->type, $nameScope);
$mainTypeObjectClassNames = $mainType->getObjectClassNames();
if (count($mainTypeObjectClassNames) > 1) {
Expand Down Expand Up @@ -1361,4 +1380,39 @@ private function getTypeAliasResolver(): TypeAliasResolver
return $this->typeAliasResolverProvider->getTypeAliasResolver();
}

/**
* Returns the TypeAlias for $name if it is a generic (parameterised) type alias
* visible in the current $nameScope, or null otherwise.
*/
private function findGenericTypeAlias(string $name, NameScope $nameScope): ?TypeAlias
{
if ($nameScope->shouldBypassTypeAliases()) {
return null;
}

// Fast path: if the name isn't registered as a type alias in this scope, skip the
// more expensive ClassReflection::getTypeAliases() call. This also prevents a circular
// NameScope-building issue: getTypeAliases() can trigger FileTypeMapper::getResolvedPhpDoc()
// which calls getNameScope() — if we are already inside getNameScope() for this class,
// that throws NameScopeAlreadyBeingCreatedException, causing the class's ResolvedPhpDocBlock
// to be poisoned with an empty block and all its type aliases to be lost.
// UsefulTypeAliasResolver uses the same guard.
if (!$nameScope->hasTypeAlias($name)) {
return null;
}

$className = $nameScope->getClassNameForTypeAlias();
if ($className === null || !$this->getReflectionProvider()->hasClass($className)) {
return null;
}

$typeAliases = $this->getReflectionProvider()->getClass($className)->getTypeAliases();
if (!array_key_exists($name, $typeAliases)) {
return null;
}

$typeAlias = $typeAliases[$name];
return $typeAlias->isGeneric() ? $typeAlias : null;
}

}
12 changes: 12 additions & 0 deletions src/Rules/MissingTypehintCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\Type\Generic\GenericStaticType;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\Generic\TemplateTypeHelper;
use PHPStan\Type\GenericTypeAliasType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
Expand All @@ -27,6 +28,7 @@
use function array_filter;
use function array_keys;
use function array_merge;
use function array_unique;
use function count;
use function implode;
use function in_array;
Expand Down Expand Up @@ -115,6 +117,16 @@ public function getNonGenericObjectTypesWithGenericClass(Type $type): array
if ($type instanceof TemplateType) {
return $type;
}
if ($type instanceof GenericTypeAliasType) {
$missing = $type->getMissingRequiredParamNames();
if ($missing !== []) {
$objectTypes[] = [
sprintf('type alias %s', $type->getAliasName()),
implode(', ', array_unique($missing)),
];
}
return $traverse($type);
}
if ($type instanceof ObjectType) {
$classReflection = $type->getClassReflection();
if ($classReflection === null) {
Expand Down
1 change: 1 addition & 0 deletions src/Rules/PhpDoc/InvalidPhpDocVarTagTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public function processNode(Node $node, Scope $scope): array
->identifier('missingType.generics')
->build();
}

}

$escapedIdentifier = SprintfHelper::escapeFormatString($identifier);
Expand Down
24 changes: 24 additions & 0 deletions src/Type/Generic/TemplateTypeScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace PHPStan\Type\Generic;

use function sprintf;
use function str_starts_with;
use function strlen;
use function substr;

final class TemplateTypeScope
{
Expand All @@ -12,6 +15,11 @@ public static function createWithAnonymousFunction(): self
return new self(null, null);
}

public static function createWithTypeAlias(string $className, string $aliasName): self
{
return new self($className, '__typeAlias_' . $aliasName);
}

public static function createWithFunction(string $functionName): self
{
return new self(null, $functionName);
Expand Down Expand Up @@ -43,6 +51,22 @@ public function getFunctionName(): ?string
return $this->functionName;
}

/** @api */
public function isTypeAlias(): bool
{
return $this->functionName !== null && str_starts_with($this->functionName, '__typeAlias_');
}

/** @api */
public function getTypeAliasName(): ?string
{
if (!$this->isTypeAlias() || $this->functionName === null) {
return null;
}

return substr($this->functionName, strlen('__typeAlias_'));
}

/** @api */
public function equals(self $other): bool
{
Expand Down
Loading
Loading