Skip to content

support for generic params on alias types - #297

Merged
ondrejmirtes merged 4 commits into
phpstan:2.6.xfrom
shmax:generics-on-phpstan-types
Dec 1, 2026
Merged

ondrejmirtes merged 4 commits into
phpstan:2.6.xfrom
shmax:generics-on-phpstan-types

Conversation

@shmax

@shmax shmax commented Apr 2, 2026 •

Copy link
Copy Markdown
Contributor

Add Generic Type Parameters to Type Aliases

This PR adds support for generic type parameters on @phpstan-type aliases, including multiline syntax support.

See: phpstan/phpstan-src#5378

Features

Generic Type Parameters on Type Aliases

Type aliases can now declare template parameters directly on the alias definition:

/**
 * @phpstan-type Wrapper<T> T
 * @phpstan-type Pair<TFirst, TSecond> array{first: TFirst, second: TSecond}
 * @phpstan-type Collection<T of object> list<T>
 * @phpstan-type WithDefault<T = string> T
 */

Multiline Syntax Support

Complex generic type aliases can be formatted across multiple lines for improved readability:

/**
 * @phpstan-type Response<
 *     TData,
 *     TError of \Throwable
 * > array{
 *     success: bool,
 *     data: TData|null,
 *     error: TError|null
 * }
 */

/**
 * @phpstan-type Page<
 *     TItem of object, // what the page lists
 *     TCursor of int|string = int, // where the next page starts
 * > array{items: list<TItem>, next: TCursor|null}
 */

Use Cases

This enables more flexible and reusable type definitions:

/**
 * @phpstan-type Result<T, E = \Exception> array{ok: bool, value: T|null, error: E|null}
 */
class ResultHandler
{
    /**
     * @param Result<User, AuthException> $result
     */
    public function handle(array $result): void
    {
        // ...
    }
}

Technical Details

AST Changes

  • TypeAliasTagValueNode now includes a $templateTypes property containing an array of TemplateTagValueNode[]
  • The parser recognizes <...> syntax immediately after the alias name
  • Supports all template features: bounds (of), defaults (=), and multiple parameters

Parser Behavior

The parser now:

  • Reads the <...> after the alias name with TypeParser::parseTemplateTypes(), the same method that reads the templates of a callable type, so the templates may be written one per line, with comments and a trailing comma
  • Keeps reading the type alias without templates the same way as before

Printer Support

The Printer class outputs generic type aliases with template parameters in both regular and format-preserving modes. In format-preserving mode, adding, removing, or changing a template keeps the formatting of the rest of the tag.

Testing

Added comprehensive test coverage:

  • Single template parameter
  • Multiple template parameters
  • Bounded template parameters (T of object)
  • Default template parameters (T = string)
  • Multiline formatting with whitespace
  • Integration with existing type alias features
  • Trailing commas and comments in the template list
  • Invalid template lists (Foo<T T, Foo<>)
  • Line and index attributes of the template nodes
  • Format-preserving printing of added, removed, and changed templates
  • The grammar in doc/grammars for the fuzzer tests

All existing tests pass, ensuring backward compatibility.

This aligns PHPDoc type alias syntax more closely with TypeScript, Java, and other languages that support generic type declarations.

@shmax
shmax marked this pull request as draft April 2, 2026 15:57
@shmax
shmax marked this pull request as draft April 2, 2026 15:57
@shmax
shmax marked this pull request as ready for review April 2, 2026 16:34
@ondrejmirtes

Copy link
Copy Markdown
Member

I'm sorry, I'm sure you put some effort in, but the "example (before)" with @template makes me think of AI slop. This was never a valid work around. Type aliases are resolved before generics. Care to explain?

@shmax

shmax commented Apr 15, 2026

Copy link
Copy Markdown
Contributor Author

Ah, you're right. I had stuff like that in my code, but it was more of a "doesn't workaround". Removed, thank you. Any issues with the code?

@shmax

shmax commented Apr 26, 2026

Copy link
Copy Markdown
Contributor Author

@ondrejmirtes This has been sitting for over 10 days. Are we waiting on something?

@shmax

shmax commented Jun 16, 2026

Copy link
Copy Markdown
Contributor Author

@ondrejmirtes this has been sitting for 2 and a half months. Please review.

@ondrejmirtes ondrejmirtes mentioned this pull request Dec 1, 2026
@ondrejmirtes
ondrejmirtes force-pushed the generics-on-phpstan-types branch from 0183817 to a38811e Compare December 1, 2026 15:31
@ondrejmirtes
ondrejmirtes changed the base branch from 2.3.x to 2.6.x December 1, 2026 15:31
The templates are read by TypeParser::parseTemplateTypes(), which allows comments and a trailing comma, and the grammar describes them for the fuzzer. TemplateTypes is declared before the type so that format-preserving printing visits them in token order.

Amp-Thread-ID: https://ampcode.com/threads/T-01a23235-ed63-7495-8268-50e3db8a9e95
@ondrejmirtes
ondrejmirtes force-pushed the generics-on-phpstan-types branch from a38811e to da88cc2 Compare December 1, 2026 15:38
@ondrejmirtes

Copy link
Copy Markdown
Member

Next PHPStan release brings a lot of typesystem improvements, like class-string-map, recursive template bounds, and recursive type aliases. So generic type aliases fit into that too.

Thank you!

@ondrejmirtes
ondrejmirtes merged commit 516c0ce into phpstan:2.6.x Dec 1, 2026
64 of 67 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants