Skip to content
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

new-keyword-redefinable causes a Fatal error: Uncaught ReflectionException: Class "*" does not exist in #126

Closed
wvega opened this issue Feb 4, 2022 · 3 comments
Assignees

Comments

@wvega
Copy link
Contributor

wvega commented Feb 4, 2022

Hey everyone! Thank you so much for all your work on Patchwork. I use it for most of the tests I write, either through https://github.com/10up/wp_mock or using Patchwork's API directly.

Recently I started experimenting with the new-keyword-redefinable and ran into an issue. It seems that Patchwork is unable to resolve some namespaces correctly and as a result trying to run any test file fails with something like:

Fatal error: Uncaught ReflectionException: Class "PHPUnit\TextUI\Timer" does not exist in vendor/antecedent/patchwork/src/CallRerouting.php:597

I was able to reproduce that particular error on a fresh project using Patchwork and PHPUnit only. You can follow these steps to give it a try:

  1. Clone https://github.com/wvega/patchwork-namespace-resolution-issue
  2. Run composer install
  3. Run vendor/bin/phpunit --bootstrap=tests/bootstrap.php tests
  4. You should see the error now

Then, if you change new-keyword-redefinable to false and run the phpunit command again, tests run normally.

The correct namespace for the Timer class is SebastianBergmann\Timer\Timer. PHPUnit\TextUI is the namespace of the TestRunner class, the one that instantiates a Timer.

I did some debugging and I think it all happens in the resolveName() function:

$name = '\\' . ltrim(getNamespaceAt($s, $pos) . '\\' . $name, '\\');

At that point I can see SebastianBergmann\Timer\Timer in the $uses array:

...
            [SebastianBergmann\Timer\Timer] => SebastianBergmann\Timer\Timer
...

However, the resolveName() function has no way to retrieve that value. Should the array have [Timer] => SebastianBergmann\Timer\Timer instead?

If that's the case then maybe the problem is somewhere in collectUseDeclarations() but I didn't get to explore those functions.

I'm running PHP 8.1. Please let me know if I can provide any additional information.

@antecedent
Copy link
Owner

antecedent commented Feb 4, 2022

Thank you; it seems that you got to the root of the issue! This seems to be related to #120 #104. Since that update, qualified names now come as single tokens, breaking the resolution mechanism that you described.

@antecedent antecedent self-assigned this Feb 4, 2022
antecedent added a commit that referenced this issue Feb 6, 2022
@antecedent
Copy link
Owner

Please try version 2.1.20, this should now be fixed in it.

@wvega
Copy link
Contributor Author

wvega commented Feb 7, 2022

@antecedent thank you for looking into this issue so quickly. 2.1.20 fixed the problem!

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

No branches or pull requests

2 participants