Home > Development Tools > phpstorm > Detailed graphic explanation of PHPStorm's automatic execution of code formatting

Detailed graphic explanation of PHPStorm's automatic execution of code formatting

藏色散人
Release: 2022-12-29 17:43:54
forward
3179 people have browsed it

In our daily development, PHP CS Fixer is indispensable to help us unify the code style, but PHP CS Fixer, unlike ESLint, can be automatically executed in PHPStorm when saving.

Detailed graphic explanation of PHPStorms automatic execution of code formatting

PHPStorm does not provide us with the option to execute the PHP CS Fixer, and "reformatting the code" cannot meet our needs most of the time.


For this we need to add a "File Watcher" in PHPStorm to automatically perform code formatting.

1. First install PHP CS Fixer globally

composer global require friendsofphp/php-cs-fixer
Copy after login

2. Execute

php-cs-fixer
Copy after login

Detailed graphic explanation of PHPStorms automatic execution of code formatting

means the installation is successful, if the prompt command is not found , then you need to add the global composer vendor directory to the global variables. I use zsh, change it to your own here.

echo 'export PATH="$HOME/.composer/vendor/bin:$PATH"' >> ~/.zshrc
Copy after login

3. Open PHPStorm, add the custom file

Detailed graphic explanation of PHPStorms automatic execution of code formatting

program file address, enter the command line, and fill in

which php-cs-fixer
Copy after login

Detailed graphic explanation of PHPStorms automatic execution of code formatting

Parameter column:

fix $FileDir$/$FileName$
Copy after login

That’s it. Now whenever we save, php-cs-fixer will be automatically executed. Now there is another problem. It may be that every Projects have different .php-cs.dist formatted configuration files. The above configuration uses the global php-cs-fixer configuration file. If you want to use a separate configuration file, you need to modify the configuration as follows:

fix --config=$ProjectFileDir$/.php-cs.dist $FileDir$/$FileName$
Copy after login

.php-cs.dist is usually placed in the project root directory.

Finally attached .php-cs.dist Configuration file

<?php $header = <<<&#39;EOF&#39;EOF;$finder =  PhpCsFixer\Finder::create()
            ->exclude('tests/Fixtures')   //排除文件
            ->in(__DIR__);return PhpCsFixer\Config::create()
    ->setRiskyAllowed(true)
    ->setRules([
        '@PSR2'                                 => true,
        '@Symfony:risky'                        => true,
        'array_syntax'                          => ['syntax' => 'short'],
        'combine_consecutive_unsets'            => true,   //多个unset,合并成一个
        // one should use PHPUnit methods to set up expected exception instead of annotations
        'general_phpdoc_annotation_remove'      => ['expectedException', 'expectedExceptionMessage', 'expectedExceptionMessageRegExp'], //phpdocs中应该省略已经配置的注释
        //'header_comment'                        => array('header' => $header), //添加,替换或者删除 header 注释。
        'heredoc_to_nowdoc'                     => true,     //删除配置中多余的空行和/或者空行。
        'no_extra_consecutive_blank_lines'      => ['break', 'continue', 'extra', 'return', 'throw', 'use', 'parenthesis_brace_block', 'square_brace_block', 'curly_brace_block'],
        'no_unreachable_default_argument_value' => false, //在函数参数中,不能有默认值在非缺省值之前的参数。有风险
        'no_useless_else'                       => true,  //删除无用的eles
        'no_useless_return'                     => true,  //删除函数末尾无用的return
        'no_empty_phpdoc'                       => true,  // 删除空注释
        'no_empty_statement'                    => true,  //删除多余的分号
        'no_leading_namespace_whitespace'       => true,  //删除namespace声明行包含前导空格
        'no_spaces_inside_parenthesis'          => true,  //删除括号后内两端的空格
        'no_trailing_whitespace'                => true,  //删除非空白行末尾的空白
        'no_unused_imports'                     => true,  //删除未使用的use语句
        'no_whitespace_before_comma_in_array'   => true,  //删除数组声明中,每个逗号前的空格
        'no_whitespace_in_blank_line'           => true,  //删除空白行末尾的空白
        'ordered_class_elements'                => false, //class elements排序
        'ordered_imports'                       => false, // use 排序
        'phpdoc_add_missing_param_annotation'   => true,   //添加缺少的 Phpdoc @param参数
        'phpdoc_trim'                           => true,
        //   'phpdoc_trim_consecutive_blank_line_separation' => true, //删除在摘要之后和PHPDoc中的描述之后,多余的空行。
        'phpdoc_order'                          => true,
        'psr4'                                  => true,
        // 'strict_comparison'                     => true,   //严格比较,会修改代码有风险
        //'strict_param'                          => true,
        'ternary_operator_spaces'               => true,  //标准化三元运算的格式
        'ternary_to_null_coalescing'            => true,  //尽可能使用null合并运算符??。需要PHP> = 7.0。
        'whitespace_after_comma_in_array'       => true, // 在数组声明中,每个逗号后必须有一个空格
        'trim_array_spaces'                     => true,  //删除数组首或尾随单行空格
        'align_multiline_comment'               => [                   //每行多行 DocComments 必须有一个星号(PSR-5),并且必须与第一行对齐。
            'comment_type' => 'phpdocs_only'
        ],
        'array_indentation'                 => true,  //数组的每个元素必须缩进一次
    ])
    ->setFinder($finder);
Copy after login

Recommended study: "PHPstorm usage tutorial"

The above is the detailed content of Detailed graphic explanation of PHPStorm's automatic execution of code formatting. 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