PHP_CodeSniffer

什麼是 PHP_CodeSniffer?

PHP_CodeSniffer 是一個代碼風格檢測工具。它包含兩類腳本,phpcs 和 phpcbf。

phpcs 是能依照你設定的官方程式碼規範進行自動偵錯的腳本。

phpcbf 是能自動幫你將不符合程式碼規範的程式碼改成符合規範的程式碼,但某些地方還是要手動修正。


什麼是 Composer?

因為我們安裝時會用到 Composer 所以就來簡單說明一下什麼是 Composer。

Composer 是 PHP 常用的套件管理機制,它可以在我們的專案引入別人或自己寫好的套件。

安裝

https://getcomposer.org/download/ 去官網下載 Composer

win鍵 + R 並輸入 %appdata% 裡面有 Composer 的資料夾是它下載好後應該要在的位置

打開 CMD 並輸入 composer 看到這個畫面表示安裝成功

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

常見指令

composer init 這個指令能自動幫你建立 composer.json 檔案,也會自動幫你新增 composer.json 中需要的設定

composer require "XXXXXX" 這個指令安裝的套件只能在下指令的那個專案中使用

composer global require "XXXXXX" 這個指令安裝的套件能在全部有 Composer 的專案中使用


如何安裝 PHP_CodeSniffer?

Composer 全域安裝 (composer global require "squizlabs/php_codesniffer=*")

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

這樣就安裝好了

設定 phpcs 的預設檢查標準(phpcs config-set default_standard XXXX)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


PHP_CodeSniffer 效果

  • phpcs

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • phpcbf

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →


參考資料

PHP_CodeSniffer 的 github
Composer 官網