Since this error now has bit me twice, and I had to search for a while to find out how I fixed it the first time, I decided to just write a quick blog post about it instead. And this is indeed it.
After updating phpstan on a Drupal project, and removing the configuration option autoload_files I suddenly started getting this polite little error message on my static analysis continuous integration runs:
Reflection error: Drupal\Tests\PhpunitCompatibilityTrait not found.
💡 Learn more at https://phpstan.org/user-guide/discovering-symbols
To fix this, I simply added the trait to the new configuration property scanFiles:
scanFiles:
- %currentWorkingDirectory%/drupal/core/tests/Drupal/Tests/PhpunitCompatibilityTrait.php
If your project has Drupal in another folder than "drupal", simply substitute that with "web", "docroot" or whatever your project dictates.
That will serve for me as a reminder to next time. And hopefully to you. Speaking of hope, here is an animated gif called "hope".