[轉載]使用PHP_CodeSniffer規範php代碼

爲何使用PHP_CodeSniffer

一個開發團隊統一的編碼風格,有助於他人對代碼的理解和維護,對於大項目來講尤爲重要。php

PHP_CodeSniffer是PEAR中的一個用PHP5寫的用來檢查嗅探PHP代碼是否有違反一組預先設置好的編碼標準的一個包,它是確保你的代碼簡潔一致的必不可少的開發工具,甚至還能夠幫助程序員減小一些語義錯誤。html

什麼是Pear

因爲PHP_CodeSniffer的安裝依賴PHP和Pear環境,那麼咱們有必要了解下什麼是Pear。git

來自百度百科程序員

PEAR是PHP擴展與應用庫(the PHP Extension and Application Repository)的縮寫。它是一個PHP擴展及應用的一個代碼倉庫,簡單地說,PEAR之於PHP就像是CPAN(Comprehensive Perl Archive Network)之於Perl。
PEAR的基本目標是發展成爲PHP擴展和庫代碼的知識庫,而這個項目最有雄心的目標則是試圖定義一種標準,這種標準將幫助開發者編寫可移植、可重用的代碼。

安裝Pear

在已經安裝了PHP環境的前提下,進入php目錄,若是沒有go-pear.php文件,就到http://pear.php.net/go-pear.phar下載go-pear.php文件,該地址在瀏覽器打開能夠看到一段PHP的代碼,直接保存文件另存爲go-pear.phar到php根目錄下面。這裏提供我的百度網盤下載:https://pan.baidu.com/s/1jIy6HEmgithub

使用管理員方式打開命令行,輸入如下命令:web

1 cd c:\php
2 php go-pear.phar

 

這是出現:瀏覽器

1 Are you installing a system-wide PEAR or a local copy?
2 (system|local) [system] :

 

直接回車默認system繼續,出現以下:app

複製代碼
1 Below is a suggested file layout for your new PEAR installation.  To
 2 change individual locations, type the number in front of the
 3 directory.  Type 'all' to change all of them or simply press Enter to
 4 accept these locations.
 5 
 6  1. Installation base ($prefix)                   : C:\php
 7  2. Temporary directory for processing            : C:\php\tmp
 8  3. Temporary directory for downloads             : C:\php\tmp
 9  4. Binaries directory                            : C:\php
10  5. PHP code directory ($php_dir)                 : C:\php\pear
11  6. Documentation directory                       : C:\php\docs
12  7. Data directory                                : C:\php\data
13  8. User-modifiable configuration files directory : C:\php\cfg
14  9. Public Web Files directory                    : C:\php\www
15 10. System manual pages directory                 : C:\php\man
16 11. Tests directory                               : C:\php\tests
17 12. Name of configuration file                    : C:\WINDOWS\pear.ini
18 13. Path to CLI php.exe                           : C:\php
19 
20 1-13, 'all' or Enter to continue:
複製代碼

 

直接回車,出現以下,表示安裝成功,phpstorm

/*省略*/
The 'pear' command is now at your service at c:\php\pear.bat
/*省略*/

在php根目錄下面會看到以下幾個文件:ide

雙擊pear.bat文件,註冊pear到當前環境。

安裝PHP_CodeSniffer

在安裝完pear以後,就能夠安裝php_CodeSniffer了,繼續在cmd中輸入:

1 pear install PHP_CodeSniffer

等待安裝完成,安裝完成後php根目錄下回出現如下兩個文件:

按照下圖依次打開文件夾,在看以下目錄結構:

在php->pear->PHP->CodeSniffer->Standards中能夠看到一些php的規範,Generic是通用規範。

如今咱們就可使用這些規範來檢測咱們的php代碼了,先說說在命令行中如何使用。

咱們可使用phpcs -h來看看使用幫助:

phpcs -h

 看到的以下:

這裏我只簡單的說明如何檢查單個文件或整個文件目錄:

1 phpcs -n F:\Hg\web\application\controllers\  //檢測文件目錄
2 phpcs -n F:\Hg\web\application\controllers\home_controller.php  //檢測單個文件

 看到以下結果(單個文件):

這樣,咱們就能夠根據這些錯誤信息去修改咱們的代碼,使其符合規範。

咱們能夠指定使用某一個規範進行檢測,方法以下:

1 phpcs -n --standard=Zend F:\Hg\web\application\controllers\

 

不指定標準,會使用php通用規範Generic。

安裝CodeIgniter標準

https://github.com/thomas-ernest/CodeIgniter-for-PHP_CodeSniffer下載包解壓,複製src目錄到php->pear->PHP->CodeSniffer->Standards目錄下,而且更名爲CodeIgniter

上圖爲解壓後圖

上圖爲放到php代碼規範下後的圖。

如今就可使用CodeIgniter標準檢測代碼了:

1 phpcs -n --standard=CodeIgniter F:\Hg\web\application\controllers\

 

PHPSTORM配置PHP_CodeSniffer檢測環境

 打開phpstorm的配置框,找到Languages & Frameworks -> php-> Code Sniffer,不一樣版本的phpstorm可能會有出入,直接搜索Code Sniffer也能夠。

點擊以下進行編輯:

設置PHP Code Sniffer path爲phpcs.bat的路徑。

 點擊Validate,出現以下圖表示設置成功:

打開配置搜索Inspections, 展開PHP,勾選PHP Code Sniffer validation, 選擇Coding standard爲CodeIgniter, 點擊OK肯定。

接下來,在編碼PHP的時候就會出現規範提示

 

如上圖,鼠標移動到有波浪提示的地方,就會出現phpcs的規範提示了。

配置到此結束,但願能夠幫到須要的程序猿!

 

最規範的代碼就是不出現任何的波浪提示。

原文連接:http://www.cnblogs.com/huangbx/p/php_codesniffer.html

相關文章
相關標籤/搜索