-
Notifications
You must be signed in to change notification settings - Fork 473
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
Fix support for classes named after pseudotypes in phpdoc #365
Conversation
@ondrejmirtes any guidance about where I should add a test covering this ? |
Note that once this is released, the doc at https://phpstan.org/writing-php-code/phpdocs-basics#classes-named-after-internal-php-types should be updated to stop talking about |
Hi, I'd be pretty careful with this. It might be a BC break for some people. I'd rather be it this was a TypeNodeResolverExtension enabled via bleedingEdge in Also, we could (and should) do it for more similar types like |
@ondrejmirtes AFAICT, If this approach about preferring an existing class over the pseudotype for valid class names works for you, applying it to |
I'd like some additional logic:
You can test this in NodeScopeResolver::testFileAsserts by adding a new data provider and see how the existing data providers work like. |
do you have any example for that ? |
TypeNodeResolverExtension: https://github.com/phpstan/phpstan-phpunit/blob/master/src/PhpDoc/PHPUnit/MockObjectTypeNodeResolverExtension.php
But feel free to implement and test the logic as it is. I have a feeling that the NodeScopeResolverTest will not see the stuff registered behind bleedingEdge tag right now. |
@ondrejmirtes what is the expected order for these 2 rules ? What should I do if a file without namespace contains a use statement ? |
Use the use statement 😊 |
497b34c
to
d952fa1
Compare
@ondrejmirtes I pushed an update implementing the new logic. The tests are not yet done though. |
Please don't do that for the names that are affected by this warning: https://3v4l.org/f1UF7 |
ah, I was not aware of that warning on PHP 8. I tried defining a class but not typehinting it. |
@stof Hi, just asking - when will you have time to finish this PR? Thanks. |
@ondrejmirtes I'll try to do it this evening |
Looks like and it still treat that as a class typehint in all PHP versions. The only impacted thing is how you write the typehint when using such class name (you cannot use an unqualified lowercase name) |
@ondrejmirtes given that the warning only applies to lowercase names, should I really disqualify them entirely ? |
d45166a
to
3526237
Compare
@ondrejmirtes I added tests covering this. But the tests for classes existing in the same namespace (without use statements) don't work because it looks like the |
@stof It should be fine, you need to run Also, please use the modern style tests in |
@ondrejmirtes I updated to the modern style, and I added a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love this, I really really do. Looks like it will solve problems people have without actually breaking anything.
One more thing to fix and I'll click the Merge button :)
src/PhpDoc/TypeNodeResolver.php
Outdated
@@ -263,6 +305,25 @@ private function resolveIdentifierTypeNode(IdentifierTypeNode $typeNode, NameSco | |||
return new ObjectType($stringName); | |||
} | |||
|
|||
private function tryResolvePseudoTypeClassType(IdentifierTypeNode $typeNode, NameScope $nameScope): ?Type | |||
{ | |||
if (isset($nameScope->getUses()[strtolower($typeNode->name)])) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of this leaking of NameScope internals, I'd add NameScope::hasUseAlias()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ondrejmirtes good idea. This is done.
The non-standard pseudotype
int|float
will be used only if the resolved class does not exist.Closes phpstan/phpstan#4039