LoginSignup
1
0

More than 1 year has passed since last update.

php-cs-fixer 自動でdeclare(strict_types=1)を付与する

Posted at

概要

  • php-cs-fixerを用いて自動でdeclare(strict_types=1);を付与する方法をメモ的にまとめる。

方法

  • php-cs-fixer本体の設定を変更する。

    .php-cs-fixer.dist.php
    <?php 
    
    declare(strict_types=1);
    
    $finder = PhpCsFixer\Finder::create()
        // チェックするディレクトリの指定
        ->in([
            __DIR__ . '/app',
            __DIR__ . '/config',
            __DIR__ . '/database/seeders',
            __DIR__ . '/routes',
            __DIR__ . '/tests',
        ]);
    
    $config = new PhpCsFixer\Config();
    
    return $config
        ->setRiskyAllowed(true)
        ->setRules([
            '@PSR12' => true,
            'declare_strict_types' => true, // こちらを追加
        ])
        ->setFinder($finder);
    
  • 整形を実行する。

  • vscodeの拡張を使っている方はそちらも修正する。

    setting.json
    {
        "i18n-ally.localesPaths": [
            "lang"
        ],
    
        // php-cs-fixer
        "php-cs-fixer.executablePath": "${workspaceFolder}/tools/php-cs-fixer/vendor/bin/php-cs-fixer",
        "php-cs-fixer.executablePathWindows": "",   //eg: php-cs-fixer.bat
        "php-cs-fixer.onsave": true,
        "php-cs-fixer.config": ".php-cs-fixer.dist.php;",
        "php-cs-fixer.allowRisky": true, // こちらをtrueに変更
        "php-cs-fixer.pathMode": "override",
        "php-cs-fixer.exclude": [],
        "php-cs-fixer.autoFixByBracket": true,
        "php-cs-fixer.autoFixBySemicolon": false,
        "php-cs-fixer.formatHtml": false,
        "php-cs-fixer.documentFormattingProvider": true
    
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