Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alappe committed Jul 11, 2013
0 parents commit 355ca42
Show file tree
Hide file tree
Showing 6 changed files with 295 additions and 0 deletions.
104 changes: 104 additions & 0 deletions README.md
@@ -0,0 +1,104 @@
# grunt-phpmd

> Grunt plugin for running [PHP Mess Detector](http://phpmd.org).
_This plugin is developed for Grunt `0.4.0` and is not tested for backward compatibility with Grunt `0.3.x`._

## Getting Started

1. Install this grunt plugin with the follwing command:

```shell
npm install grunt-phpmd --save-dev
```

2. [Install PHPMD](http://phpmd.org/download/index.html)

3. Add this to your project's `Gruntfile.js`:

```js
grunt.loadNpmTasks('grunt-phpmd');
```

## PHPMD task

_Run this task with the `grunt phpmd` command._

_This task is a [multi task][] so any targets, files and options should be specified according to the [multi task][] documentation._

[multi task]: https://github.com/gruntjs/grunt/wiki/Configuring-tasks

### Usage Example

```js
phpmd: {
application: {
dir: 'application'
}
options: {
rulesets: 'codesize'
}
}
```

### Target Properties

#### dir
Type: `String`

The file or directory where phpmd should search for files.

### Options
#### bin
Type: `String`
Default: `'phpmd'`

The binary name if it is in your PATH or the full path if not.

#### reportFormat
Type: `String`
Default: `'xml'`

The type of report to generate. PHPMD supports `xml`, `text` and `html`.

#### reportFile
Type: `String`
Default: `false`

Set a path and filename here to write to a file, otherwise it will write to stdout.

#### suffixes
Type: `String`
Default: `false`

Set a comma-separated string of valid source code filename extensions.

#### exclude
Type: `String`
Default: `false`

Set a comma-separated string of patterns that are used to ignore directories.

#### minimumPriority
Type: `Number`
Default: `false`

Rule priority threshold: rules with lower priority than this will not be used.

#### strict
Type: `Boolean`
Default: `false`

Also report those nodes with a @SuppressWarnings annotation.

#### rulesets
Type: `String`
Default: `'codesize,unusedcode,naming'`

A ruleset filename or a comma-separated string of rulesetfilenames. Make sure to have no spaces between the items!

#### maxBuffer
Type: `Number`
Default: `200*1024`

Override the maxBuffer-Size of nodejs's exec() function if you expect a long output on stdout.
25 changes: 25 additions & 0 deletions package.json
@@ -0,0 +1,25 @@
{
"name": "grunt-phpmd",
"version": "0.1.0",
"description": "Grunt plugin for running PHP Mess Detector.",
"main": "tasks/phpmd.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/alappe/grunt-phpmd.git"
},
"keywords": [
"gruntplugin",
"phpmd",
"mess-detection",
"ci",
"continous-integration"
],
"author": "Andreas Lappe",
"license": "BSD",
"bugs": {
"url": "https://github.com/alappe/grunt-phpmd/issues"
}
}
55 changes: 55 additions & 0 deletions src/tasks/lib/phpmd.coffee
@@ -0,0 +1,55 @@
###
grunt-phpmd
Copyright (c) 2013 Andreas Lappe
http://kaeufli.ch
Licensed under the BSD license.
###

path = require 'path'
exec = (require 'child_process').exec

exports.init = (grunt) ->

exports = config = {}
cmd = done = null
defaults =
bin: 'phpmd'
# Can be xml, text or html
reportFormat: 'xml'
# Path and filename, otherwise STDOUT is used
reportFile: false
suffixes: false
exclude: false
minimumPriority: false
strict: false
rulesets: 'codesize,unusedcode,naming'
maxBuffer: 200*1024

buildCommand = (dir) ->
cmd = "#{path.normalize config.bin} #{dir} #{config.reportFormat} #{config.rulesets}"
cmd += " --minimumpriority #{config.minimumPriority}" if config.minimumPriority
cmd += " --reportfile #{config.reportFile}" if config.reportFile
cmd += " --suffixes #{config.suffixes}" if config.suffixes
cmd += " --exclude #{config.exclude}" if config.exclude
cmd += " --strict" if config.strict
cmd

exports.setup = (runner) ->
dir = path.normalize runner.data.dir
config = runner.options defaults
cmd = buildCommand dir
grunt.log.writeln "Starting phpmd (target: #{runner.target.cyan}) in #{dir.cyan}"
grunt.verbose.writeln "Execute: #{cmd}"
done = runner.async()

exports.run = ->
cmdOptions = maxBuffer: config.maxBuffer
exec cmd, cmdOptions, (err, stdout, stderr) ->
grunt.log.write stdout if stdout
if err
# As documented on # http://phpmd.org/documentation/index.html#exit-codes
grunt.fatal err if err.code isnt 2
done()

exports
14 changes: 14 additions & 0 deletions src/tasks/phpmd.coffee
@@ -0,0 +1,14 @@
###
grunt-phpmd
Copyright (c) 2013 Andreas Lappe
http://kaeufli.ch
Licensed under the BSD license.
###

module.exports = (grunt) ->
phpmd = (require './lib/phpmd').init grunt

grunt.registerMultiTask 'phpmd', 'Run phpmd', ->
phpmd.setup @
phpmd.run()
80 changes: 80 additions & 0 deletions tasks/lib/phpmd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions tasks/phpmd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 355ca42

Please sign in to comment.