概要
- 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