新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
php-cs-fixer大有用處!能自動(dòng)修證代碼風(fēng)格~

最近在看 php 之道,看到 代碼風(fēng)格指南 章節(jié)的 php-cs-fixer。
php-cs-fixer 是能夠自動(dòng)幫你修證代碼風(fēng)格,不僅僅是格式化。
如果只需要代碼保存時(shí)自動(dòng)格式化的,PhpStorm 可以開(kāi)啟這個(gè):
之前看別人發(fā)的項(xiàng)目,很多都是沒(méi)有格式化的,至少以上 PhpStorm 保存時(shí)自動(dòng)格式化也沒(méi)有開(kāi)啟吧。
接下來(lái)開(kāi)始講下,開(kāi)啟保存自動(dòng) php-cs-fixer 修正代碼的方法。
環(huán)境
- PhpStorm
- PHP 8
安裝 php-cs-fixer
這邊使用全局安裝
composer global require friendsofphp/php-cs-fixer
在項(xiàng)目根路徑下,新建文件:.php-cs-fixer.php,內(nèi)容如下:
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);
然后在 PhpStorm 設(shè)置
- 名稱:php-cs-fixer(自己喜歡的即可)
- 文件類型:PHP
- 程序:php-cs-fixer
- 參數(shù):fix $FileDir$/$FileName$ -vvv –diff
- 要刷新的輸出路徑:$FileDir$/$FileName$
- 工作目錄:$ProjectFileDir$
- 自動(dòng)保存編輯的文件以觸發(fā)觀察程序:去掉默認(rèn)的勾選
- 顯示控制臺(tái):改為始終
講下可能需要講的
- 參數(shù):
- 這邊使用了調(diào)試模式 -vvv,顯示的東西比較多,后面覺(jué)得煩可以去掉
- –diff 能夠顯示修改了什么,見(jiàn)文章下的《開(kāi)啟控制臺(tái)顯示后》
- 要刷新的輸出路徑:這個(gè)抄來(lái)的,目前效果還要驗(yàn)證
- 自動(dòng)保存編輯的文件以觸發(fā)觀察程序:就是說(shuō),只要我們輸入什么,它自動(dòng)保存,不需要 command + s 進(jìn)行保存就可以觸發(fā) php-cs-fixer。個(gè)人比較習(xí)慣手動(dòng)保存,請(qǐng)根據(jù)喜好進(jìn)行設(shè)置。
- 顯示控制臺(tái):配合 –diff,顯示修改了什么東西
效果舉例
當(dāng)我們進(jìn)行保存時(shí),它就會(huì)自動(dòng)修正代碼,在這里是修正為 PHP 7 以上的風(fēng)格。
在控制臺(tái)就顯示以下:
開(kāi)啟控制臺(tái)顯示后
/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
進(jìn)程已結(jié)束,退出代碼為 0
當(dāng)然也可以手動(dòng)在命令行執(zhí)行來(lái)批量修正整個(gè)app 目錄?;蛘呤褂?git 的,提交前自動(dòng)修正等。
網(wǎng)站題目:php-cs-fixer大有用處!能自動(dòng)修證代碼風(fēng)格~
文章分享:http://www.5511xx.com/article/cdhocco.html


咨詢
建站咨詢
