Grunt plugin for running PHP Code Sniffer

Note: This plugin is developed for Grunt 0.4.0 and is not tested for backward compatibility with Grunt 0.3.x.

Tired of doing repetitive task of checking your code if your following PSR 4 coding stadards? There is a more convenient way! Come! Let’s learn!

1. Install this grunt plugin with the following command:

npm install grunt-phpcs --save-dev

2. Install PHP Code Sniffer (preferably with composer)

3. Add this to your project’s Gruntfile.js gruntfile:

grunt.loadNpmTasks('grunt-phpcs');

PHP Code Sniffer task
Run this task with the grunt phpcs command.

Task targets, files and options may be specified according to the grunt configuring tasks guide.

Usage Example

        phpcs: {
            application: {
                src: ['app/htdocs/*.php']
            },
            options: {
                bin: '/usr/bin/phpcs',
                standard: 'Zend'
            }
        }

Custom callback
Do whatever you want with the output.

    function log(err, stdout, stderr, cb) {
        console.log(stdout);
        cb();
    }

    grunt.initConfig({

        phpcs: {
            application: {
                src: ['app/htdocs/*.php']
            },
            options: {
                bin: '/usr/bin/phpcs',
                standard: 'Zend'
            }
        }

    });

4. Sample full gruntfile.js contents.

'use strict';

module.exports = function(grunt) {
    // load config
    grunt.initConfig({

        phpcs: {
            application: {
                src: ['app/htdocs/*.php']
            },
            options: {
                bin: '/usr/bin/phpcs',
                standard: 'Zend'
            }
        }
    
    });
        
    // load plugins
    grunt.loadNpmTasks('grunt-phpcs');
    
    // define task 'default'
    grunt.registerTask('default', 'Log some stuff.', function() {
        grunt.log.write('Logging some stuff...').ok();
    });
    
};

5. Run the file we created.

Note: Intentionally, I added errors on that php file sample we used below.

Sample index.php file I used.


class Page_Index 
{
    $test = "hello world";         
    echo "hi there" . $test   ; 

    public function boo()   {
        echo "\n great! ";    
    }
}
    
$ini = new Page_Index();

$ini->boo();

----------------------------------------------------------------------

Run gruntfile like this on your current project root directory: 

project-root$>  grunt phpcs --force

Sample results:

Running "phpcs:application" (phpcs) task

FILE: /var/home/someone/excite/project-root/app/htdocs/index.php
----------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 2 LINES
----------------------------------------------------------------------
 4 | ERROR | [x] Expected 0 spaces before opening brace; 1 found
 8 | ERROR | [x] Opening brace should be on a new line
----------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

Time: 11ms; Memory: 2.5Mb

Done, but with warnings.

Now, you can see what things you need to adjust to comply to PSR 4 coding standards.

Hope you learn from this, until next time! For more grunt options you may try to use other options below.

Other Grunt Options
bin
Type: String Default: phpcs

maxBuffer
Type: Number Default: 200*1024

Set the buffer size.

verbose
Type: Boolean Default: false

Output more verbose information.

showSniffCodes
Type: Boolean Default: false

Show sniff codes in all reports.

severity
Type: Integer Default: false

The minimum severity required to display an error or warning.

warningSeverity
Type: Integer Default: false

The minimum severity required to display a warning.

errorSeverity
Type: Integer Default: false

The minimum severity required to display an error.

standard
Type: String Default: false

Define the standard to use.

report
Type: String Default: false

Report types and options

reportFile
Type: String Default: false

Log report to the file.

tabWidth
Type: Integer Default: false

Automatically convert tabs to the specified number of spaces when sniffing.