Skip to content

[TypeInfo] Add type alias support #59804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2025
Merged

Conversation

mtarld
Copy link
Contributor

@mtarld mtarld commented Feb 18, 2025

Q A
Branch? 7.3
Bug fix? no
New feature? yes
Deprecations? no
Issues
License MIT

Add type aliasing support in TypeContext and StringTypeResolver, which enables the read of @phpstan-type and @phpstan-import-type.

With this PR, the following code will be properly understood by TypeInfo (before, it used to throw an UnhandledException):

/**
 * @phpstan-type TypeAlias = array<string, list<bool>>
 */
final class Dummy
{
    /**
     * @var TypeAlias
     */
    public mixed $aliasedType;
}

@carsonbot carsonbot added this to the 7.3 milestone Feb 18, 2025
@mtarld mtarld force-pushed the feat/type-aliases branch 2 times, most recently from 34dbc2f to 14c1e1e Compare February 18, 2025 16:01
@nicolas-grekas
Copy link
Member

Note that the portable syntax (= understood by phpstan & psalm & phpstorm) is this one:
@phpstan-type TypeAlias = array<string, list<bool>>

@Neirda24
Copy link
Contributor

Note that the portable syntax (= understood by phpstan & psalm & phpstorm) is this one: @phpstan-type TypeAlias = array<string, list<bool>>

But it is not how people usually write it I think. is it ?

@nicolas-grekas
Copy link
Member

They have to if they want support from their tools...

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test case using psalm-type just to confirm phpstan's parser handles both?

@Neirda24
Copy link
Contributor

They have to if they want support from their tools...

hmmm IMO when this PR is merged it must not enforce one specific way of writing things with these tools. It should support both with and without =. Current projects / libraries that uses these kind of features don't always use the =. Requiring it would restrict people from using the TypeInfo on tools that did not follow the symfony requirements.

@Neirda24
Copy link
Contributor

They have to if they want support from their tools...

More on this. The documentation https://phpstan.org/writing-php-code/phpdoc-types#local-type-aliases doesn't even mention this. I asked Ondrej a while back on symfony's slack about this but he didn't tought it was worth adding it to documentation.

@mtarld
Copy link
Contributor Author

mtarld commented Feb 19, 2025

@Neirda24, @nicolas-grekas, I updated the code/tests to make it work for @psalm-*, @phpstan-*, with and without the = so that it matches most of the use cases.

@nicolas-grekas
Copy link
Member

Thank you @mtarld.

@nicolas-grekas nicolas-grekas merged commit 77b8358 into symfony:7.3 Feb 19, 2025
9 of 11 checks passed
@mtarld mtarld deleted the feat/type-aliases branch February 19, 2025 16:27
@Neirda24
Copy link
Contributor

So fast ! Thank you @mtarld !

@fabpot fabpot mentioned this pull request May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants