Recently I was reading The Tao of PHP and saw php-cs-fixer in the Code Style Guide chapter.
php-cs-fixer can automatically help you fix your code style, not just formatting.
If you only need automatic formatting when saving the code, PhpStorm can turn this on:
I have seen projects posted by others before, and many of them are not formatted. Yes, at least the automatic formatting when saving in PhpStorm is not turned on.
Let’s start with the method of saving the automatic php-cs-fixer correction code.
composer global require friendsofphp/php-cs-fixer
See https://cs.symfony.com/doc/installation.htmlin the project Under the root path, create a new file: .php-cs-fixer.php with the following content:
<?phpuse PhpCsFixer\Config;use PhpCsFixer\Finder;$rules = [ '@PHP80Migration' => true, 'ordered_imports' => [ 'sort_algorithm' => 'alpha', ], 'class_attributes_separation' => [ 'elements' => [ 'const' => 'one', 'method' => 'one', 'property' => 'one', ], ],];$finder = Finder::create() ->in([ __DIR__.'/app', __DIR__.'/config', __DIR__.'/database', __DIR__.'/resources', __DIR__.'/routes', __DIR__.'/tests', ]) ->name('*.php') ->notName('*.blade.php') ->ignoreDotFiles(true) ->ignoreVCS(true);return (new Config()) ->setFinder($finder) ->setRules($rules) ->setRiskyAllowed(true) ->setUsingCache(true);
/Users/dogeow/.composer/vendor/bin/php-cs-fixer fix /Users/dogeow/PhpstormProjects/antic-api/routes/console.php -vvv --diff Cannot load Xdebug - it was already loaded PHP CS Fixer 3.3.2 Trinacria by Fabien Potencier and Dariusz Ruminski Runtime: PHP 8.0.8 Loaded config default from "/Users/dogeow/PhpstormProjects/antic-api/.php-cs-fixer.php". Using cache file ".php-cs-fixer.cache". Paths from configuration file have been overridden by paths provided as command arguments. F 1 / 1 (100%) Legend: ?-unknown, I-invalid file syntax (file ignored), S-skipped (cached or empty file), .-no changes, F-fixed, E-error 1) routes/console.php (assign_null_coalescing_to_coalesce_equal) ---------- begin diff ---------- --- /Users/dogeow/PhpstormProjects/antic-api/routes/console.php +++ /Users/dogeow/PhpstormProjects/antic-api/routes/console.php @@ -90,5 +90,5 @@ }); Artisan::command('test', function () { - $taskTag['name'] = $taskTag['name'] ?? 'url'; + $taskTag['name'] ??= 'url'; }); ----------- end diff ----------- Fixed all files in 0.024 seconds, 14.000 MB memory used 进程已结束,退出代码为 0
PHP Video Tutorial"
The above is the detailed content of php-cs-fixer is very useful! Can automatically correct code style~. For more information, please follow other related articles on the PHP Chinese website!