LoginSignup
13
7

More than 5 years have passed since last update.

phpstan を使ったコードの静的解析を CI に取り入れる

Posted at

Getting Started

インストールして、

$ composer require --dev phpstan/phpstan

対象パスを指定して実行。

$ vendor/bin/phpstan analyse src tests

あら簡単。

設定ファイルの利用

phpstan.neon 設定ファイルを作成して、以下のように参照することが出来る。 -l はレベルのオプションで デフォルトは0。 最大で 7まで指定でき数字が大きいとより厳しい検査が実施される。

$ vendor/bin/phpstan analyse -c phpstan.neon app -l 0 

過剰なエラーの抑制

設定ファイル内にエラー文言を正規表現で記述してエラーの抑制を設定することが出来る。

parameters:
    ignoreErrors:
        - '#Call to an undefined static method App\\Model\\File(set)?::withTrashed\(\)#'
        - '#Call to an undefined static method Illuminate\\Support\\Facades\\DB::[\w]*\(\)#'

DBクエリの結果とかを入れる動的なオブジェクトは明示的な宣言をしてやると楽。

parameters:
    universalObjectCratesClasses:
        - Illuminate\Database\Eloquent\Model

フレームワーク固有の拡張などを入れるのも良い。

Laravel の場合は以下など。

$ composer require --dev weebly/phpstan-laravel
includes:
    - vendor/weebly/phpstan-laravel/extension.neon
13
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
7