1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

larastanの導入方法

Posted at

はじめに

larastanは、laravel向けのPHP静的解析ツールです。コードの品質を向上させ、バグの発見や問題を事前に発見することができます。型チェックやメソッドの存在確認、変数の使用状況など、様々な観点からコードを分析してくれます。

公式引用:Adds code analysis to Laravel improving developer productivity and code quality.
(deepL:Laravelにコード解析を追加し、開発者の生産性とコード品質を向上させます。)

導入

インストール

php8.2+ & laravel 11.15+は 3.0 利用可能
composer require --dev "larastan/larastan:^3.0"

設定ファイル作成

アプリケーションのルートにphpstan.neonまたはphpstan.neon.distを作成
level の初期値は 5 か 6 が良さそう

includes:
    - vendor/larastan/larastan/extension.neon
    - vendor/nesbot/carbon/extension.neon

parameters:

    paths:
        - app/

    # Level 10 is the highest level
    level: 6

#    ignoreErrors:
#        - '#PHPDoc tag @var#'
#
#    excludePaths:
#        - ./*/*/FileToBeExcluded.php

実行

./vendor/bin/phpstan analyse

メモリエラーの場合(Allowed memory size exhausted)

./vendor/bin/phpstan analyse --memory-limit=2G

OKの場合

スクリーンショット 2025-06-02 15.41.22.png

NGの場合

スクリーンショット 2025-06-02 15.42.39.png
(画像ではuseの定義がなく、エラーが出ています)

ベースラインファイル作成

ベースファイルの生成
./vendor/bin/phpstan analyse --generate-baseline

phpstan.neonのincludeに追加

includes:
    - vendor/larastan/larastan/extension.neon
    - vendor/nesbot/carbon/extension.neon
    - phpstan-baseline.neon // 追加

ベースラインで生成されたエラーは以降では表示されなくなるのでプロジェクトの途中で導入して警告が多い場合や導入以降の追加コードのみ検証したい場合でも対応できます。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?