Laravel8にLarastanがインストールできない AND 解析対象除外のファイル指定

### 参考にしたサイト
https://tech-tech.blog/php/laravel/larastan/
めっちゃ助かりました！！

### 環境
Docker Desktop for Mac
PHP 8.0
Laravel: 8

```bash:PHPのコンテナ内でPHPとLaravelのバージョン確認
root@9c5e6003337c:/work# php -v
PHP 8.0.18 (cli) (built: Apr 20 2022 12:05:49) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.18, Copyright (c) Zend Technologies

root@9c5e6003337c:/work# php artisan --version
Laravel Framework 8.83.10
```

### Larastanをインストールしたいのにできない！
#### 現象
`composer require nunomaduro/larastan --dev`でインストールできるっていろんな記事に書いてあったけどできない。。。。
何度やってもできない。

```bash:phpコンテナ内でLarastanをインストール
root@9c5e6003337c:/work# composer require nunomaduro/larastan --dev
Info from https://repo.packagist.org: #StandWithUkraine
Using version ^2.1 for nunomaduro/larastan
./composer.json has been updated
Running composer update nunomaduro/larastan
Loading composer repositories with package information
Info from https://repo.packagist.org: #StandWithUkraine
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - nunomaduro/larastan v2.1.0 requires composer/pcre ^1.0 -> found composer/pcre[1.0.0, 1.0.1, 1.x-dev] but the package is fixed to 3.0.0 (lock file version) by a partial update and that version does not match. Make sure you list it as an argument for the update command.
    - nunomaduro/larastan[v2.1.1, ..., v2.1.4] require illuminate/console ^9 -> found illuminate/console[v9.0.0-beta.1, ..., 9.x-dev] but these were not loaded, likely because it conflicts with another require.
    - Root composer.json requires nunomaduro/larastan ^2.1 -> satisfiable by nunomaduro/larastan[v2.1.0, ..., v2.1.4].

Use the option --with-all-dependencies (-W) to allow upgrades, downgrades and removals for packages currently locked to specific versions.

Installation failed, reverting ./composer.json and ./composer.lock to their original content.
```

pcreのバージョンがどうのこうの・・・？なにそれおいしいの？

### Larastanのバージョンを指定する必要がある！
`composer require nunomaduro/larastan --dev`じゃなくて、
`composer require nunomaduro/larastan:^1.0 --dev`

```bash:appコンテナ内で実行
root@9c5e6003337c:/work# composer require nunomaduro/larastan:^1.0 --dev
Info from https://repo.packagist.org: #StandWithUkraine
./composer.json has been updated
Running composer update nunomaduro/larastan
Loading composer repositories with package information
Info from https://repo.packagist.org: #StandWithUkraine
Updating dependencies
Lock file operations: 9 installs, 0 updates, 0 removals
  - Locking composer/ca-bundle (1.3.1)
  - Locking composer/composer (2.3.5)
  - Locking composer/metadata-minifier (1.0.0)
  - Locking composer/spdx-licenses (1.5.6)
  - Locking justinrainbow/json-schema (5.2.12)
  - Locking nunomaduro/larastan (1.0.3)
  - Locking react/promise (v2.9.0)
  - Locking seld/jsonlint (1.9.0)
  - Locking seld/phar-utils (1.2.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 9 installs, 0 updates, 0 removals
  - Downloading composer/ca-bundle (1.3.1)
  - Downloading composer/metadata-minifier (1.0.0)
  - Downloading composer/spdx-licenses (1.5.6)
  - Downloading justinrainbow/json-schema (5.2.12)
  - Downloading seld/phar-utils (1.2.0)
  - Downloading seld/jsonlint (1.9.0)
  - Downloading react/promise (v2.9.0)
  - Downloading composer/composer (2.3.5)
  - Downloading nunomaduro/larastan (1.0.3)
  - Installing composer/ca-bundle (1.3.1): Extracting archive
  - Installing composer/metadata-minifier (1.0.0): Extracting archive
  - Installing composer/spdx-licenses (1.5.6): Extracting archive
  - Installing justinrainbow/json-schema (5.2.12): Extracting archive
  - Installing seld/phar-utils (1.2.0): Extracting archive
  - Installing seld/jsonlint (1.9.0): Extracting archive
  - Installing react/promise (v2.9.0): Extracting archive
  - Installing composer/composer (2.3.5): Extracting archive
  - Installing nunomaduro/larastan (1.0.3): Extracting archive
1 package suggestions were added by new dependencies, use `composer suggest` to see details.
Package swiftmailer/swiftmailer is abandoned, you should avoid using it. Use symfony/mailer instead.
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: barryvdh/laravel-debugbar
Discovered Package: facade/ignition
Discovered Package: fruitcake/laravel-cors
Discovered Package: laravel/sail
Discovered Package: laravel/sanctum
Discovered Package: laravel/tinker
Discovered Package: laravel/ui
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Package manifest generated successfully.
98 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan vendor:publish --tag=laravel-assets --ansi --force
No publishable resources for tag [laravel-assets].
Publishing complete.
```

completeって出てるから成功しました！！

### 原因
nunomaduro/larastanのgithubのREADMEを確認すると、
https://github.com/nunomaduro/larastan
>Requires:
PHP 8.0+
Laravel 9.0+

つまり、laravel8は満たしてない！！
インストール時にバージョンを指定しなかった場合は最新がインストールしようとするので、条件を満たしておらずインストールエラーが発生する。

### そして実行してみるとconsole.phpでエラー
```bash:解析を実行
// 自分は、パスはすでに通してあるので、phpstanだけで実行できる
// パスを通していない場合は ./vendor/bin/phpstan analyseで実行する

root@9c5e6003337c:/work# phpstan analyse
Note: Using configuration file /work/phpstan.neon.
 46/46 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 ------ --------------------------- 
  Line   routes/console.php         
 ------ --------------------------- 
  18     Undefined variable: $this  
 ------ --------------------------- 


                                                                                                                        
 [ERROR] Found 1 error                                                                                                  
                                                                                                                        

```

routes/console.phpのエラーがありますね。
しかも解決の術がないらしい。結局、解析対象から除外するしかない。

#### 設定ファイルphpstan.neonのexcludePathsにroutes/console.phpを追記
```:phpstan.neon
includes:
    - ./vendor/nunomaduro/larastan/extension.neon

parameters:
    paths:
        - app
        - bootstrap
        - config
        - database
        - resources/views
        - routes
    level: 0

    excludePaths:
        - ./*/*/FileToBeExcluded.php
        - ./routes/console.php // ←これを追記
    checkMissingIterableValueType: false
    checkGenericClassInNonGenericObjectType: false

```


### まとめ

php7.4以下、Laravel8以下未満：`composer require nunomaduro/larastan:^1.0 --dev`
php8.0以上、Laravel9以上：`composer require nunomaduro/larastan --dev`

Laravel8以下の場合はバージョン指定をしよう！！
laravel9が２０２２年２月８日にリリースされたので、それ以前の記事では見つからないんですね。
[Laravelリリースノート](https://readouble.com/laravel/9.x/ja/releases.html)