2014-05-08 18:02:24 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2014-05-12 18:26:17 +02:00
|
|
|
* PHPCI - Continuous Integration for PHP
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2014, Block 8 Limited.
|
|
|
|
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
|
|
|
* @link https://www.phptesting.org/
|
|
|
|
*/
|
2014-05-08 18:02:24 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Check if this plugin can be executed.
|
|
|
|
* @param $stage
|
|
|
|
* @param Builder $builder
|
|
|
|
* @param Build $build
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-05-08 18:02:24 +02:00
|
|
|
public static function canExecute($stage, Builder $builder, Build $build)
|
|
|
|
{
|
|
|
|
if ($stage == 'test') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Set up the plugin, configure options, etc.
|
|
|
|
* @param Builder $phpci
|
|
|
|
* @param Build $build
|
|
|
|
* @param array $options
|
|
|
|
*/
|
2014-05-08 18:02:24 +02:00
|
|
|
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()
|
|
|
|
{
|
2014-05-09 17:05:21 +02:00
|
|
|
// Check that the binary exists:
|
|
|
|
$checker = $this->phpci->findBinary('phpdoccheck');
|
|
|
|
|
|
|
|
// Build ignore string:
|
2014-05-08 18:02:24 +02:00
|
|
|
$ignore = '';
|
|
|
|
if (count($this->ignore)) {
|
|
|
|
$ignore = ' --exclude="' . implode(',', $this->ignore) . '"';
|
|
|
|
}
|
|
|
|
|
2014-05-09 17:05:21 +02:00
|
|
|
// Are we skipping any checks?
|
|
|
|
$add = '';
|
|
|
|
if ($this->skipClasses) {
|
|
|
|
$add .= ' --skip-classes';
|
|
|
|
}
|
2014-05-08 18:02:24 +02:00
|
|
|
|
2014-05-09 17:05:21 +02:00
|
|
|
if ($this->skipMethods) {
|
|
|
|
$add .= ' --skip-methods';
|
2014-05-08 18:02:24 +02:00
|
|
|
}
|
|
|
|
|
2014-05-09 17:05:21 +02:00
|
|
|
// Build command string:
|
2014-05-08 18:02:24 +02:00
|
|
|
$path = $this->phpci->buildPath . $this->path;
|
2014-05-09 17:05:21 +02:00
|
|
|
$cmd = $checker . ' --json --directory="%s"%s%s';
|
2014-05-08 18:02:24 +02:00
|
|
|
|
|
|
|
// 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,
|
2014-05-09 17:05:21 +02:00
|
|
|
$add
|
2014-05-08 18:02:24 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Re-enable exec output logging:
|
|
|
|
$this->phpci->logExecOutput(true);
|
|
|
|
|
2015-02-20 14:37:36 +01:00
|
|
|
$output = json_decode($this->phpci->getLastOutput(), true);
|
2014-05-08 18:02:24 +02:00
|
|
|
$errors = count($output);
|
|
|
|
$success = true;
|
|
|
|
|
|
|
|
$this->build->storeMeta('phpdoccheck-warnings', $errors);
|
2015-02-20 14:37:36 +01:00
|
|
|
$this->reportErrors($output);
|
2014-05-08 18:02:24 +02:00
|
|
|
|
|
|
|
if ($this->allowed_warnings != -1 && $errors > $this->allowed_warnings) {
|
|
|
|
$success = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $success;
|
|
|
|
}
|
2015-02-20 14:37:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Report all of the errors we've encountered line-by-line.
|
|
|
|
* @param $output
|
|
|
|
*/
|
|
|
|
protected function reportErrors($output)
|
|
|
|
{
|
|
|
|
foreach ($output as $error) {
|
2015-10-15 11:07:54 +02:00
|
|
|
$message = 'Class ' . $error['class'] . ' is missing a docblock.';
|
|
|
|
$severity = PHPCI\Model\BuildError::SEVERITY_LOW;
|
2015-02-20 14:37:36 +01:00
|
|
|
|
|
|
|
if ($error['type'] == 'method') {
|
2015-10-15 11:07:54 +02:00
|
|
|
$message = $error['class'] . '::' . $error['method'] . ' is missing a docblock.';
|
|
|
|
$severity = PHPCI\Model\BuildError::SEVERITY_NORMAL;
|
2015-02-20 14:37:36 +01:00
|
|
|
}
|
|
|
|
|
2015-10-15 11:07:54 +02:00
|
|
|
$this->build->reportError(
|
|
|
|
$this->phpci,
|
|
|
|
'php_docblock_checker',
|
|
|
|
$message,
|
|
|
|
$severity,
|
|
|
|
$error['file'],
|
|
|
|
$error['line']
|
|
|
|
);
|
2015-02-20 14:37:36 +01:00
|
|
|
}
|
|
|
|
}
|
2014-05-08 18:02:24 +02:00
|
|
|
}
|