PHPCS – PHP Code Sniffer

https://github.com/squizlabs/PHP_CodeSniffer/wiki

About

PHP Codesniffer adalah set dari dua script PHP: phpcs adalah script yang mendeteksi file PHP, javascript dan CSS dari pelanggaran standard coding yang ditentukan, kemudian yang kedua phpcbf adalah script untuk melakukan perbaikan secara automatis dari standard coding yang ditentukan. PHP Codesniffer adalah tool development esensial yang menjamin kode tetap bersih dan konsisten.

Requirement

PHP Codesniffer membutuhkan PHP versi 5.1.2 atau lebih baru, beberapa aturan sniff mungkin membutuhkan requirement lain.

Installation

Recommended: composer

composer global require "squizlabs/php_codesniffer=*"

pastikan /.composer/vendor/bin ada di PATH

Usage

Untuk mengecek apakah file melanggar standard coding PEAR, execute command berikut di shell.

$ phpcs /path/to/code/myfile.php

FILE: /path/to/code/myfile.php
--------------------------------------------------------------------------------
FOUND 5 ERROR(S) AFFECTING 2 LINE(S)
--------------------------------------------------------------------------------
  2 | ERROR | Missing file doc comment
 20 | ERROR | PHP keywords must be lowercase; expected "false" but found "FALSE"
 47 | ERROR | Line not indented correctly; expected 4 spaces but found 1
 51 | ERROR | Missing function doc comment
 88 | ERROR | Line not indented correctly; expected 9 spaces but found 6
--------------------------------------------------------------------------------

 

Atau jika ingin mengecek semua file PHP di dalam direktori, bisa dispesifikasikan direktori saja

$ phpcs /path/to/code
FILE: /path/to/code/myfile.php
--------------------------------------------------------------------------------
FOUND 5 ERROR(S) AFFECTING 5 LINE(S)
--------------------------------------------------------------------------------
  2 | ERROR | Missing file doc comment
 20 | ERROR | PHP keywords must be lowercase; expected "false" but found "FALSE"
 47 | ERROR | Line not indented correctly; expected 4 spaces but found 1
 51 | ERROR | Missing function doc comment
 88 | ERROR | Line not indented correctly; expected 9 spaces but found 6
--------------------------------------------------------------------------------

FILE: /path/to/code/yourfile.php
--------------------------------------------------------------------------------
FOUND 1 ERROR(S) AND 1 WARNING(S) AFFECTING 1 LINE(S)
--------------------------------------------------------------------------------
 21 | ERROR   | PHP keywords must be lowercase; expected "false" but found
    |         | "FALSE"
 21 | WARNING | Equals sign not aligned with surrounding assignments
--------------------------------------------------------------------------------

Rekomendasi

  • Gunakan PSR 2 sebagai standard untuk pengecekan code standard violation.
  • Gunakan integrasi dengan IDE atau text editor untuk mempermudah development.

Contoh

Misalkan ada file PHP sebagai berikut

<?php

class Test_My_class
{
    public function index_test()
    {
        $varA = TRUE;

        if($varA) {
            echo 'T';
        }
    }

    private function test_argument($test1,$test2)
    {
        $array = array(1,2);

        if($test1)
        {
            foreach($array as $value){
                echo $value;
            }
        }
    }
}

 

Jika kita jalankan pengecekan phpcs terhadap file tersebut

phpcs --standard=PSR2 test.php 

FILE: /Users/libreorca/Htdocs/test/phpcs/test.php
----------------------------------------------------------------------
FOUND 11 ERRORS AFFECTING 7 LINES
----------------------------------------------------------------------
  3 | ERROR | [ ] Each class must be in a namespace of at least one
    |       |     level (a top-level vendor name)
  3 | ERROR | [ ] Class name "Test_My_class" is not in camel caps
    |       |     format
  5 | ERROR | [ ] Method name "Test_My_class::index_test" is not in
    |       |     camel caps format
  7 | ERROR | [x] TRUE, FALSE and NULL must be lowercase; expected
    |       |     "true" but found "TRUE"
  9 | ERROR | [x] Expected 1 space after IF keyword; 0 found
 14 | ERROR | [ ] Method name "Test_My_class::test_argument" is not
    |       |     in camel caps format
 14 | ERROR | [x] Expected 1 space between comma and argument
    |       |     "$test2"; 0 found
 18 | ERROR | [x] Expected 1 space after IF keyword; 0 found
 18 | ERROR | [x] Expected 1 space after closing parenthesis; found 9
 20 | ERROR | [x] Expected 1 space after FOREACH keyword; 0 found
 20 | ERROR | [x] Expected 1 space after closing parenthesis; found 0
----------------------------------------------------------------------
PHPCBF CAN FIX THE 7 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

Time: 19ms; Memory: 3.5Mb

 

Sesuai dengan standard PSR2, kode diatas banyak melanggar standard coding. Ada beberapa pelanggaran yang bisa difix dengan phpcbf secara automatis. Hasilnya bisa dicek dibawah

phpcbf --standard=PSR2 test.php 
Changing into directory /Users/libreorca/Htdocs/test/phpcs
Processing test.php [PHP => 116 tokens in 25 lines]... DONE in 4ms (7 fixable violations)
        => Fixing file: 0/7 violations remaining [made 6 passes]... DONE in 20ms
Patched 1 file
Time: 59ms; Memory: 3.75Mb

phpcs --standard=PSR2 test.php 

FILE: /Users/libreorca/Htdocs/test/phpcs/test.php
----------------------------------------------------------------------
FOUND 4 ERRORS AFFECTING 3 LINES
----------------------------------------------------------------------
  3 | ERROR | Each class must be in a namespace of at least one level
    |       | (a top-level vendor name)
  3 | ERROR | Class name "Test_My_class" is not in camel caps format
  5 | ERROR | Method name "Test_My_class::index_test" is not in camel
    |       | caps format
 14 | ERROR | Method name "Test_My_class::test_argument" is not in
    |       | camel caps format
----------------------------------------------------------------------

Time: 25ms; Memory: 3.5Mb

 

Hasil file setelah difix phpcbf secara automatis

<?php

class Test_My_class
{
    public function index_test()
    {
        $varA = true;

        if ($varA) {
            echo 'T';
        }
    }

    private function test_argument($test1, $test2)
    {
        $array = array(1,2);

        if ($test1) {
            foreach ($array as $value) {
                echo $value;
            }
        }
    }
}

 

Beberapa pelanggaran sudah di fix automatis oleh phpcbf, tapi file tersebut masih ada pelanggaran terhadap standard PSR2, kita perlu melakukan refactor manual.

<?php
namespace Phpcs\Test;

class TestMyClass
{
    public function indexTest()
    {
        $varA = true;

        if ($varA) {
            echo 'T';
        }
    }

    private function testArgument($test1, $test2)
    {
        $array = array(1,2);

        if ($test1) {
            foreach ($array as $value) {
                echo $value;
            }
        }
    }
}

 

Jika kita re-run phpcs dan hasilnya kosong, maka file PHP sudah mengikuti standard yang ditentukan (dalam kasus ini PSR2).

phpcs --standard=PSR2 test.php
*empty string*

Remark

  • Perhatikan kode hasil refactor phpcs tidak mendeteksi unusued code (function testArgument, dan argumen test2), oleh sebab itu phpcs digunakan berdampingan dengan phpmd untuk hasil yang optimum.
  • Lakukan phpcs secara berkala untuk menjaga kualitas koding – atau integrasikan dengan IDE / text editor masing masing.

Credit to: Yusuf Irwandi

Leave a Reply

Your email address will not be published. Required fields are marked *