Adding PHP Docblock Checker plugin

This commit is contained in:
Dan Cryer 2014-05-08 16:02:24 +00:00
parent 74b390a8de
commit 9ecd3bfa11
4 changed files with 221 additions and 2 deletions

View file

@ -0,0 +1,138 @@
<?php
/**
* PHPCI - Continuous Integration for PHP
*
* @copyright Copyright 2013, Block 8 Limited.
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
* @link http://www.phptesting.org/
*/
namespace PHPCI\Plugin;
use PHPCI;
use PHPCI\Builder;
use PHPCI\Model\Build;
/**
* PHP Docblock Checker Plugin - Checks your PHP files for appropriate uses of Docblocks
* @author Dan Cryer <dan@block8.co.uk>
* @package PHPCI
* @subpackage Plugins
*/
class PhpDocblockChecker implements PHPCI\Plugin, PHPCI\ZeroConfigPlugin
{
/**
* @var \PHPCI\Builder
*/
protected $phpci;
/**
* @var \PHPCI\Model\Build
*/
protected $build;
/**
* @var string Based on the assumption the root may not hold the code to be
* tested, extends the build path.
*/
protected $path;
/**
* @var array - paths to ignore
*/
protected $ignore;
protected $skipClasses = false;
protected $skipMethods = false;
public static function canExecute($stage, Builder $builder, Build $build)
{
if ($stage == 'test') {
return true;
}
return false;
}
public function __construct(Builder $phpci, Build $build, array $options = array())
{
$this->phpci = $phpci;
$this->build = $build;
$this->ignore = $phpci->ignore;
$this->path = '';
$this->allowed_warnings = 0;
if (isset($options['zero_config']) && $options['zero_config']) {
$this->allowed_warnings = -1;
}
if (array_key_exists('skip_classes', $options)) {
$this->skipClasses = true;
}
if (array_key_exists('skip_methods', $options)) {
$this->skipMethods = true;
}
if (!empty($options['path'])) {
$this->path = $options['path'];
}
if (array_key_exists('allowed_warnings', $options)) {
$this->allowed_warnings = (int)$options['allowed_warnings'];
}
}
/**
* Runs PHP Mess Detector in a specified directory.
*/
public function execute()
{
$ignore = '';
if (count($this->ignore)) {
$ignore = ' --exclude="' . implode(',', $this->ignore) . '"';
}
var_dump($ignore);
$checker = $this->phpci->findBinary('phpdoccheck');
if (!$checker) {
$this->phpci->logFailure('Could not find phpdoccheck.');
return false;
}
$path = $this->phpci->buildPath . $this->path;
$cmd = $checker . ' --json --directory="%s"%s%s%s';
// Disable exec output logging, as we don't want the XML report in the log:
$this->phpci->logExecOutput(false);
// Run checker:
$this->phpci->executeCommand(
$cmd,
$path,
$ignore,
($this->skipClasses ? ' --skip-classes' : ''),
($this->skipMethods ? ' --skip-methods' : '')
);
// Re-enable exec output logging:
$this->phpci->logExecOutput(true);
$output = json_decode($this->phpci->getLastOutput());
$errors = count($output);
$success = true;
$this->build->storeMeta('phpdoccheck-warnings', $errors);
$this->build->storeMeta('phpdoccheck-data', $output);
if ($this->allowed_warnings != -1 && $errors > $this->allowed_warnings) {
$success = false;
}
return $success;
}
}

View file

@ -17,6 +17,8 @@ test:
standard: "PSR2"
php_loc:
php_unit:
php_docblock_checker:
allowed_warnings: -1 # Allow unlimited warnings for now.
failure:
email:

View file

@ -0,0 +1,78 @@
var phpdoccheckPlugin = PHPCI.UiPlugin.extend({
id: 'build-phpdoccheck-warnings',
css: 'col-lg-12 col-md-12 col-sm-12 col-xs-12',
title: 'PHP Docblock Checker',
lastData: null,
displayOnUpdate: false,
box: true,
register: function() {
var self = this;
var query = PHPCI.registerQuery('phpdoccheck-data', -1, {key: 'phpdoccheck-data'})
$(window).on('phpdoccheck-data', function(data) {
self.onUpdate(data);
});
$(window).on('build-updated', function(data) {
if (data.queryData.status > 1) {
self.displayOnUpdate = true;
query();
}
});
},
render: function() {
return $('<table class="table table-striped" id="phpdoccheck-data">' +
'<thead>' +
'<tr>' +
' <th>Type</th>' +
' <th>File</th>' +
' <th>Line</th>' +
' <th>Class</th>' +
' <th>Method</th>' +
'</tr>' +
'</thead><tbody></tbody></table>');
},
onUpdate: function(e) {
if (this.lastData && this.lastData[0]) {
return;
}
this.lastData = e.queryData;
var errors = this.lastData[0].meta_value;
var tbody = $('#phpdoccheck-data tbody');
tbody.empty();
for (var i in errors) {
var file = errors[i].file;
if (PHPCI.fileLinkTemplate) {
var fileLink = PHPCI.fileLinkTemplate.replace('{FILE}', file);
fileLink = fileLink.replace('{LINE}', errors[i].line);
file = '<a target="_blank" href="'+fileLink+'">' + file + '</a>';
}
var row = $('<tr>' +
'<td>'+errors[i].type+'</td>' +
'<td>'+file+'</td>' +
'<td>'+errors[i].line+'</td>' +
'<td>'+errors[i].class+'</td>' +
'<td>'+errors[i].method+'</td></tr>');
if (errors[i].type == 'method') {
row.addClass('danger');
} else {
row.addClass('warning');
}
tbody.append(row);
}
}
});
PHPCI.registerPlugin(new phpdoccheckPlugin());

View file

@ -7,7 +7,8 @@ var warningsPlugin = PHPCI.UiPlugin.extend({
'phpcs-warnings': 'PHPCS Warnings',
'phpcs-errors': 'PHPCS Errors',
'phplint-errors': 'PHPLint Errors',
'phpunit-errors': 'PHPUnit Errors'
'phpunit-errors': 'PHPUnit Errors',
'phpdoccheck-warnings': 'PHP Docblock Checker Warnings'
},
data: {},
displayOnUpdate: false,
@ -20,7 +21,7 @@ var warningsPlugin = PHPCI.UiPlugin.extend({
queries.push(PHPCI.registerQuery(key, -1, {num_builds: 10, key: key}));
}
$(window).on('phpmd-warnings phpcs-warnings phpcs-errors phplint-errors phpunit-errors', function(data) {
$(window).on('phpmd-warnings phpcs-warnings phpcs-errors phplint-errors phpunit-errors phpdoccheck-errors', function(data) {
self.onUpdate(data);
});