php-censor/PHPCI/Plugin/PhpMessDetector.php

134 lines
3.5 KiB
PHP
Raw Normal View History

2013-05-03 17:02:53 +02:00
<?php
2013-05-16 03:16:56 +02:00
/**
* 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/
*/
2013-05-03 17:02:53 +02:00
namespace PHPCI\Plugin;
2013-10-10 02:01:06 +02:00
use PHPCI\Builder;
use PHPCI\Model\Build;
/**
* PHP Mess Detector Plugin - Allows PHP Mess Detector testing.
* @author Dan Cryer <dan@block8.co.uk>
* @package PHPCI
* @subpackage Plugins
*/
2013-05-03 17:02:53 +02:00
class PhpMessDetector implements \PHPCI\Plugin
{
2013-07-19 15:40:16 +02:00
/**
* @var \PHPCI\Builder
*/
protected $phpci;
/**
* @var array
*/
protected $suffixes;
/**
* @var string, based on the assumption the root may not hold the code to be
* tested, exteds the base path only if the provided path is relative. Absolute
* paths are used verbatim
*/
protected $path;
/**
* @var array - paths to ignore
*/
protected $ignore;
/**
* Array of PHPMD rules. Can be one of the builtins (codesize, unusedcode, naming, design, controversial)
* or a filenname (detected by checking for a / in it), either absolute or relative to the project root.
2013-07-19 15:40:16 +02:00
* @var array
*/
protected $rules;
2013-05-03 17:02:53 +02:00
/**
* @param \PHPCI\Builder $phpci
* @param array $options
*/
2013-10-10 02:01:06 +02:00
public function __construct(Builder $phpci, Build $build, array $options = array())
{
$this->phpci = $phpci;
2013-10-10 02:01:06 +02:00
$this->build = $build;
$this->suffixes = array('php');
$this->ignore = $phpci->ignore;
$this->path = '';
$this->rules = array('codesize', 'unusedcode', 'naming');
if (!empty($options['path'])) {
$this->path = $options['path'];
}
2013-10-10 02:01:06 +02:00
foreach (array('rules', 'ignore', 'suffixes') as $key) {
$this->overrideSetting($options, $key);
}
}
2013-05-03 17:02:53 +02:00
/**
* Runs PHP Mess Detector in a specified directory.
*/
public function execute()
{
$ignore = '';
if (count($this->ignore)) {
$ignore = ' --exclude ' . implode(',', $this->ignore);
}
2013-05-03 17:02:53 +02:00
$suffixes = '';
if (count($this->suffixes)) {
$suffixes = ' --suffixes ' . implode(',', $this->suffixes);
}
if (!empty($this->rules) && !is_array($this->rules)) {
$this->phpci->logFailure('The "rules" option must be an array.');
return false;
}
2013-10-10 02:01:06 +02:00
foreach ($this->rules as &$rule) {
if (strpos($rule, '/') !== false) {
2013-10-10 02:01:06 +02:00
$rule = $this->phpci->buildPath . $rule;
}
}
2013-10-08 09:50:10 +02:00
$phpmd = $this->phpci->findBinary('phpmd');
if (!$phpmd) {
$this->phpci->logFailure('Could not find phpmd.');
return false;
}
$path = $this->phpci->buildPath . $this->path;
if (!empty($this->path) && $this->path{0} == '/') {
$path = $this->path;
}
2013-10-08 09:50:10 +02:00
$cmd = $phpmd . ' "%s" text %s %s %s';
2013-10-10 02:01:06 +02:00
$success = $this->phpci->executeCommand(
$cmd,
$path,
2013-10-10 02:01:06 +02:00
implode(',', $this->rules),
$ignore,
$suffixes
);
$errors = count(array_filter(explode(PHP_EOL, trim($this->phpci->getLastOutput()))));
2013-10-10 02:01:06 +02:00
$this->build->storeMeta('phpmd-warnings', $errors);
return $success;
}
2013-10-10 02:01:06 +02:00
protected function overrideSetting($options, $key)
{
if (isset($options[$key]) && is_array($options[$key])) {
2013-10-10 02:01:06 +02:00
$this->{$key} = $options[$key];
}
}
}