Home > Backend Development > PHP Tutorial > php-cs-fixer is very useful! Can automatically correct code style~

php-cs-fixer is very useful! Can automatically correct code style~

藏色散人
Release: 2023-04-10 20:02:01
forward
4329 people have browsed it

Start

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:

php-cs-fixer is very useful! Can automatically correct code style~

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.

Environment

  • PhpStorm
  • PHP 8

##Install php-cs -fixer

Use global installation here

composer global require friendsofphp/php-cs-fixer
Copy after login
See https://cs.symfony.com/doc/installation.html

in 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 = [
    &#39;@PHP80Migration&#39; => 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);
Copy after login
Then set up PhpStorm

php-cs-fixer is very useful! Can automatically correct code style~

php-cs-fixer is very useful! Can automatically correct code style~

    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 edited files to trigger the observer: remove the default checkbox
  • Show console: Change to always
Tell what you may need to talk about

    Parameters:
    • The debugging mode-vvv is used here, which displays more things. If you find it annoying later, you can remove it.
    • -diff can display what has been modified, see " After turning on the console display》
  • The output path to be refreshed: This is copied, the current effect still needs to be verified
  • Automatically save the edited file to trigger the observation program: That is to say, as long as we enter something, it will be saved automatically, and php-cs-fixer can be triggered without command s to save. Personally, I am more accustomed to saving manually, so please set it according to your preference.
  • Display console: cooperate with –diff to display what has been modified

Example of effect

php-cs-fixer is very useful! Can automatically correct code style~

When we save, it will automatically correct the code, in this case to the style of PHP 7 or above.

php-cs-fixer is very useful! Can automatically correct code style~

The console displays the following:

After turning on the console display
/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(&#39;test&#39;, function () {
-    $taskTag[&#39;name&#39;] = $taskTag[&#39;name&#39;] ?? &#39;url&#39;;
+    $taskTag[&#39;name&#39;] ??= &#39;url&#39;;
 });

      ----------- end diff -----------


Fixed all files in 0.024 seconds, 14.000 MB memory used

进程已结束,退出代码为 0
Copy after login

Of course, you can also manually Execute it on the command line to batch correct the entire app directory. Or use git to automatically correct before submission, etc.

Recommended learning: "

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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template