Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
When running PHPStan check I'm getting the following error:
Child process error (exit code 255): PHP Warning: file_get_contents(phar:///var/www/html/vendor/phpstan/phpstan/phpstan/vendor/phpunit/phpunit/src/Framework/TestCase.php): failed to open stream: phar error: "vendor/phpunit/phpunit/src/Framework/TestCase.php" is not a file in phar "/var/www/html/vendor/phpstan/phpstan/phpstan" in /var/www/html/core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit8/ClassWriter.php on line 43 Warning: file_get_contents(phar:///var/www/html/vendor/phpstan/phpstan/phpstan/vendor/phpunit/phpunit/src/Framework/TestCase.php): failed to open stream: phar error: "vendor/phpunit/phpunit/src/Framework/TestCase.php" is not a file in phar "/var/www/html/vendor/phpstan/phpstan/phpstan" in /var/www/html/core/tests/Drupal/TestTools/PhpUnitCompatibility/PhpUnit8/ClassWriter.php on line 43 PHP Fatal error: Declaration of Drupal\Tests\UnitTestCase::setUp() must be compatible with PHPUnit\Framework\TestCase::setUp(): void in /var/www/html/core/tests/Drupal/Tests/UnitTestCase.php on line 41 Fatal error: Declaration of Drupal\Tests\UnitTestCase::setUp() must be compatible with PHPUnit\Framework\TestCase::setUp(): void in /var/www/html/core/tests/Drupal/Tests/UnitTestCase.php on line 41
See https://dispatcher.drupalci.org/job/drupal8_contrib_patches/36806/Phpstan/
The same happens locally
Seems like the following code:
$reflector = new \ReflectionClass($autoloader);
$vendor_dir = dirname($reflector->getFileName(), 2);
does not work when executed within the other reflection (by PHPStan) - my assumption needs to be verified.
Proposed resolution
Implement alternative PHPUnit source code directory detection
Remaining tasks
Provide patch & review
User interface changes
None
API changes
None
Data model changes
None
Comment | File | Size | Author |
---|---|---|---|
#8 | interdiff-3143604-2-8.txt | 1.3 KB | chr.fritsch |
#8 | 3143604-8.patch | 1.77 KB | chr.fritsch |
#2 | 3143604-2.patch | 1.82 KB | Taran2L |
Comments
Comment #2
Taran2LComment #3
Taran2LComment #4
Taran2LWith this patch, everything works as expected https://dispatcher.drupalci.org/job/drupal8_contrib_patches/36849/Phpstan/
Comment #5
dungahkI can confirm those changes fixes the issue. I'm running PHPStan with a Drupal 9.0.0-rc1 website. I will not say the changes are correct because they are out of my expertise at the moment.
Comment #6
longwaveThis passes the DrupalCI tests and so the change is fine for there, as reported above this works for PHPStan now as well, so I think this is good to go - I don't think we could or should add a separate test for this specific case in core itself.
Comment #7
alexpottPlease don't removed this line. I think it is important to document where this idea came from.
Let's make less of an assumption about how the class is being autoloaded.
We can do
instead. If getClassMap() works then findFile() should too.
Comment #8
chr.fritschThanks for reviewing, @alexpott.
I applied your suggestions.
Comment #9
longwaveCan someone test #8 with PHPStan, if that works this can be marked RTBC again?
Comment #10
chr.fritschThis CI run (https://travis-ci.org/github/BurdaMagazinOrg/module-update_helper/builds...) uses the patch and PHPStan is now working.
Comment #11
longwaveThanks!
Comment #12
alexpottCommitted 15b39f0 and pushed to 9.1.x. Thanks!
Will backport to 9.0.x once that is open for commits.
Comment #15
alexpott9.0.x is now open for bugfixes.
Comment #16
yang_yi_cn CreditAttribution: yang_yi_cn at New/Mode commentedI'm not sure if the fix is good?
After applying the patch I got a bunch of phpstan errors like:
Prior to this patch I didn't get those errors, and I believe the functions still exist in D9 but somehow can't be found after the patch.
Is it due to the changes in the patch about autoloader and reflector?
Comment #17
yang_yi_cn CreditAttribution: yang_yi_cn at New/Mode commentedComment #18
alexpott@yang_yi_cn this change doesn't affect autoloading of class in PHPStan - there's something else going on. This change was about how we detect the location of the TestCase class.