Skip to content

LocalTypeAliasesRule: report invalid type alias names #497

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 3 commits into from
Apr 19, 2021

Conversation

jiripudil
Copy link
Contributor

follow-up of #460

@jiripudil jiripudil force-pushed the invalid-type-alias-check branch from bde92c7 to 730f4bd Compare April 18, 2021 19:40
$aliasNameResolvedType = $this->typeNodeResolver->resolve(new IdentifierTypeNode($aliasName), $nameScope->bypassTypeAliases());
if (!($aliasNameResolvedType instanceof ObjectType)
&& !($aliasNameResolvedType instanceof TemplateType) // aliases take precedence over type parameters, this is reported by other rules using TemplateTypeCheck
|| in_array($aliasName, ['self', 'parent'], true)
Copy link
Member

Choose a reason for hiding this comment

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

  1. The parentheses around instanceof aren't necessary, !$foo instanceof Foo works
  2. But I'd like parentheses disambiguating $foo && $bar || $baz, I honestly don't know what happens :) So there should be either ($foo && $bar) || $baz or $foo && ($bar || $baz)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ha, I guess that's some quirk of codesniffer then. When I had the disambiguating parentheses there, it reported "Useless parentheses" and since removing them helped, I didn't know it meant those parentheses around instanceof.

@ondrejmirtes
Copy link
Member

BTW do we have covered also @phpstan-import-type UserAddress from User as int?

@jiripudil
Copy link
Contributor Author

BTW do we have covered also @phpstan-import-type UserAddress from User as int?

Now we do :) it also helped make the parentheses less messy

@jiripudil jiripudil force-pushed the invalid-type-alias-check branch from b9fffc8 to ea781c7 Compare April 19, 2021 07:03
@jiripudil
Copy link
Contributor Author

I had to add back the behaviour removed in e381f0b: the resolution in the rule here actually has to bypass type aliases, otherwise it would report false positives for every alias which references a non-ObjectType 😟

@jiripudil jiripudil requested a review from ondrejmirtes April 19, 2021 11:28
@ondrejmirtes ondrejmirtes merged commit 592d3d1 into phpstan:master Apr 19, 2021
@ondrejmirtes
Copy link
Member

Thank you!

@jiripudil jiripudil deleted the invalid-type-alias-check branch April 19, 2021 12:01
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