Skip to content

Allow @phpstan-type and @phpstan-import-type above functions and methods - #7295

Merged
ondrejmirtes merged 3 commits into
2.3.xfrom
function-type-aliases
Dec 3, 2026
Merged

ondrejmirtes merged 3 commits into
2.3.xfrom
function-type-aliases

Conversation

@ondrejmirtes

@ondrejmirtes ondrejmirtes commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Type aliases can now be declared (@phpstan-type, @psalm-type, @phan-type) and imported (@phpstan-import-type, @psalm-import-type) above functions and methods.

  • An alias applies to that function's PHPDoc and its body: inline @var tags, closures, and anonymous classes inside it. Once the function ends, the alias is unknown again.
  • It shadows a class alias, and an alias from an enclosing function, of the same name. An anonymous class's own aliases shadow it in turn.
  • An alias may mention the templates of the function and of its class. Template bounds and defaults may name the alias (@template V of Box = Box).
  • An alias imported from the enclosing class keeps that class's templates. Imported from anywhere else, they resolve to their bounds, as with class-level imports.

The new FunctionTypeAliasesRule (level 0) reports for these aliases what LocalTypeAliasesRule reports for class aliases. It also reports an alias, or a generic alias's parameter, that shadows a template in scope.

Also in this PR:

  • An invalid type in an alias body now carries the reason as a tip, the same way the PHPDoc tag rules show it. For example: "Type alias Pair<A, B> takes 2 type arguments, 1 given."
  • The never-ending expansion message now names the alias that refers, instead of the one the cycle was detected at.

Importing from a function or method (from function doFoo) is not part of this PR.

Refs phpstan/phpstan#9164

Issue bot shows these issues fixed; their reproducers are added as regression tests:

Closes phpstan/phpstan#13315
Closes phpstan/phpstan#12208
Closes phpstan/phpstan#9424

The aliases are seen by the PHPDoc and the body of the function or method,
including inline @var tags, closures and anonymous classes inside it, and
are unknown once it ends. They shadow the aliases of the class and of the
functions around them, and an anonymous class's own aliases shadow them.
They may mention the templates of the function and of its class, and the
bounds and defaults of those templates may name them.

FunctionTypeAliasesRule reports for them what LocalTypeAliasesRule reports
for class aliases, and also an alias or an alias parameter that shadows a
template in scope.

Amp-Thread-ID: https://ampcode.com/threads/T-01a238be-4c1b-73d6-9750-3146e02793ea
The error for an invalid type in a type alias carries the reason of the
ErrorType as a tip, like the PHPDoc tag rules do for unresolvable types,
e.g. that a generic alias is given too few type arguments, or an argument
outside the bound of its parameter.

Amp-Thread-ID: https://ampcode.com/threads/T-01a238be-4c1b-73d6-9750-3146e02793ea
The message named the alias the cycle was reached again at instead of the
one whose body passes the growing argument, so for Node<T> referring to
Link<list<T>> it said that Link<T> refers to Link<T>.

Amp-Thread-ID: https://ampcode.com/threads/T-01a238be-4c1b-73d6-9750-3146e02793ea
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant