本篇文章中介紹的擴展是 vscode-phpcs,用于項目開發中 PHP 代碼的編碼規范。
Github 庫地址 vscode-phpcs,使用這個擴展依賴于一個前置條件,系統需要安裝 PHP_CodeSniffer
官方是這么介紹這個擴展的
https://github.com/shevaua/vscode-phpcs
認識 PHP_CodeSniffer
PHP_CodeSniffer is an essential development tool that ensures your code remains clean and consistent.
在眾多的第三方組織和相關的代碼規范推薦中,PHP_CodeSniffer 是一款代碼檢查工具,用于規范團隊開發中的類名,注釋,參數等各個方面,幫助團隊成員形成統一的編碼風格。它本身可以與很多IDE結合,也提供了修改和指定自定義代碼規范的功能。
PHP_CodeSniffer 包地址 PHP_CodeSniffer
Github 庫地址 PHP_CodeSniffer
PHP_CodeSniffer 安裝
根據操作平臺不同,PHP_CodeSniffer有不同的安裝 方式,集中于Pear,Composer,和Brew。
Linux 安裝方式
pear install PHP_CodeSniffer
whereis phpcs
phpcs: /usr/bin/phpcs
phpcs --version
PHP_CodeSniffer version 3.5.0 (stable) by Squiz (http://www.squiz.net)
Brew 安裝
Mac 下采用以下命令安裝,最為方便
brew install php-code-sniffer
安裝路徑
/usr/local/Cellar/php-code-sniffer
/usr/local/Cellar/php-code-sniffer/3.4.1/bin
ls
phpcbf phpcs
Composer 方式
composer global require squizlabs/php_codesniffer
安裝路徑
/Users/name/.composer/vendor/squizlabs/php_codesniffer/
ls
phpcbf phpcbf.bat phpcs phpcs.bat
/Users/name/.composer/vendor/bin
文件夾下有兩個文件 phpcbf phpcs
運行
./phpcs --version
可以查看版本號
PHP_CodeSniffer version 3.4.2 (stable) by Squiz (http://www.squiz.net)
Mac 下推薦使用 brew 安裝
其它安裝方式參考 Github 庫PHP_CodeSniffer
VS Code 中啟用 PHP_CodeSniffer
VS Code 中啟用 PHP_CodeSniffer 需要安裝 phpcs 插件。
Before using this plugin, you must ensure that phpcs is installed on your system,The preferred method is using composer for both system-wide and project-wide installations.
也就是說在 VS Code 中使用 phpcs 插件之前,需要首先在系統上安裝有 phpcs,安裝方法是依賴于 composer 方式,并且可以從系統級和項目級兩個緯度選擇安裝。如果只是在 VS Code 中安裝插件,系統級中不安裝 phpcs。會出現以下錯誤
phpcs: Request workspace/configuration failed with message: Unable to locate phpcs. Please add phpcs to your global path or use composer dependency manager to install it in your project locally.
還有一點,phpcs插件只是CS Code中實現PHP CodeSniffer中的一個,還有很多其它插件也在做基于PHP CodeSniiffer的代碼整潔優化工作。比如 EasyCodingStandard
系統級全局安裝
composer global require squizlabs/php_codesniffer
Changed current directory to /Users/wmm/.composer
Using version ^3.4 for squizlabs/php_codesniffer
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing squizlabs/php_codesniffer (3.4.0): Downloading (100%)
Writing lock file
Generating autoload files
項目級別的安裝
以下是 VS Code 對于插件 phpcs 的介紹說明,更多參考 https://marketplace.visualstudio.com/items?itemName=ikappas.phpcs
Project-wide Installation
The phpcs
linter can be installed in your project using the Composer Dependency Manager for PHP.
- Install composer.
- Require
phpcs
package by typing the following at the root of your project in a terminal:
composer require --dev squizlabs/php_codesniffer
phpcs 與 PHP Sniffer 關系
VS Code 還有另外一個插件 PHP Sniffer。在特性中寫著兩條
Runs phpcs to lint PHP code.
Runs phpcbf to format fixable PHP code validation errors.`
顯然可以推斷 PHP Sniffer 也是對 PHP_CodeSniffer 的封裝。驗證后發現,這個插件可以不使用,phpcs也可以運行的很好。
如果同時安裝 PHP Sniffer 和 phpcs 插件,但是沒有安裝 PHP_CodeSniffer 會出現以下錯誤
phpcs: Unable to locate phpcs. Invalid version string encountered!
PHP_CodeSniffer 典型使用
phpcs目錄下運行以下命令
./phpcs -i
The installed coding standards are MySource, PEAR, PSR1, PSR12, PSR2, Squiz and Zend
使用規范
1 方法注釋 參數,返回值三項用空行分割。
2 參數必須有注釋,也識別空格,多個參數縱向需要對齊。
3 每一行的代碼長度不能超過一定的長度。
Line exceeds 85 characters; contains 98 characters
4 私有方法使用下劃線 _ 開始。
Private method name " " must be prefixed with an underscore
關于PSR
眾所周知PHP語言是基于開源社區發展起來的,社區可以說又集中又分散,框架,類庫數量眾多,如何在最大程度上約束代碼規范,趨向于統一和標準化,有一個組織在推進這個事情,那就是FIG Framework Interop Group。phpcs這些代碼格式化工具也是以PSR為理論基礎。
The Framework Interop Group has proposed and approved a series of style recommendations.
One recommended way to use namespaces is outlined in PSR-4, which aims to provide a standard file, class and namespace convention to allow plug-and-play code.
業界最常用的規范標準成稿是PSR4,這里 https://phptherightway.com/#code_style_guide是一篇關于代碼規范的概要介紹,可以深入閱讀。
總結
VS Code 中對于插件 phpcs 安裝方式的介紹,實際上更多是 PHP_CodeSniffer 工具的安裝介紹,插件的作用就是幫助項目與 PHP_CodeSniffer 做關聯。要體驗到完善的 phpcs 使用體驗,還需要 PHP 智能注釋這個插件,自動為方法提供注釋,再手動做細節調整。
Code Review 是保證團隊代碼質量的重要措施之一,而代碼規范是 Code Review 的重要一環,而代碼規范又要借助于有效的工具,phpcs 就是這樣的工具,推動代碼整潔,高效開發,培養工程師對于代碼整潔度的潔癖和質量交付的嚴格約束。
文章已同步到公眾號《圖南科技》歡迎關注