phpstan-drupal: Reflection error: Drupal\Tests\PhpunitCompatibilityTrait not found
How is drupal-check installed?
drupal-check is installed as a dependency to my project
Environment:
- OS: macOS
- PHP Version: 7.3
- Drupal core: 9.0.x
Describe the bug Reflection error occurs when using multiple arguments to test different files. $ vendor/bin/drupal-check --deprecations --no-progress --drupal-root=…/drupal --exclude-dir=vendor cookiebot_consent.module src/Event/LibrariesEvent.php
This issue was partially fixed in https://github.com/phpstan/phpstan/issues/3522 but still occurs when using different files as arguments.
cookiebot_consent.module
$event = new LibrariesEvent($extension);
src/Event/LibrariesEvent.php
use Symfony\Contracts\EventDispatcher\Event;
class LibrariesEvent extends Event {
Symfony\Contracts\EventDispatcher\Event
namespace Symfony\Contracts\EventDispatcher;
use Psr\EventDispatcher\StoppableEventInterface;
if (interface_exists(StoppableEventInterface::class)) {
class Event implements StoppableEventInterface
{
}
} else {
class Event
{
}
}
Console output
------ --------------------------------------------------------------------
Line cookiebot_consent.module
------ --------------------------------------------------------------------
Reflection error: Psr\EventDispatcher\StoppableEventInterface not
found.
💡 Learn more at https://phpstan.org/user-guide/discovering-symbols
------ --------------------------------------------------------------------
------ --------------------------------------------------------------------
Line src/Event/LibrariesEvent.php
------ --------------------------------------------------------------------
Reflection error: Psr\EventDispatcher\StoppableEventInterface not
found.
💡 Learn more at https://phpstan.org/user-guide/discovering-symbols
52 Reflection error: Psr\EventDispatcher\StoppableEventInterface not
found.
💡 Learn more at https://phpstan.org/user-guide/discovering-symbols
------ --------------------------------------------------------------------
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 31 (21 by maintainers)
Commits related to this issue
- Use phpstan/phpstan 0.12.42 due to bug https://github.com/mglaman/drupal-check/issues/186 — committed to arlina-espinoza/apigee-edge-drupal by arlina-espinoza 4 years ago
- Use phpstan/phpstan 0.12.42 due to bug https://github.com/mglaman/drupal-check/issues/186 (#491) — committed to apigee/apigee-edge-drupal by arlina-espinoza 4 years ago
- [#246] Use phpstan/phpstan 0.12.42 due to bug https://github.com/mglaman/drupal-check/issues/186 — committed to arlina-espinoza/apigee-m10n-drupal by arlina-espinoza 4 years ago
- HUB-698 - phpstan/phpstan small downgrade to "0.12.42" to avoid false positivies and errors with upgrade_status deprecation discovery. More info: https://www.drupal.org/project/upgrade_status/issue... — committed to UH-StudentServices/student_guide by deleted user 4 years ago
- Workaround downgrade to phpstan 0.12.42 After the latest release of phpstan/phpstan 0.12.43 we run into reflection errors. Issue has been reported on the mglaman/drupal-check project: mglaman/drupa... — committed to district09/php_package_qa-drupal by MPParsley 4 years ago
- [#246] Add CI testing matrix against D8 and D9. (#249) * [#246] Add CI testing matrix against D8 and D9. * [#246] Remove wikimedia/composer-merge-plugin from CI as it's causing double updates and ... — committed to apigee/apigee-m10n-drupal by arlina-espinoza 4 years ago
Since we updated phpstan/phpstan to 0.12.43 we’re seeing a lot of these Reflection errors when running drupal-check, here’s another one:
Looks like something changed in ondrejmirtes/better-reflection that’s triggering this.
I found the error! It’s due to an alias defined for PHPUnit compatibility between PHPUnit 6 and 7. I also double checked, it’s not due to it being defined dynamically.
See: https://git.drupalcode.org/project/drupal/-/blob/8.8.x/core/tests/Drupal/Tests/PhpunitCompatibilityTrait.php#L10
When I comment out that line of code and target the trait directly, non-aliased, everything works fine: ie
use \Drupal\TestTools\PhpUnitCompatibility\PhpUnit7\TestCompatibilityTrait;
.When I modify the statement to be hardcoded and not rely on
RunnerVersion::getMajor()
it still breaks. So there is something about defining the trait alias that makes reflection break.#144 fixes the tests here and uncovers this bug