Laravel10でLaravel用の静的解析ツールLarastanを導入して実施してみた際の記録と解説です。
GitHub - larastan/larastan: ⚗️ Adds code analysis to Laravel improving developer productivity and code quality.
⚗️ Adds code analysis to Laravel improving developer productivity and code quality. - GitHub - larastan/larastan: ⚗️ Add...
前提条件
- Ubuntuで作業しています
- PHP8.2以降インストール済
- Composer2.xインストール済
- Laravel10.xをインストールします
- Larastan2.xをインストールします
Laravelプロジェクト作成
執筆時点でLaravelの最新版は10.3.2なので、このバージョン指定でインストールしていきます。
![](https://macocci7.net/blog/wp-content/uploads/2024/01/intro_larastan01.png)
GitHub - laravel/laravel: Laravel is a web application framework with expressive, elegant syntax. We’ve already laid the foundation for your next big idea — freeing you to create without sweating the small things.
Laravel is a web application framework with expressive, elegant syntax. We’ve already laid the foundation for your next ...
作業ディレクトリ上でコマンド実行します。
composer create-project laravel/laravel webapp
![](https://macocci7.net/blog/wp-content/uploads/2024/01/intro_larastan02.png)
プロジェクト作成が完了したら、プロジェクトフォルダに入ってWEBサービスを起動します。
cd webapp
php artisan serve
![](https://macocci7.net/blog/wp-content/uploads/2024/01/intro_larastan03.png)
ブラウザでデフォルトページが表示されました。
![](https://macocci7.net/blog/wp-content/uploads/2024/01/intro_larastan04.png)
Larastanインストール
プロジェクトトップでコマンド実行します。
composer require larastan/larastan:^2.0 --dev
![](https://macocci7.net/blog/wp-content/uploads/2024/01/intro_larastan05.png)
設定ファイル作成
プロジェクトトップに「phpstan.neon」を作成します。
includes:
- vendor/larastan/larastan/extension.neon
parameters:
paths:
- app/
# Level 9 is the highest level
level: 5
# ignoreErrors:
# - '#PHPDoc tag @var#'
#
# excludePaths:
# - ./*/*/FileToBeExcluded.php
#
# checkMissingIterableValueType: false
同様に「phpstan.neon.dist」を作成します。
LarastanはPHPStanのラッパーなので、設定内容自体はPHPStanそのもので、Larastan拡張設定は最上部でincludeしています。
Larastan実行
プロジェクトトップでコマンド実行します。
./vendor/bin/phpstan analyse
![](https://macocci7.net/blog/wp-content/uploads/2024/01/intro_larastan06-1.png)
「level:9」で実施しても「No errors」でした。
テスト対象を増やしてみる
「phpstan.neon」を次のように修正しました。
includes:
- vendor/larastan/larastan/extension.neon
parameters:
paths:
- app/
- config/
- database/
- resources/
- routes/
- tests/
# Level 9 is the highest level
level: 9
# ignoreErrors:
# - '#PHPDoc tag @var#'
#
# excludePaths:
# - ./*/*/FileToBeExcluded.php
#
# checkMissingIterableValueType: false
これで実行してみたら、メモリ上限に達してクラッシュしました。。
![](https://macocci7.net/blog/wp-content/uploads/2024/01/intro_larastan07.png)
メッセージ通りに「–memory-limit」オプションを付けて再実行してみます。
./vendor/bin/phpstan analyze --memory-limit 256M
![](https://macocci7.net/blog/wp-content/uploads/2024/01/intro_larastan08.png)
ちょっとウルサイ感じですね。
「config」と「routes」は外してみましょう。
paths:
- app/
#- config/
- database/
- resources/
#- routes/
- tests/
# Level 9 is the highest level
level: 9
再度実行してみます。
![](https://macocci7.net/blog/wp-content/uploads/2024/01/intro_larastan09.png)
コントローラーを追加してテストする
コントローラーを追加してルーティングしてみます。
php artisan make:controller HelloController
![](https://macocci7.net/blog/wp-content/uploads/2024/01/intro_larastan10.png)
ドキュメントブロック無しで適当に編集します。
![](https://macocci7.net/blog/wp-content/uploads/2024/01/intro_larastan11.png)
ルーティングします。
![](https://macocci7.net/blog/wp-content/uploads/2024/01/intro_larastan12.png)
ブラウザで表示確認してみます。
![](https://macocci7.net/blog/wp-content/uploads/2024/01/intro_larastan13.png)
Larastanを実行してみます。
![](https://macocci7.net/blog/wp-content/uploads/2024/01/intro_larastan14.png)
リターンタイプの指定が無いとのことで叱られました。
コントローラーにドキュメントブロックを追記します。
![](https://macocci7.net/blog/wp-content/uploads/2024/01/intro_larastan15.png)
再度Larastanを実行してみます。
![](https://macocci7.net/blog/wp-content/uploads/2024/01/intro_larastan16.png)
OKですね。
コメント