PHP_CodeSniffer 使用攻略

來源:互聯網
上載者:User

目錄

安裝 PHP_CodeSniffer

安裝 phpcs

phpcs 是 PHP 代碼規範的偵查工具。

# 下載$ curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar# 加入到命令目錄$ mv phpcs.phar /usr/local/bin/phpcs# 賦予執行許可權$ sudo chmod +x /usr/local/bin/phpcs# 檢驗是否成功$ phpcs -h

安裝 phpcbf

phpcbf 是 PHP 代碼規範的修複工具。

# 下載$ curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar# 加入到命令目錄$ mv phpcbf.phar /usr/local/bin/phpcbf# 賦予執行許可權$ sudo chmod +x /usr/local/bin/phpcbf# 檢驗是否成功$ phpcbf -h

使用 phpcs

phpcs 配置

1.查看詳細配置。使用命令: phpcs --config-show(下面是我當前的配置)

colors:                  1default_standard:        PSR2encoding:                utf-8error_severity:          1ignore_errors_on_exit:   1ignore_warnings_on_exit: 1report_format:           summaryreport_width:            autoseverity:                1show_progress:           1show_warnings:           1tab_width:               4warning_severity:        8

2.設定預設的編碼通訊協定。(這個很重要,建議使用 PSR2的標準)

# 查看配置$ phpcs -iThe installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz and Zend# 設定編碼通訊協定為 PSR2$ phpcs --config-set default_standard PSR2

3.隱藏警告。(當然,對於強迫症來說,警告都是不允許的,非強迫症患者可以使用此配置項)

# 隱藏警告提醒$ phpcs --config-set show_warnings 0# 開啟警告提醒$ phpcs --config-set show_warnings 1

4.顯示檢查進程。(如果項目需要檢查的檔案較多可以開啟這個)

# 顯示檢查進程$ phpcs --config-set show_progress 1# 關閉進程顯示$ phpcs --config-set show_progress 0

5.顯示顏色。 (給自己點顏色看看哈)

# 顯示顏色$ phpcs --config-set colors 1# 關閉顏色顯示$ phpcs --config-set colors 0

6.修改錯誤和警告層級

# 顯示所有的錯誤和警告$ phpcs --config-set severity 1# 顯示所有的錯誤,部分警告 注意等級可有從 5-8 5 的警告顯示會更多,8 的更少$ phpcs --config-set severity 1$ phpcs --config-set warning_severity 5 

7.設定預設編碼

# 設定 utf-8$ phpcs --config-set encoding utf-8

8.設定 tab 的寬度

# tab 為 4 個空格$ phpcs --config-set tab_width 4# 也可以對單獨檔案生效$ phpcs --tab-width=0 /path/to/code

代碼驗證

1.校正單個檔案

# 校正單個檔案$ phpcs filename

2.校正目錄(如:整個項目)

# 校正目錄 注意這個時候別因為 linux 學的太好加個 -R 哈。$ phpcs /path/dir

結果分析

結果展現的形式:

full, xml, checkstyle, csv, json, emacs, source, summary, diff, svnblame, gitblame, hgblame or notifysend

指定展現形式:

# 匯總的形式phpcs --report=summary test01.php# json 形式 (個人覺得這個形式更清晰)phpcs --report=json test01.php

修複代碼

使用 phpcbf

覆蓋式修複

# 直接覆蓋$ phpcbf /path/code

產生中間檔案

# 產生新檔案$ phpcbf /path/to/code --suffix=.fixed

使用 diff

# 以 test.php 為例 會產生 test.php.diff 檔案$ phpcs --report-diff=test.php.diff test01.php

總結

記住,這隻是一個工具。但是, 工欲善其事,必先利其器。這裡可以打個小廣告利器 裡面是有很多好用的工具的。

如果,你需要應用到團隊,需要看你團隊使用的什麼架構。然後,根據架構適當的調整一下配置的 錯誤等級警告層級

參考連結

1. PHP_CodeSniffer_wiki

2. Configuration Options

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.