LoginSignup
3
2

More than 1 year has passed since last update.

GitHub ActionsにLaraStanを追加する

Posted at

TL;DR

以下のソースをリポジトリの.github/workflows/larastan.ymlに配置すれば導入できます。
このとき、ブランチ名の先頭がTEST-から始まるものだけ解析対象になります。

.github/workflows/larastan.yml
name: LaraStan
on:
  # GitHubにPushしたときに実行
  push:
    branches:
      # ブランチ名が"TEST-"で始まるもののみを対象とする
      - TEST-*

jobs:
  phpstan:
    # Ubuntuの最新版を利用する
    runs-on: ubuntu-latest
    steps:
      # リポジトリの最新ファイル1世代分だけをチェックアウト
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0

      # PHP環境のセットアップ
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          # PHPのバージョンを7.4に指定
          php-version: '7.4'
          tools: composer, cs2pr
          coverage: none

      - name: Composer install
        run: composer install

      # setup-phpにphpstanはあるがLarastanがなさそうなので、個別にインストール
      - name: Larastan install
        run: composer require nunomaduro/larastan --dev

      # リポジトリの中で今回のPushで変更があったファイルだけを取得し、スペースでつなげた文字列を取得する
      - name: Get changed files
        id: changed-files
        uses: tj-actions/changed-files@v29.0.3

      # PHPStanを実行。解析レベルは10段階中最低の0とし、解析対象は上で取得したファイル名リストの変数を使う
      - name: Run PHPStan
        run: vendor/bin/phpstan analyze --memory-limit=-1 -l 0 -c vendor/nunomaduro/larastan/extension.neon  ${{ steps.changed-files.outputs.all_changed_files }}

解説

ほぼほぼコメントに書いてありますが、本GitHub Actionsの特徴を説明してきます。

  • ブランチ名がTEST-で始まるときのみLaraStan1でコードを静的解析します。ブランチ名にだけ気をつければ、既存のブランチのプルリクを妨げない配慮です。すべてのブランチを対象にするのなら対象ブランチ名を*にすると良いでしょう。
    例)TEST-hidao-top-page-layout-fix
  • PHPのバージョンは7.4を指定しています。これはsetup-phpプラグイン2READMEにある通り指定できます。
    PHPのバージョンによってはOSのバージョンを変更する必要があります。
  • setup-phpプラグインにはphpstanを入れるオプションはありますが、LaraStanを入れるオプションが無いようだったので、composerコマンドを叩いて直接インストールしています。
  • pushしたときに変更のあったファイルだけを解析したいので、changed-filesプラグイン3で変更のあったファイルだけを抜き出しています。
  • とりあえず導入したてなので、解析レベルを10段階中最低の0にしてます。リポジトリの成熟具合に合わせてレベルを上げていけばよいかと思います。

改善するなら

  • venderディレクトリ以下のキャッシュを持つようにしておけば処理時間が短くなると思われます。
  1. https://github.com/nunomaduro/larastan

  2. https://github.com/shivammathur/setup-php

  3. https://github.com/tj-actions/changed-files

3
2
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
3
2