- Notifications
You must be signed in to change notification settings - Fork 1.5k
🔥 Fix PHPCS native autoloader & class recognition in PHPCS 3.x. #1452
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 PHPCS native autoloader & class recognition in PHPCS 3.x. #1452
Conversation
While working on making a custom standard compatible with PHPCS 3.x using `class_alias()`-es, a number of sniff files were getting misidentified by the autoloader causing PHPCS to choke. This change fixes the issue I was encountering by using the the `$newClasses` diff in reverse. While debugging this I notices copy/paste artefacts in the subsequent checks for new interfaces and traits. Both of which were - incorrectly - being compared against the `$classes` variable. I've taken the liberty to rename the local variables related to this as well to make the distinction clearer.
The I need to give the main change more thought and more testing. I previously just grabbed the last class in the list and assumed that was the loaded one, but that was causing issues with HHVM so I changed to looking from the front of the list and finding the first unknown class. The commit is here: 12bcb35 I don't have more detail on the commit, but I'm pretty sure HHVM was just dumping classes in whatever order it felt like so I couldn't rely on the order in any way. |
I suspect the other issue I reported #1453 might be loosely related to this as well. I'll see if I can do some more debugging and think of a more stable solution. |
Note: HHVM version at the time of failure was
|
So I bit of debugging on a newer HHVM ( This is vanilla PHP loading a couple of the PHPCompat classes:
And this is HHVM:
So it looks as if |
I had to swap around the trait and interface checks to make HHVM work properly again, but I think adding the array_reverse() to the class list is looking ok for HHVM support. The key problem I was previously solving is that the actual included class could pop up in the middle of the list, so I cant rely on the first or last class name to be correct. But going through and looking for the first class we don't yet know about seems to work in HHVM both forwards and backwards, especially given the aliases don't appear in the list. So I'll commit the changes I've made to get HHVM and PHPCompat support going again and we'll see what travis says. |
Looks like it worked. I'm assuming that's going to work for you given it's pretty much what you had, but can you check it for me please? |
Thanks for looking into this. I'll test this later today. Looks like your commit is missing the additional bugfix I added though:
The variable rename was just to make it more obvious those blocks work with different data and can be left out, but I kind of think you intended to do the |
if ($className === null) { | ||
$newClasses = array_reverse(array_diff(get_declared_traits(), $classes)); | ||
foreach ($newClasses as $name) { | ||
$newTraits = array_reverse(array_diff(get_declared_traits(), $traits)); |
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.
⬆️ The $traits
here
if ($className === null) { | ||
$newClasses = array_reverse(array_diff(get_declared_interfaces(), $classes)); | ||
foreach ($newClasses as $name) { | ||
$newInterfaces = array_reverse(array_diff(get_declared_interfaces(), $interfaces)); |
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.
⬆️ The $interfaces
here
Tested & found working. Thanks! P.S.: not closing the issue yet as my remark about the trait/interface |
Unbelievable. I looked at the code so many times and never saw that. It's amazing nothing ever broke. Thanks a lot for pointing that out again. |
Happens to the best of us ;-) Anyway, as the additional commit has now been made, I believe this PR can be closed. Agreed ? |
Agreed :) |
While working on making a custom standard compatible with PHPCS 3.x using
class_alias()
-es, a number of sniff files were getting misidentified by the autoloader causing PHPCS to choke.This change fixes the issue I was encountering by using the the
$newClasses
diff in reverse.While debugging this I noticed copy/paste artifacts in the subsequent checks for new interfaces and traits. Both of which were - incorrectly - being compared against the
$classes
variable.I've taken the liberty to rename the local variables related to this as well to make the distinction clearer.