vscode 中使用php-cs-fixer和PHP Formatter 插件規范化PHP代碼


   它是php-fig組織定義的PHP代碼規范,良好的代碼規范可以提高代碼可讀性,團隊溝通維護成本
   使用它可以按照指定的規范格式化您的PHP代碼,此工具不僅可以檢測有不符合規范的代碼,而且還可以修復它們
 
 

 

1.使用php-cs-fixer.phar格式化php文件
         1. 下載 php-cs-fixer.phar到任意目錄,盡量放到php目錄下 )
         2.在VScode里手動調用 php-cs-fixer.phar 進行格式化
case3.gif
 
 
2.在Vscode里 PHP Formatter 插件
    1.下載 php-cs-fixer.phar到任意目錄,盡量放到php目錄下 )
    2.安裝 PHP Formatter 插件 並進行自定義設置(php路徑、 php-cs-fixer.phar路徑,Rules等 )
    3.自定義 PHP Formatter 插件的快捷鍵
 
 
配置信息如下:
 
//打印日志信息,用於調試
"phpformatter.logging":true,
//不使用composer方式
"phpformatter.composer":false,
//添加自定義參數,默認的參數level已經在新版本中移出所以會導致運行出錯
//RULES=[@PSR1,@PSR2,@Symfony]
//source:https://github.com/FriendsOfPHP/PHP-CS-Fixer#usage
"phpformatter.arguments":["--rules=@Symfony"],
// Should point to php-cs-fixer.phar file, if you have installed this manually (without Composer). Should include .phar extension.
// php-cs-fixer.phar路徑,使用composer方式時可以不填
"phpformatter.pharPath":"d:/wamp/bin/php/php5.5.12/php-cs-fixer.phar",
// If the pharPath is set, and you are not using Composer, and you haven't added PHP to your PATH, this should point to the php.exe file.
// php路徑,使用composer方式時可以不填
"phpformatter.phpPath":"d:/wamp/bin/php/php5.5.12/php.exe"

 

VScode自定義快捷鍵配置
 
case2.gif
 
參考:






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM