PHP_CodeSniffer 檢查程式碼標準 phpcs phpcbf

PHP_CodeSniffer 是一個檢查程式碼是否符合標準的工具,裡面有兩個 PHP scripts,phpcs, phpcbf

安裝

有幾種方法來安裝 PHP_CodeSniffer

直接下載 Phar 檔

curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
php phpcs.phar -h

curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar
php phpcbf.phar -h

用 Composer 安裝

composer global require "squizlabs/php_codesniffer=*"

./vendor/bin/phpcs -h
./vendor/bin/phpcbf -h

phpcs

會建設定檔 CodeSniffer.conf 可設定設定值

phpcbf

範例

  1. 設定標準預設值為 PSR-2 default_standard=PSR2

    $ phpcs --config-set default_standard PSR2
    Config value "default_standard" added successfully
    
  2. 設定顏色開啟 colors=1

    $ phpcs --config-set colors 1
    Config value "colors" added successfully
    
  3. 一個範例檔案 demo.php

    <?php
    
    class demo {
        protected $num;
    
        function __construct(int $num = 0)
        {
            if($num != 0){
                $this->num = $num;
            }
        }
    }
    
    ?>
    
  4. phpcs 檢查

    $ phpcs demo.php
    
    FILE: F:\Coding\Project\test\demo.php
    ----------------------------------------------------------------------
    FOUND 7 ERRORS AFFECTING 4 LINES
    ----------------------------------------------------------------------
      3 | ERROR | [ ] Each class must be in a namespace of at least one
        |       |     level (a top-level vendor name)
      3 | ERROR | [ ] Class name "demo" is not in camel caps format
      3 | ERROR | [x] Opening brace of a class must be on the line after
        |       |     the definition
      6 | ERROR | [ ] Visibility must be declared on method "__construct"
      8 | ERROR | [x] Expected 1 space after IF keyword; 0 found
      8 | ERROR | [x] Expected 1 space after closing parenthesis; found 0
     14 | ERROR | [x] A closing tag is not permitted at the end of a PHP
        |       |     file
    ----------------------------------------------------------------------
    PHPCBF CAN FIX THE 4 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ----------------------------------------------------------------------
    
    Time: 40ms; Memory: 4Mb
    

    有4個地方可以用 phpcbf 去修正 ( [X] 的部分)

  5. phpcbf 修正

    $ phpcbf demo.php
    Changing into directory F:\Coding\Project\test
    Processing demo.php [PHP => 63 tokens in 14 lines]... DONE in 7ms (4 fixable violations)
            => Fixing file: 0/4 violations remaining [made 3 passes]... DONE in 21ms
    Patched 1 file
    Time: 129ms; Memory: 6Mb
    
  6. 修正完的 demo.php

    <?php
    
    class demo
    {
        protected $num;
    
        function __construct(int $num = 0)
        {
            if ($num != 0) {
                $this->num = $num;
            }
        }
    }
    
    
  7. phpcs 再檢查一次

    $ phpcs demo.php
    
    FILE: F:\Coding\Project\test\demo.php
    ----------------------------------------------------------------------
    FOUND 3 ERRORS AFFECTING 2 LINES
    ----------------------------------------------------------------------
     3 | ERROR | Each class must be in a namespace of at least one level
       |       | (a top-level vendor name)
     3 | ERROR | Class name "demo" is not in camel caps format
     7 | ERROR | Visibility must be declared on method "__construct"
    ----------------------------------------------------------------------
    
    Time: 39ms; Memory: 4Mb
    
  8. 手動修正 demo.php

    <?php
    
    namespace Clouding\test;
    
    class Demo
    {
        protected $num;
    
        public function __construct(int $num = 0)
        {
            if ($num != 0) {
                $this->num = $num;
            }
        }
    }
    

PhpStorm

version: 2017.1.3

phpcs

PhpStorm 也有支援 PHP_CodeSniffer,隨時檢查程式碼有沒有符合標準。

設定

  1. Languages & Frameworks > PHP > Code Sniffer > ... phpcs_1.png

  2. 新增 phpcs 路徑,後按 Validate 驗證是否正確 phpcs_2.png > Windwos系統 要指向 phpcs.bat > 如果直接下載 phar 檔沒有 .bat,自己生一個 > echo @php “%~dp0phpcs.phar” %* > phpcs.bat

  3. Editor > InspectionsPHP > PHP Code Sniffer validation打勾並設定 Coding standrad PSR2 phpcs_3.png

測試

多了波浪警告不符合的地方 phpcs_4.png

參考資料