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
Autoload source locator startLine check can cause a file to not be autoloaded #4194
Comments
I triaged this during a live stream code session with debugging if that would help explain the issue. |
Hi, I don't fully understand this problem, and I'm not sure that the proposed fix is correct. You're saying:
I don't follow the "This makes sense as line 10 has logic conditions." part. Class reflection always points to the class definition, i.e. line 17. Verified here: https://3v4l.org/uTXdp What's actually on line 10 in the original trait is the class_alias call. But I wasn't able to replicate that issue either, both calls point to line 3 here: https://3v4l.org/qFuiS So is that something specific to aliasing traits? 🤔 |
So, I'll work on a test and explain more later today. But one thing I did realize: the file which caused issues only has problems with the autoload source locator. If it's discovered with I haven't looked yet, but I wonder if that locator has the same checks on start line? And is that why this has been near impossible to reproduce? I wrote tests months ago for this but could never reproduce the problem. But the tests don't try to autoload the fixture files, which explains why the bug didn't surface. It's specifically in the autoload locator. I'll work on this more today. |
Well, I just don't get it. I made an updated example: https://3v4l.org/IfhZY and https://3v4l.org/5HOOW. Part of my feels like it's not reproducible unless using separate files. Because the reflection from PHP had the start line not at the I do know the bug we're experiencing with this funky aliased trait is fixed when discovered with It was added in this commit: phpstan/phpstan-src@e2fe1ef. I don't know why this randomly started breaking, honestly. That trait has been there for quite some time.
The weird part is that things stopped working at 0.12.43. So I'm going to comb over phpstan/phpstan-src@0.12.42...0.12.43 and try to see. I'm really at a complete loss. Like was it something in \PHPStan\Reflection\BetterReflection\SourceLocator\FileNodesFetcher::fetchNodes and how it's parsed? I think I'm getting closer. |
Going to apologize for the comment spam – but I'm trying to deep dive this between calls 😬 . Got to So, I'm guessing it is something due to |
I'll close this in favor of the php-parser issue: nikic/PHP-Parser#738 |
Hi, thank you for taking the time, I appreciate it :) Don't worry about spamming here, I have a similar procedure when investigating issues :D |
It looks like it may be due to the handling of optional attributes in traits. I finally have a failing test somewhere for this: nikic/PHP-Parser#739. Hopefully, we can find a fix that gets released, PHPStan can use it, and phpstan-drupal is finally unblocked 🥳 |
Nothing stands out to me in the trait file, what are optional attributes in traits? |
I'm guessing it's something in the parsing of PHP 8 attributes. I just took an assumption from nikic's comment
So there's nothing wrong with the trait, just the parsing of it and supporting PHP 8 attributes. I think. And I think I know where. |
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. |
Bug report
If a ClassLike node – Traits, Class, Interface – has any code before the definition it causes a start line mismatch and the file is not located. For example, a file may have a conditional to set a class alias due to various reasons.
This was uncovered while working on mglaman/phpstan-drupal#149.
The start line for the trait is line 17, however PHP internal reflection says the start line of the file is 10. This makes sense as line 10 has logic conditions. But PHPStan exits due to the fact the identifier starts on line 17. So PHPStan should properly check against the node name start line not the file logic start time.
Should look more like
Code snippet that reproduces the problem
Due to it relating to autoloading, I cannot think of a way to test on the Playground. But here's the code in general.
Expected output
It says the symbol is not found as the autoloader exits due to mismatched start lines. It should find the file since it loaded the source.
The text was updated successfully, but these errors were encountered: