CodeIgniter DevKit で PHPStan を使い静的解析する
この記事は CodeIgniter Advent Calendar 2022 - Qiita の21日目です。まだ、空きがありますので、興味のある方は気軽に参加してください。
「CodeIgniter DevKit で Rector を使いコーディングスタイルを修正する」 の続きです。
PHPStanとは?
PHPStanはPHPの静的解析ツールです。
PHPStanの設定
DevKitをインストールすれば、PHPStanはインストール済みです。
DevKitのインストールについては 「CodeIgniter DevKit で php-cs-fixer を使いコーディングスタイルを修正する」 を参照してください。
vendor/codeigniter4/devkit/src/Template/phpstan.neon.dist
をプロジェクトルートにコピーします。
そして、コピーした phpstan.neon.dist をプロジェクトに合わせて調整します。
parameters:
    tmpDir: build/phpstan
    level: 5
    paths:
        - app/
        - tests/
    bootstrapFiles:
        - vendor/codeigniter4/framework/system/Test/bootstrap.php
    excludePaths:
        - app/Config/Routes.php
        - app/Views/*
    ignoreErrors:
    universalObjectCratesClasses:
        - CodeIgniter\Entity
        - CodeIgniter\Entity\Entity
        - Faker\Generator
    scanDirectories:
        - vendor/codeigniter4/framework/system/Helpers
    dynamicConstantNames:
        - APP_NAMESPACE
        - CI_DEBUG
        - ENVIRONMENT
ここでは、特に変更せずそのままです。
なお、パスの設定が正しいかは必ず確認してください。DevKitのテンプレートのパスはCodeIgniter4のアプリのパスに設定されているため、ライブラリの場合は少し修正が必要です。
composer.json の設定
実行するためのコマンドを composer.json に追加します。
--- a/composer.json
+++ b/composer.json
@@ -27,7 +27,11 @@
],
"cs": "php-cs-fixer fix --ansi --verbose --dry-run --diff",
"cs-fix": "php-cs-fixer fix --ansi --verbose --diff --using-cache=yes",
-       "style": "@cs-fix"
+       "style": "@cs-fix",
+       "analyze": [
+           "bash -c \"XDEBUG_MODE=off phpstan analyse\""
+       ],
+       "sa": "@analyze"
  },
  "support": {
  "forum": "http://forum.codeigniter.com/",
これで、composer sa でPHPStanが実行されます。
PHPStanの実行
それではPHPStanを実行してみましょう。
$ composer sa
> bash -c "XDEBUG_MODE=off phpstan analyse"
Note: Using configuration file /.../codeigniter4login/phpstan.neon.dist.
 56/56 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
 ------ --------------------------------------------------------------------------------------- 
  Line   app/Controllers/Users.php                                                              
 ------ --------------------------------------------------------------------------------------- 
  16     Call to deprecated method getMethod() of class CodeIgniter\HTTP\Request:               
         The $upper functionality will be removed and this will revert to its PSR-7 equivalent  
  68     Call to deprecated method getMethod() of class CodeIgniter\HTTP\Request:               
         The $upper functionality will be removed and this will revert to its PSR-7 equivalent  
  108    Call to deprecated method getMethod() of class CodeIgniter\HTTP\Request:               
         The $upper functionality will be removed and this will revert to its PSR-7 equivalent  
 ------ --------------------------------------------------------------------------------------- 
 [ERROR] Found 3 errors                                                                                                 
Script bash -c "XDEBUG_MODE=off phpstan analyse" handling the analyze event returned with error code 1
Script @analyze was called via sa
エラーが報告されました。
Request::getMethod() の引数 $upper の廃止予定に関する警告です。
PHPStan設定の調整
Call to deprecated method getMethod() of class CodeIgniter\HTTP\Request: The $upper functionality will be removed and this will revert to its PSR-7 equivalent
このエラーはフレームワークのPHPDocコメントが原因なので、 アプリのコードの問題ではありません。ユーザーが修正することはできません。
そこでエラーを抑制します。
ベースラインの作成
ベースラインを作成しましょう。
$ vendor/bin/phpstan --generate-baseline
 ! [NOTE] The Xdebug PHP extension is active, but "--xdebug" is not used. This may slow down performance and the process
 !        will not halt at breakpoints.                                                                                 
Note: Using configuration file /.../codeigniter4login/phpstan.neon.dist.
 56/56 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
 [OK] Baseline generated with 3 errors.
これで、以下の phpstan-baseline.neon が作成されました。
parameters:
    ignoreErrors:
        -
            message: """
                #^Call to deprecated method getMethod\\(\\) of class CodeIgniter\\\\HTTP\\\\Request\\:
                The \\$upper functionality will be removed and this will revert to its PSR\\-7 equivalent$#
            """
            count: 3
            path: app/Controllers/Users.php
しかし、このエラーは Request::getMethod() を使うたびに発生します。
そこで、以下のように回数を削除して、パスを変更します。
parameters:
    ignoreErrors:
        -
            message: """
                #^Call to deprecated method getMethod\\(\\) of class CodeIgniter\\\\HTTP\\\\Request\\:
                The \\$upper functionality will be removed and this will revert to its PSR\\-7 equivalent$#
            """
            path: app/Controllers/*.php
次にベースラインを使うようにメインの設定ファイルを変更します。
--- a/phpstan.neon.dist
+++ b/phpstan.neon.dist
@@ -1,3 +1,5 @@
+includes:
+   - phpstan-baseline.neon
 parameters:
    tmpDir: build/phpstan
    level: 5
これで、ベースラインが使われるようになりました。
再度、実行してエラーが出ないことを確認しましょう。
$ composer sa
> bash -c "XDEBUG_MODE=off phpstan analyse"
Note: Using configuration file /.../codeigniter4login/phpstan.neon.dist.
 56/56 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
 [OK] No errors
設定が完了し、問題がなければ、Gitでcommitしてください。
まとめ
- CodeIgniter DevKit はCodeIgniterのライブラリとプロジェクトのための開発用のツールキットです。
 - DevKitにはPHPStanが含まれており、簡単に静的解析を実行できます。
 
「CodeIgniter DevKit で Psalm を使い静的解析する」 へ続く。
この記事は CodeIgniter Advent Calendar 2022 - Qiita の21日目です。まだ、空きがありますので、興味のある方は気軽に参加してください。
関連
参考
Date: 2022/12/21



![徹底攻略PHP5技術者認定[上級]試験問題集  [PJ0-200]対応 徹底攻略PHP5技術者認定[上級]試験問題集  [PJ0-200]対応](http://tatsu-zine.com/images/books/164/cover_s.jpg)

