-
Notifications
You must be signed in to change notification settings - Fork 61
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
Native types shouldn't try to use the current namespace #117
Comments
This is a parser. It does not do semantic resolution, it just returns AST. PHPStan then corrently resolves types in TypeNodeResolver.
That's not unfortunate. That's simply wrong. PSR-4 does not allow to mix classes and functions in the same directory, i.e. to safe library needs to fix their autoloading defintion as suggested in thecodingmachine/safe#253 (comment) |
I guess this issue is just misfiled and should be in phpstan/phpstan. But try phpstan/phpstan:1.6.x-dev and enable bleedingEdge https://phpstan.org/blog/what-is-bleeding-edge, there should be no side effects of autoloading. |
Additionaly Larastan is also wrong to assume that |
Ok, thank you very much for your answers and sorry for filing the bug on the wrong place. I assumed it was the parser who read infered @ondrejmirtes Do you want me to reopen this on the phpstan repo? I'll test the latest version as soon as I get some time off work. Thanks to both for the quick replies! |
Also, just for the record, |
The "wrong resolution" part of the problem is literally one line above the link you posted https://github.com/nunomaduro/larastan/blob/v2.1.4/src/Types/GenericEloquentBuilderTypeNodeResolverExtension.php#L36. If Larastan fix this issue then there would be no attempt to load
They use same directory which PSR-4 does not support. Repeated call to |
And now I wonder what is actually the best way Larastan can fix the resolution issue. Maybe PHPStan should expose a constant with all known keywords, so Larastan could replace if ($innerTypeNode instanceof IdentifierTypeNode
&& $this->provider->hasClass($nameScope->resolveStringName($innerTypeNode->name))
&& (new ObjectType(Model::class))->isSuperTypeOf(new ObjectType($nameScope->resolveStringName($innerTypeNode->name)))->yes()
) {
...
} with sth like if ($innerTypeNode instanceof IdentifierTypeNode
&& !isset(TypeNodeResolver::KEYWORDS[strtolower($innerTypeNode->name)]
&& $this->provider->hasClass($nameScope->resolveStringName($innerTypeNode->name))
&& (new ObjectType(Model::class))->isSuperTypeOf(new ObjectType($nameScope->resolveStringName($innerTypeNode->name)))->yes()
) {
...
} |
@JanTvrdik Thanks for the explanation, I'm still finding my way around phpstan and AST, so I wasn't 100% sure what the code was doing. The one thing I still don't understand is (and sorry if I'm missing something obvious), there are no classes (afaics) on the Sorry to waste your time, I'm learning a lot in the journey trying to debug and fix this issue. |
@j3j5 Do you understand that calling
The Safe's autoloading definition is incorrect, because it configures Composer autoloader in a way that makes it possible for some calls of The Safe library needs to
|
Yes, I understand what you mean now, I misinterpreted point number 4 earlier as being the responsibility of the autoloader implementation but indeed, it's as much responsibility of the class/file being loaded using PSR-4. I'm going to try to implement this suggestion on a PR for them. Again, thanks for you time. I appreciate your responses. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Hi,
It seems phpdoc-parser tries to use the files' namespace even for native types. Not sure how to best explain this, let me do my best.
There this really odd issue on the Safe library where phpstan + larastan would throw an error on any library using the Safe package. At first I thought the error was on larastan but after they fixed what I thought was the root cause, the error was still there, so after some more debugging (thank $DEITY for xdebug) I found out that the problem was on the library (or on phpdoc-parser, it could be fixed on both places).
Long story short, the library is basically a bunch of files redeclaring native php functions, the issue is that whenever there's a phpdoc on one of their files using
@param array
or@return array
, since all function files are namespaced underSafe
, the parser tries to use the typeSafe\array
, which unfortunately has a PSR4 match on theirarray.php
file. The bug appeared when the library was used together with larastan because larastan checks if any of the parameters is a subclass of one of Laravel's Eloquent classes on one of their extensions, triggering the second loading of the same file (doing(new ObjectType(Model::class))->isSuperTypeOf(new ObjectType($nameScope->resolveStringName('Safe\array')))->yes()
). I (erroneously) thought that adding\
to the array will force it to be on the global namespace but it turns out that\array
is not a valid type for phpstan.Anyway, I believe the native types from PHP should take precedence when being parsed (same than the actual PHP behaviour), maybe with some sort of list to be compared with when parsing the types from
@param
and@return
, although I'm not fully familiar with the implementation to make a proper suggestion.Please, let me know if there are any doubts so I can explain further, not my best day today 😛
The text was updated successfully, but these errors were encountered: