天天看點

windows環境PhpStorm中簡單使用PHP_CodeSniffer規範php代碼

為什麼使用PHP_CodeSniffer

一個開發團隊統一的編碼風格,有助于他人對代碼的了解和維護,對于大項目來說尤其重要。

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

什麼是Pear

由于PHP_CodeSniffer的安裝依賴PHP和Pear環境,那麼我們有必要了解下什麼是Pear。

來自

百度百科

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.php到php根目錄下面。

使用管理者方式打開指令行,輸入以下指令:

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繼續,出現如下:

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:      

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

/*省略*/
The 'pear' command is now at your service at c:\php\pear.bat
/*省略*/      
windows環境PhpStorm中簡單使用PHP_CodeSniffer規範php代碼

在php根目錄下面會看到如下幾個檔案:

windows環境PhpStorm中簡單使用PHP_CodeSniffer規範php代碼

輕按兩下pear.bat檔案,注冊pear到目前環境。

安裝PHP_CodeSniffer

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

1 pear install PHP_CodeSniffer      

等待安裝完成,安裝完成後php根目錄下回出現以下兩個檔案:

windows環境PhpStorm中簡單使用PHP_CodeSniffer規範php代碼

按照下圖依次打開檔案夾,在看如下目錄結構:

windows環境PhpStorm中簡單使用PHP_CodeSniffer規範php代碼

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

現在我們就可以使用這些規範來檢測我們的php代碼了,先說說在指令行中如何使用。

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

phpcs -h      

 看到的如下:

windows環境PhpStorm中簡單使用PHP_CodeSniffer規範php代碼

這裡我隻簡單的說明如何檢查單個檔案或整個檔案目錄:

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

 看到如下結果(單個檔案):

windows環境PhpStorm中簡單使用PHP_CodeSniffer規範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

windows環境PhpStorm中簡單使用PHP_CodeSniffer規範php代碼

上圖為解壓後圖

windows環境PhpStorm中簡單使用PHP_CodeSniffer規範php代碼

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

現在就可以使用CodeIgniter标準檢測代碼了:

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

PHPSTORM配置PHP_CodeSniffer檢測環境

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

windows環境PhpStorm中簡單使用PHP_CodeSniffer規範php代碼

點選如下進行編輯:

windows環境PhpStorm中簡單使用PHP_CodeSniffer規範php代碼

設定PHP Code Sniffer path為phpcs.bat的路徑。

windows環境PhpStorm中簡單使用PHP_CodeSniffer規範php代碼

 點選Validate,出現如下圖表示設定成功:

windows環境PhpStorm中簡單使用PHP_CodeSniffer規範php代碼

打開配置搜尋Inspections, 展開PHP,勾選PHP Code Sniffer validation, 選擇Coding standard為CodeIgniter, 點選OK确定。

windows環境PhpStorm中簡單使用PHP_CodeSniffer規範php代碼

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

windows環境PhpStorm中簡單使用PHP_CodeSniffer規範php代碼

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

配置到此結束,希望可以幫到需要的程式猿!

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

參考:

http://baike.baidu.com/subview/20453/16587839.htm