圖文詳解PHPStorm實現自動執行代碼格式化

在我們日常開發中,一定少不了 php cs fixer 來幫我們統一代碼風格,但是php cs fixer 不像 eslint 一樣,可以在 phpstorm 中在保存時自動執行。

圖文詳解PHPStorm實現自動執行代碼格式化

phpstorm 并沒有為我們提供可執行 PHP CS Fixer 的選項,「重新格式化代碼」大部分時都不能滿足我們的需求。


為此我們需要在 PHPStorm 中添加一個 「File Watcher」來自動執行代碼格式化。

1.首先全局安裝 PHP CS Fixer

立即學習PHP免費學習筆記(深入)”;

composer global require friendsofphp/php-cs-fixer

2.執行

php-cs-fixer

圖文詳解PHPStorm實現自動執行代碼格式化

代表安裝成功了,如果提示命令未找到,那么你需要將全局 composer vendor 目錄添加到全局變量,我用的是 zsh,這里改成你自己的。

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

3.打開 PHPStorm,添加自定義文件

圖文詳解PHPStorm實現自動執行代碼格式化

程序文件地址,命令行輸入,并填入

which php-cs-fixer

圖文詳解PHPStorm實現自動執行代碼格式化

參數欄:

fix $FileDir$/$FileName$

到這就搞定了,現在每當我們保存時就會自動執行 php-cs-fixer,現在還有一個問題,是可能每個項目有不同的 .php-cs.dist 格式化配置文件,以上的配置是使用了全局 php-cs-fixer 配置文件,如果要使用單獨的配置文件,需要修改配置如下:

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

.php-cs.dist 通常放在項目根目錄。

最后附上 .php-cs.dist 配置文件

<?php $header = <<<'EOF'EOF;$finder =  PhpCsFixerFinder::create()             ->exclude('tests/Fixtures')   //排除文件             ->in(__DIR__);return PhpCsFixerConfig::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);

推薦學習:《PHPstorm使用教程》? ? ? ? ??

? 版權聲明
THE END
喜歡就支持一下吧
點贊15 分享