Last Updated: February 25, 2016
·
974
· vfragosop

Baseline Gruntfile for PHP development

These are my boilerplate dependencies and gruntfile for PHP development.

gruntfile.js

grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    phpcs: {
        application: {
            dir: ['src']
        },
        options: {
            bin: 'vendor/bin/phpcs',
            standard: 'PSR2'
        }
    },
    phpcpd: {
        application: {
            dir: 'src'
        },
        options: {
            bin: 'vendor/bin/phpcpd',
            quiet: true
        }
    },
    phpmd: {
        application: {
            dir: 'src'
        },
        options: {
            bin: 'vendor/bin/phpmd',
            reportFormat: 'text'
        }
    },
    phpunit: {
        tests: {
            dir: 'tests'
        },
        options: {
            bin: 'vendor/bin/phpunit',
            colors: true
        }
    },
    watch: {
        src: {
            files: [ 'src/**/*.php' ],
            tasks: [ 'clear', 'tests', 'quality' ],
            options: {
                atBegin: true,
            }
        },
        tests: {
            files: [ 'tests/**/*.php' ],
            tasks: [ 'clear', 'tests' ]
        }
    }
});

grunt.loadNpmTasks('grunt-clear');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-notify');
grunt.loadNpmTasks('grunt-phpcs');
grunt.loadNpmTasks('grunt-phpcpd');
grunt.loadNpmTasks('grunt-phpmd');
grunt.loadNpmTasks('grunt-phpunit');

grunt.registerTask('tests', [
    'phpunit'
]);

grunt.registerTask('quality', [
    'phpcs',
    'phpcpd',
    'phpmd'
]);

grunt.registerTask('default', [
    'clear',
    'tests',
    'quality'
]);

package.json

"devDependencies": {
    "grunt": "~0.4.4",
    "grunt-clear": "~0.2.1",
    "grunt-contrib-watch": "~0.6.0",
    "grunt-notify": "~0.2.20",
    "grunt-phpcs": "~0.2.2",
    "grunt-phpcpd": "~0.1.3",
    "grunt-phpmd": "~0.1.0",
    "grunt-phpunit": "~0.3.3"
}

composer.json

Important: Remove the spaces after the @ (it's a markdown issue)

"require-dev": {
    "phpmd/phpmd": "2.0.*@ dev",
    "phpunit/phpunit": "4.1.*@ dev",
    "sebastian/phpcpd": "2.0.*@ dev",
    "squizlabs/php_codesniffer": "2.0.*@ dev"
}