LoginSignup
26
25

More than 1 year has passed since last update.

Docker X Laravel PHP CS Fixer を導入して、ソースコードを自動整形する

Last updated at Posted at 2021-05-13

環境

PHP Coding Standards Fixer(php-cs-fixer)

リポジトリ名はPHP-CS-Fixer、正式名称はPHP Coding Standards Fixer、コマンド名はphp-cs-fixerとなります。

php-cs-fixer の導入

$ composer require --dev friendsofphp/php-cs-fixer

.php-cs-fixer.dist.php

php-cs-fixerの設定ファイルを追加します。

$ touch .php-cs-fixer.dist.php
.php-cs-fixer.dist.php
<?php declare(strict_types=1);

$finder = PhpCsFixer\Finder::create()
    ->in([
        __DIR__ . '/app',
        __DIR__ . '/config',
        __DIR__ . '/database/factories',
        __DIR__ . '/database/seeders',
        __DIR__ . '/routes',
        __DIR__ . '/tests',
    ]);

$config = new PhpCsFixer\Config();

return $config
    ->setRiskyAllowed(true)
    ->setRules([
        '@PhpCsFixer:risky' => true,
        'blank_line_after_opening_tag' => false,
        'linebreak_after_opening_tag' => false,
        'declare_strict_types' => true,
        'phpdoc_types_order' => [
            'null_adjustment' => 'always_last',
            'sort_algorithm' => 'none',
        ],
        'no_superfluous_phpdoc_tags' => false,
        'global_namespace_import' => [
            'import_classes' => true,
            'import_constants' => true,
            'import_functions' => true,
        ],
        'php_unit_test_case_static_method_calls' => [
            'call_type' => 'this'
        ],
        'phpdoc_align' => [
            'align' => 'left',
        ],
        'not_operator_with_successor_space' => true,
    ])
    ->setFinder($finder);

.gitignore に追加

.php-cs-fixer.cache ファイルはGit管理は不要なので、 .gitignore に追加します。

.php-cs-fixer.cache

php-cs-fixer 実行

# version
$ ./vendor/bin/php-cs-fixer --version
PHP CS Fixer 3.0.0 Constitution by Fabien Potencier and Dariusz Ruminski

# dry run
$ ./vendor/bin/php-cs-fixer fix -v --diff --dry-run

# fix
$ ./vendor/bin/php-cs-fixer fix -v --diff

GitHub Actions でプルリク作成時に自動チェックする

$ mkdir -p .github/workflows
$ touch .github/workflows/integration-php-cs-fixer.yml
pull-request-php-cs-fixer.yml
name: PHP Coding Standards Fixer

on:
  pull_request:

jobs:
  php-cs-fixer:
    runs-on: ubuntu-latest

    defaults:
      run:
        working-directory: backend

    steps:
      - uses: actions/checkout@v2

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.0'

      - name: Prepare
        run: |
          wget https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v3.0.0/php-cs-fixer.phar -O php-cs-fixer
          chmod a+x php-cs-fixer

      - name: OS Version
        run: cat /etc/os-release

      - name: PHP Version
        run: php -v

      - name: PHP CS Fixer Version
        run: php php-cs-fixer --version

      - name: PHP CS Fixer Run
        run: php php-cs-fixer fix --diff -vvv --dry-run

補足: ルールセット

GitHubにルールセットがまとめられています。
おすすめルールセットは PhpCsFixer:risky です。

補足: php-cs-fixerの細かい設定

PHP-CS-Fixer Configuratorという便利なサービスがあります。
細かい変更を行いたい場合はこちらをご利用下さい。

補足: Laravel Bladeのフォーマッター

別途フォーマットツールがあるので、こちらをお使いください。

26
25
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
26
25