├── .gitignore ├── .jshintrc ├── .npmignore ├── .travis.yml ├── Gruntfile.js ├── LICENSE ├── README.md ├── composer.json ├── package.json ├── php └── modules │ └── Grunt │ └── PhpCs │ └── World.php └── tasks └── phpcs.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | tmp 4 | composer.lock 5 | vendor 6 | .DS_Store -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "curly": true, 3 | "eqeqeq": true, 4 | "immed": true, 5 | "latedef": true, 6 | "newcap": true, 7 | "noarg": true, 8 | "sub": true, 9 | "undef": true, 10 | "boss": true, 11 | "eqnull": true, 12 | "node": true 13 | } -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .npm-debug.log 3 | tmp 4 | composer.lock 5 | vendor -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: 2 | - node_js 3 | node_js: 4 | - "0.8" 5 | - "0.10" 6 | before_install: 7 | - sudo apt-get update -qq 8 | - sudo apt-get install -qq php5 9 | install: 10 | - curl -sS https://getcomposer.org/installer | php 11 | before_script: 12 | - php --version 13 | - php composer.phar composer --version 14 | - php composer.phar install 15 | - npm install -g grunt-cli 16 | - npm install 17 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | /* 2 | * grunt-phpunit 3 | * https://github.com/SaschaGalley/grunt-phpunit 4 | * 5 | * Copyright (c) 2013 Sascha Galley 6 | * http://xash.at 7 | * Licensed under the MIT license. 8 | */ 9 | 'use strict'; 10 | 11 | module.exports = function(grunt) { 12 | 13 | // Project configuration. 14 | grunt.initConfig({ 15 | 16 | jshint: { 17 | all: [ 18 | 'Gruntfile.js', 19 | 'tasks/*.js' 20 | ], 21 | options: { 22 | jshintrc: '.jshintrc' 23 | } 24 | }, 25 | 26 | phpcs: { 27 | options: { 28 | bin: 'vendor/bin/phpcs', 29 | standard: 'PSR2' 30 | }, 31 | application: { 32 | expand: true, 33 | cwd: 'php', 34 | src: ['**/*.php'] 35 | } 36 | } 37 | }); 38 | 39 | // Actually load this plugin's task(s). 40 | grunt.loadTasks('tasks'); 41 | 42 | grunt.loadNpmTasks('grunt-contrib-jshint'); 43 | 44 | // By default, lint and run all tests. 45 | grunt.registerTask('default', ['jshint', 'phpcs']); 46 | 47 | }; 48 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Sascha Galley 2 | 3 | Permission is hereby granted, free of charge, to any person 4 | obtaining a copy of this software and associated documentation 5 | files (the "Software"), to deal in the Software without 6 | restriction, including without limitation the rights to use, 7 | copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the 9 | Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # grunt-phpcs 2 | 3 | > Grunt plugin for running PHP Code Sniffer. 4 | 5 | _This plugin is developed for Grunt `0.4.0` and is not tested for backward compatibility with Grunt `0.3.x`._ 6 | 7 | ##Getting Started 8 | 1. Install this grunt plugin with the following command: 9 | 10 | ```shell 11 | npm install grunt-phpcs --save-dev 12 | ``` 13 | 14 | 2. Install [PHP Code Sniffer](https://github.com/squizlabs/PHP_CodeSniffer#installation) (preferably with [composer](https://github.com/composer/composer)) 15 | 16 | 3. Add this to your project's `Gruntfile.js` gruntfile: 17 | 18 | ```js 19 | grunt.loadNpmTasks('grunt-phpcs'); 20 | ``` 21 | 22 | ##PHP Code Sniffer task 23 | _Run this task with the `grunt phpcs` command._ 24 | 25 | Task targets, files and options may be specified according to the grunt [Configuring tasks](http://gruntjs.com/configuring-tasks) guide. 26 | 27 | ###Usage Example 28 | 29 | ```js 30 | phpcs: { 31 | application: { 32 | src: ['application/classes/*.php', 'application/lib/**/*.php'] 33 | }, 34 | options: { 35 | bin: 'vendor/bin/phpcs', 36 | standard: 'Zend' 37 | } 38 | } 39 | ``` 40 | 41 | ### Custom callback 42 | 43 | Do whatever you want with the output. 44 | 45 | ```js 46 | function log(err, stdout, stderr, cb) { 47 | console.log(stdout); 48 | cb(); 49 | } 50 | grunt.initConfig({ 51 | phpcs: { 52 | application: { 53 | src: ['application/classes/*.php', 'application/lib/**/*.php'] 54 | }, 55 | options: { 56 | callback: log 57 | } 58 | } 59 | }); 60 | ``` 61 | 62 | ###Options 63 | ####bin 64 | Type: `String` Default: `'phpcs'` 65 | 66 | ####maxBuffer 67 | Type: `Number` Default: `200*1024` 68 | 69 | Set the buffer size. 70 | 71 | ####verbose 72 | Type: `Boolean` Default: `false` 73 | 74 | Output more verbose information. 75 | 76 | ####showSniffCodes 77 | Type: `Boolean` Default: `false` 78 | 79 | Show sniff codes in all reports. 80 | 81 | ####severity 82 | Type: `Integer` Default: `false` 83 | 84 | The minimum severity required to display an error or warning. 85 | 86 | ####warningSeverity 87 | Type: `Integer` Default: `false` 88 | 89 | The minimum severity required to display a warning. 90 | 91 | ####errorSeverity 92 | Type: `Integer` Default: `false` 93 | 94 | The minimum severity required to display an error. 95 | 96 | ####standard 97 | Type: `String` Default: `false` 98 | 99 | Define the standard to use. 100 | 101 | ####report 102 | Type: `String` Default: `false` 103 | 104 | Report types and options 105 | 106 | ####reportFile 107 | Type: `String` Default: `false` 108 | 109 | Log report to the file. 110 | 111 | ####tabWidth 112 | Type: `Integer` Default: `false` 113 | 114 | Automatically convert tabs to the specified number of spaces when sniffing. 115 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "buildsuite", 3 | "type": "library", 4 | "require": { 5 | "php": ">=5.3.2", 6 | "seld/jsonlint": "1.*", 7 | "symfony/console": "~2.1@dev", 8 | "symfony/finder": "~2.1", 9 | "symfony/process": "~2.1@dev", 10 | "squizlabs/php_codesniffer": "1.*" 11 | } 12 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "grunt-phpcs", 3 | "description": "Grunt plugin for running PHP Code Sniffer.", 4 | "homepage": "https://github.com/SaschaGalley/grunt-phpcs", 5 | "author": { 6 | "name": "Sascha Galley", 7 | "email": "me@xash.at", 8 | "url": "http://xash.at/" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git://github.com/SaschaGalley/grunt-phpcs.git" 13 | }, 14 | "contributors": [{ 15 | "name": "James Cryer", 16 | "email": "chat@jamescryer.com", 17 | "url": "http://www.jamescryer.com/" 18 | }], 19 | "version": "0.4.0", 20 | "main": "Gruntfile.js", 21 | "engines": { 22 | "node": "0.10.x" 23 | }, 24 | "scripts": { 25 | "test": "grunt" 26 | }, 27 | "devDependencies": { 28 | "grunt-contrib-jshint": "^0.11.0", 29 | "grunt": "^0.4.5" 30 | }, 31 | "keywords": [ 32 | "gruntplugin", 33 | "phpcs" 34 | ] 35 | } 36 | -------------------------------------------------------------------------------- /php/modules/Grunt/PhpCs/World.php: -------------------------------------------------------------------------------- 1 | name = $name; 26 | } 27 | 28 | /** 29 | * Sets the world's name 30 | * 31 | * @param string $name 32 | */ 33 | public function setName($name) 34 | { 35 | $this->name = $name; 36 | } 37 | 38 | /** 39 | * Returns world's name 40 | * 41 | * @return string 42 | */ 43 | public function getName() 44 | { 45 | return $this->name; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /tasks/phpcs.js: -------------------------------------------------------------------------------- 1 | /* 2 | * grunt-phpcs 3 | * https://github.com/SaschaGalley/grunt-phpcs 4 | * 5 | * Copyright (c) 2013 Sascha Galley 6 | * http://xash.at 7 | * Licensed under the MIT license. 8 | */ 9 | 'use strict'; 10 | 11 | module.exports = function(grunt) { 12 | 13 | var path = require('path'), 14 | exec = require('child_process').exec; 15 | 16 | var command = { 17 | flags: { 18 | verbose: 'v', 19 | showSniffCodes: 's' 20 | }, 21 | options: { 22 | errorSeverity: 'error-severity', 23 | report: 'report', 24 | reportFile: 'report-file', 25 | severity: 'severity', 26 | standard: 'standard', 27 | warningSeverity: 'warning-severity', 28 | tabWidth: 'tab-width' 29 | } 30 | }, 31 | defaults = { 32 | bin: 'phpcs', 33 | report: 'full', 34 | maxBuffer: 200*1024 35 | }, 36 | done = null; 37 | 38 | grunt.registerMultiTask('phpcs', 'Run PHP Code Sniffer', function() { 39 | var done = null, 40 | parameters = null, 41 | target = this.target, 42 | options = this.options(defaults), 43 | execute = path.normalize(options.bin), 44 | files = [].concat.apply([], this.files.map(function(mapping) { return mapping.src; })).sort(); 45 | 46 | // removes duplicate files 47 | files = files.filter(function(file, position) { 48 | return !position || file !== files[position - 1]; 49 | }); 50 | 51 | // generates parameters 52 | parameters = Object.keys(options).map(function(option) { 53 | return option in command.flags && options[option] === true ? 54 | '-' + command.flags[option] : option in command.options && options[option] !== undefined ? 55 | '--' + command.options[option] + '=' + options[option] : null; 56 | }).filter(Boolean); 57 | 58 | execute += ' ' + parameters.join(' ') + ' "' + files.join('" "') + '"'; 59 | 60 | grunt.verbose.writeln('Executing: ' + execute); 61 | 62 | done = this.async(); 63 | 64 | exec(execute, {maxBuffer: options.maxBuffer}, function(error, stdout, stderr) { 65 | /* jshint -W030 */ 66 | typeof options.callback === 'function' && options.callback.call(this, error, stdout, stderr, done); 67 | stdout && grunt.log.write(stdout); 68 | error && grunt.fail.warn(stderr ? stderr : 'Task phpcs:' + target + ' failed.'); 69 | !error && grunt.log.ok(files.length + ' file' + (files.length === 1 ? '' : 's') + ' lint free.'); 70 | done(error); 71 | }); 72 | }); 73 | }; --------------------------------------------------------------------------------