您好,登錄后才能下訂單哦!
VsCode插件phpcs安裝執(zhí)行過(guò)程中遇到的問(wèn)題及解決方法,相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
我們希望規(guī)范項(xiàng)目開(kāi)發(fā)中PHP代碼的編碼規(guī)范,鎖定了php-code-sniffer這款插件,在安裝執(zhí)行過(guò)程中遇到的問(wèn)題及理解分享如下
https://github.com/squizlabs/PHP_CodeSniffer/是一個(gè)代碼規(guī)范工具,用于規(guī)范團(tuán)隊(duì)開(kāi)發(fā)中的類(lèi)名,注釋?zhuān)瑓?shù)的等方面寫(xiě)法,幫助團(tuán)隊(duì)成員形成統(tǒng)一的編碼風(fēng)格。
有多種安裝方式,mac下采用以下命令安裝,最為方便
brew install php-code-sniffer
安裝路徑 /usr/local/Cellar/php-code-sniffer
/usr/local/Cellar/php-code-sniffer/3.4.1/binlsphpcbf phpcs
composer global require squizlabs/php_codesniffer
安裝路徑
/Users/name/.composer/vendor/squizlabs/php_codesniffer/
lsphpcbf phpcbf.bat phpcs phpcs.bat
兩種安裝方式,mac下推薦使用brew安裝
其它安裝方式參考github庫(kù)[PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer/)
VsCode中啟用PHP_CodeSniffer需要安裝phpcs插件。
Before using this plugin, you must ensure that phpcs is installed on your system,The preferred method is using [composer](https://getcomposer.org/) for both system-wide and project-wide installations.
也就是說(shuō)在VsCode中使用phpcs插件之前,需要首先在系統(tǒng)上安裝有phpcs,安裝方法是依賴(lài)于composer方式,并且可以從系統(tǒng)級(jí)和項(xiàng)目級(jí)兩個(gè)緯度選擇安裝。
如果只是在vscode中安裝插件,系統(tǒng)級(jí)中不安裝phpcs。會(huì)出現(xiàn)以下錯(cuò)誤
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.
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
以下是VScode 對(duì)于插件 phpcs 的介紹說(shuō)明,更多參考 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.
1. Install [composer](https://getcomposer.org/doc/00-intro.md "https://getcomposer.org/doc/00-intro.md").
2. Require `phpcs` package by typing the following at the root of your project in a terminal:
composer require --dev squizlabs/php_codesniffer
在沒(méi)有配置完成時(shí),又發(fā)現(xiàn)了另外一個(gè)插件 PHP Sniffer。在特性中寫(xiě)著兩條
Runs phpcs to lint PHP code.
Runs phpcbf to format fixable PHP code validation errors.
顯然可以推斷 PHP Sniffer 是對(duì)phpcs的封裝。驗(yàn)證后發(fā)現(xiàn),這個(gè)插件可以不使用,PHP_CodeSniffer也能夠正常啟用。
![VsCode插件巡禮-phpcs-圖南科技]
如果同時(shí)安裝 PHP Sniffer 和 phpcs插件,但是沒(méi)有安裝PHP_CodeSniffer會(huì)出現(xiàn)以下錯(cuò)誤
phpcs: Unable to locate phpcs. Invalid version string encountered!
phpcs -i
The installed coding standards are MySource, PEAR, PSR1, PSR12, PSR2, Squiz and Zend
1 方法注釋 參數(shù),返回值三項(xiàng)用空行分割
![phpcs.png]
2 參數(shù)必須有注釋?zhuān)沧R(shí)別空格,多個(gè)參數(shù)縱向需要對(duì)齊。
3 每一行的代碼長(zhǎng)度不能超過(guò)一定的長(zhǎng)度。
>Line exceeds 85 characters; contains 98 characters
4 私有方法使用下劃線開(kāi)始。
>Private method name " " must be prefixed with an underscore
VsCode中對(duì)于插件phpcs安裝方式的介紹,實(shí)際上更多是PHP_CodeSniffer工具的安裝介紹,插件的作用就是幫助項(xiàng)目與PHP_CodeSniffer做關(guān)聯(lián)。要體驗(yàn)到完善的phpcs使用體驗(yàn),還需要PHP智能注釋這個(gè)插件,自動(dòng)為方法提供注釋?zhuān)偈謩?dòng)做細(xì)節(jié)調(diào)整。
Code Review是保證團(tuán)隊(duì)代碼質(zhì)量的重要措施之一,而代碼規(guī)范是Code Review的重要一環(huán),而代碼規(guī)范又要借助于有效的工具,phpcs就是這樣的工具,推動(dòng)代碼整潔,高效開(kāi)發(fā),培養(yǎng)工程師對(duì)于代碼整潔度的潔癖和質(zhì)量交付的嚴(yán)格約束。
看完上述內(nèi)容,你們掌握VsCode插件phpcs安裝執(zhí)行過(guò)程中遇到的問(wèn)題及解決方法的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。