Skip to content
This repository was archived by the owner on Apr 3, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,8 @@ Log report to the file.
Type: `Integer` Default: `false`

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

####force
Type: `Boolean` Default: `false`

Continue running grunt in the event of failures.
36 changes: 20 additions & 16 deletions tasks/phpcs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = function(grunt) {

var path = require('path'),
exec = require('child_process').exec;

var command = {
flags: {
verbose: 'v',
Expand All @@ -31,43 +31,47 @@ module.exports = function(grunt) {
defaults = {
bin: 'phpcs',
report: 'full',
maxBuffer: 200*1024
maxBuffer: 200*1024,
force: false
},
done = null;

grunt.registerMultiTask('phpcs', 'Run PHP Code Sniffer', function() {
var done = null,
parameters = null,
target = this.target,
options = this.options(defaults),
execute = path.normalize(options.bin),
files = [].concat.apply([], this.files.map(function(mapping) { return mapping.src; })).sort();

// removes duplicate files
files = files.filter(function(file, position) {
files = files.filter(function(file, position) {
return !position || file !== files[position - 1];
});

// generates parameters
parameters = Object.keys(options).map(function(option) {
return option in command.flags && options[option] === true ?
'-' + command.flags[option] : option in command.options && options[option] !== undefined ?
return option in command.flags && options[option] === true ?
'-' + command.flags[option] : option in command.options && options[option] !== undefined ?
'--' + command.options[option] + '=' + options[option] : null;
}).filter(Boolean);

execute += ' ' + parameters.join(' ') + ' "' + files.join('" "') + '"';

grunt.verbose.writeln('Executing: ' + execute);

done = this.async();

exec(execute, {maxBuffer: options.maxBuffer}, function(error, stdout, stderr) {
/* jshint -W030 */
typeof options.callback === 'function' && options.callback.call(this, error, stdout, stderr, done);
stdout && grunt.log.write(stdout);
error && grunt.fail.warn(stderr ? stderr : 'Task phpcs:' + target + ' failed.');
!error && grunt.log.ok(files.length + ' file' + (files.length === 1 ? '' : 's') + ' lint free.');
done(error);
if (!options.force && error) {
grunt.fail.warn(stderr ? stderr : 'Task phpcs:' + target + ' failed.');
} else if (!error) {
grunt.log.ok(files.length + ' file' + (files.length === 1 ? '' : 's') + ' lint free.');
}
done();
});
});
};
};