「Upgrade Status」モジュールのPHPStanエラーを解消する方法

  •  
 
TaKo2021年3月19日 - 15:48 に投稿

タグ

関連リンク

概要

「Upgrade Status」モジュールの調査中、プロジェクトのScanがPhpstanエラーでうまくいきませんでした。
そのエラーの解消した方法をここで共有します。

調査環境

  バージョン
CentOS 7.9.2009
httpd 2.4.46
PHP 7.4.16
MySQL 8.0.23
Composer 2.0.11
Drush 10.4.0 (Drush Lancherも導入)
Drupal 8.9.13 (tarballベースで準備)

解消手順

*Upgrade Statusのインストールは割愛します。

「Upgrade Status」画面でScanを実行します。
そして、problemsを確認すると以下の様なエラーが出ていました。

ファイル名 エラー
PHPStan failed 0
PHPStan command failed:

/var/www/html/d8tod9/vendor/bin/phpstan analyse --error-format=json -c /tmp/upgrade_status/deprecation_testing.neon /var/www/html/d8tod9/modules/custom/upgrade_test 2> /tmp/upgrade_status/phpstan_error_output

Command output:

Empty.

Command error:

PHP Warning: Class 'PHPUnit\Framework\AssertionFailedError' not found in /var/www/html/d8tod9/core/tests/bootstrap.php on line 192 Warning: Class 'PHPUnit\Framework\AssertionFailedError' not found in /var/www/html/d8tod9/core/tests/bootstrap.php on line 192 ....省略...

modules/custom/upgrade_test 0 The 'upgrade_test' extension is not installed. Cannot check deprecated library use.
modules/custom/upgrade_test/upgrade_test.info.yml 0 Add core_version_requirement: ^8 || ^9 to designate that the module is compatible with Drupal 9. See https://drupal.org/node/3070687.
modules/custom/upgrade_test/composer.json 0 Parse error in composer.json. Having a composer.json is not a requirement for Drupal 9 compatibility but if there is one, it should be valid. See https://drupal.org/node/2514612.

PHP Warning: Class 'PHPUnit\...省略...と表示されています。
そこで、PHPUnitをインストールしてみました。


cd PATH/TO/DRUPAL_ROOT
find ./ -name phpunit
vendor/grasmash/yaml-expander/tests/phpunit
vendor/grasmash/expander/tests/phpunit

vender/binphpunitがないので、インストールされていないようです。

composerphpunitのインストールを試みます。

参考:PHPUnit のインストール — PHPUnit latest Manual

composer require phpunit/phpunit --dev --no-update
composer update --dry-run 
composer update
drush cr

*phpunit:9.5.2がインストールされました。

もう一度Scanを実行します。

だめでした。

参考:Drupal 8 PHPUnit testing custom modules - Stack Overflow
参考:8 - PHPUnit errors occur when running drupal-check - Drupal Answers
参考:PHPUnit in Drupal | Automated testing | Drupal Wiki guide on Drupal.org

PHPとPHPUnitのバージョンに気をつけないといけないようです。
PHPUnitのバージョンに気をつけないといけないようです。
Drupal9なら、PHPUnit9系でも問題ないようです。

少し低いバージョンのphpunitにダウングレードします。

composer require phpunit/phpunit:^7 --dev --no-update -W
composer update --dry-run
composer update
drush cr

*phpunit:7.5.20にダウングレードされました。

もう一度Scanを実行します。

ファイル名 エラー
modules/custom/upgrade_test/tests/src/Functional/LoadTest.php   Class Behat\Mink\WebAssert not found.
modules/custom/upgrade_test 0 The 'upgrade_test' extension is not installed. Cannot check deprecated library use.
modules/custom/upgrade_test/upgrade_test.info.yml 0 Add core_version_requirement: ^8 || ^9 to designate that the module is compatible with Drupal 9. See https://drupal.org/node/3070687.
modules/custom/upgrade_test/composer.json 0 Parse error in composer.json. Having a composer.json is not a requirement for Drupal 9 compatibility but if there is one, it should be valid. See https://drupal.org/node/2514612.

*PHPStanのエラーが解消されました!

*ただし、PHPのメモリ不足によるエラー(PHP Fatal error: Allowed memory size of....)はおきます。
php.iniを編集して、十分なメモリを割り当てるか一時的に無制限にしてやる必要があります。

vi /etc/php.ini
memory_limit=128M 
↓ 
memory_limit=-1
systemctl restart httpd

コメントを追加

プレーンテキスト

  • HTMLタグは利用できません。
  • 行と段落は自動的に折り返されます。
  • ウェブページのアドレスとメールアドレスは自動的にリンクに変換されます。
CAPTCHA
この質問はあなたが人間の訪問者であるかどうかをテストし、自動化されたスパム送信を防ぐためのものです。