在我們?nèi)粘i_(kāi)發(fā)中,一定少不了 PHP CS Fixer 來(lái)幫我們統(tǒng)一代碼風(fēng)格,但是PHP CS Fixer 不像 ESLint 一樣,可以在 PHPStorm 中在保存時(shí)自動(dòng)執(zhí)行。
PHPStorm 并沒(méi)有為我們提供可執(zhí)行 PHP CS Fixer 的選項(xiàng),「重新格式化代碼」大部分時(shí)都不能滿(mǎn)足我們的需求。
為此我們需要在 PHPStorm 中添加一個(gè) 「File Watcher」來(lái)自動(dòng)執(zhí)行代碼格式化。
1.首先全局安裝 PHP CS Fixer
composer global require friendsofphp/php-cs-fixer
登錄后復(fù)制
2.執(zhí)行
php-cs-fixer
登錄后復(fù)制
代表安裝成功了,如果提示命令未找到,那么你需要將全局 composer vendor 目錄添加到全局變量,我用的是 zsh,這里改成你自己的。
echo 'export PATH="$HOME/.composer/vendor/bin:$PATH"' >> ~/.zshrc
登錄后復(fù)制
3.打開(kāi) PHPStorm,添加自定義文件
程序文件地址,命令行輸入,并填入
which php-cs-fixer
登錄后復(fù)制
參數(shù)欄:
fix $FileDir$/$FileName$
登錄后復(fù)制
到這就搞定了,現(xiàn)在每當(dāng)我們保存時(shí)就會(huì)自動(dòng)執(zhí)行 php-cs-fixer,現(xiàn)在還有一個(gè)問(wèn)題,是可能每個(gè)項(xiàng)目有不同的 .php-cs.dist
格式化配置文件,以上的配置是使用了全局 php-cs-fixer 配置文件,如果要使用單獨(dú)的配置文件,需要修改配置如下:
fix --config=$ProjectFileDir$/.php-cs.dist $FileDir$/$FileName$
登錄后復(fù)制
.php-cs.dist
通常放在項(xiàng)目根目錄。
最后附上 .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, //多個(gè)unset,合并成一個(gè) // one should use PHPUnit methods to set up expected exception instead of annotations 'general_phpdoc_annotation_remove' => ['expectedException', 'expectedExceptionMessage', 'expectedExceptionMessageRegExp'], //phpdocs中應(yīng)該省略已經(jīng)配置的注釋 //'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, //在函數(shù)參數(shù)中,不能有默認(rèn)值在非缺省值之前的參數(shù)。有風(fēng)險(xiǎn) 'no_useless_else' => true, //刪除無(wú)用的eles 'no_useless_return' => true, //刪除函數(shù)末尾無(wú)用的return 'no_empty_phpdoc' => true, // 刪除空注釋 'no_empty_statement' => true, //刪除多余的分號(hào) 'no_leading_namespace_whitespace' => true, //刪除namespace聲明行包含前導(dǎo)空格 'no_spaces_inside_parenthesis' => true, //刪除括號(hào)后內(nèi)兩端的空格 'no_trailing_whitespace' => true, //刪除非空白行末尾的空白 'no_unused_imports' => true, //刪除未使用的use語(yǔ)句 'no_whitespace_before_comma_in_array' => true, //刪除數(shù)組聲明中,每個(gè)逗號(hào)前的空格 'no_whitespace_in_blank_line' => true, //刪除空白行末尾的空白 'ordered_class_elements' => false, //class elements排序 'ordered_imports' => false, // use 排序 'phpdoc_add_missing_param_annotation' => true, //添加缺少的 Phpdoc @param參數(shù) 'phpdoc_trim' => true, // 'phpdoc_trim_consecutive_blank_line_separation' => true, //刪除在摘要之后和PHPDoc中的描述之后,多余的空行。 'phpdoc_order' => true, 'psr4' => true, // 'strict_comparison' => true, //嚴(yán)格比較,會(huì)修改代碼有風(fēng)險(xiǎn) //'strict_param' => true, 'ternary_operator_spaces' => true, //標(biāo)準(zhǔn)化三元運(yùn)算的格式 'ternary_to_null_coalescing' => true, //盡可能使用null合并運(yùn)算符??。需要PHP> = 7.0。 'whitespace_after_comma_in_array' => true, // 在數(shù)組聲明中,每個(gè)逗號(hào)后必須有一個(gè)空格 'trim_array_spaces' => true, //刪除數(shù)組首或尾隨單行空格 'align_multiline_comment' => [ //每行多行 DocComments 必須有一個(gè)星號(hào)(PSR-5),并且必須與第一行對(duì)齊。 'comment_type' => 'phpdocs_only' ], 'array_indentation' => true, //數(shù)組的每個(gè)元素必須縮進(jìn)一次 ]) ->setFinder($finder);
登錄后復(fù)制
推薦學(xué)習(xí):《PHPstorm使用教程》