start
Recently watching The way of PHP , see Code style guide PHP CS fixer in chapter.
PHP CS fixer can automatically help you fix the code style, not just formatting.
If you only need to format the code automatically when saving, PhpStorm can open this:
Many of the items sent by others have not been formatted before. At least the automatic formatting is not enabled when the above PhpStorm is saved.
Next, let's turn on the method of saving automatic PHP CS fixer correction code.
environment
- PhpStorm
- PHP 8
Install PHP CS fixer
Global installation is used here
composer global require friendsofphp/php-cs-fixer
See cs.symfony.com/doc/installation.ht...
Under the project root path, create a new file:. php-cs-fixer.php, as follows:
<?php use 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);
Then set it in PhpStorm
- Name: PHP CS fixer (whatever you like)
- File type: PHP
- Program: PHP CS fixer
- Parameters: fix $filedir $/ $filename $- VVV – diff
- Output path to refresh: $FileDir$/$FileName$
- Working directory: $ProjectFileDir$
- Automatically save the edited file to trigger the observer: remove the default check box
- Display console: change to always
Tell me what you may need to say
- Parameters:
- The debugging mode -vvv is used here. There are a lot of things displayed. You can get rid of the annoyance later
- – diff can display what has been modified. See "after opening the console display" under the article
- Output path to refresh: This is copied, and the current effect needs to be verified
- Automatically save the edited file to trigger the observer: that is, as long as we enter something, it will be saved automatically, and PHP CS fixer can be triggered without saving with command + s. I am used to saving manually. Please set it according to your preferences.
- Display console: cooperate with – diff to display what has been modified
Effect example
When we save, it will automatically modify the code. Here, it will be modified to the style of PHP 7 or above.
The following is displayed on the console:
After the console display is turned on
/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 The process has ended with exit code 0