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
/*省略*/

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

雙擊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://baike.baidu.com/subview/20453/16587839.htm


免責聲明!

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



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