2013-05-03 17:02:53 +02:00
|
|
|
<?php
|
2013-05-16 03:16:56 +02:00
|
|
|
/**
|
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/
|
|
|
|
*/
|
2013-05-03 17:02:53 +02:00
|
|
|
|
|
|
|
namespace PHPCI\Plugin;
|
|
|
|
|
2014-04-25 11:17:39 +02:00
|
|
|
use PHPCI;
|
2013-10-10 02:01:06 +02:00
|
|
|
use PHPCI\Builder;
|
|
|
|
use PHPCI\Model\Build;
|
2015-10-15 11:07:54 +02:00
|
|
|
use PHPCI\Model\BuildError;
|
2013-10-10 02:01:06 +02:00
|
|
|
|
2013-05-16 03:30:48 +02:00
|
|
|
/**
|
|
|
|
* PHP Code Sniffer Plugin - Allows PHP Code Sniffer testing.
|
|
|
|
* @author Dan Cryer <dan@block8.co.uk>
|
|
|
|
* @package PHPCI
|
|
|
|
* @subpackage Plugins
|
|
|
|
*/
|
2014-04-25 11:17:39 +02:00
|
|
|
class PhpCodeSniffer implements PHPCI\Plugin, PHPCI\ZeroConfigPlugin
|
2013-05-03 17:02:53 +02:00
|
|
|
{
|
2013-08-15 16:23:53 +02:00
|
|
|
/**
|
|
|
|
* @var \PHPCI\Builder
|
|
|
|
*/
|
2013-05-16 16:50:19 +02:00
|
|
|
protected $phpci;
|
2013-05-03 17:02:53 +02:00
|
|
|
|
2013-08-15 16:23:53 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $suffixes;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $directory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $standard;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $tab_width;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $encoding;
|
|
|
|
|
2014-04-25 11:17:39 +02:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $allowed_errors;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $allowed_warnings;
|
|
|
|
|
2013-09-18 10:54:23 +02:00
|
|
|
/**
|
|
|
|
* @var string, based on the assumption the root may not hold the code to be
|
|
|
|
* tested, exteds the base path
|
|
|
|
*/
|
|
|
|
protected $path;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array - paths to ignore
|
|
|
|
*/
|
|
|
|
protected $ignore;
|
|
|
|
|
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-04-25 11:17:39 +02:00
|
|
|
public static function canExecute($stage, Builder $builder, Build $build)
|
|
|
|
{
|
|
|
|
if ($stage == 'test') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-15 16:23:53 +02:00
|
|
|
/**
|
|
|
|
* @param \PHPCI\Builder $phpci
|
2014-04-25 11:17:39 +02:00
|
|
|
* @param \PHPCI\Model\Build $build
|
2013-08-15 16:23:53 +02:00
|
|
|
* @param array $options
|
|
|
|
*/
|
2013-10-10 02:01:06 +02:00
|
|
|
public function __construct(Builder $phpci, Build $build, array $options = array())
|
2013-05-16 16:50:19 +02:00
|
|
|
{
|
2014-04-25 11:17:39 +02:00
|
|
|
$this->phpci = $phpci;
|
|
|
|
$this->build = $build;
|
|
|
|
$this->suffixes = array('php');
|
|
|
|
$this->directory = $phpci->buildPath;
|
|
|
|
$this->standard = 'PSR2';
|
|
|
|
$this->tab_width = '';
|
|
|
|
$this->encoding = '';
|
|
|
|
$this->path = '';
|
|
|
|
$this->ignore = $this->phpci->ignore;
|
2014-04-30 15:18:58 +02:00
|
|
|
$this->allowed_warnings = 0;
|
|
|
|
$this->allowed_errors = 0;
|
|
|
|
|
|
|
|
if (isset($options['zero_config']) && $options['zero_config']) {
|
|
|
|
$this->allowed_warnings = -1;
|
|
|
|
$this->allowed_errors = -1;
|
|
|
|
}
|
2013-10-10 02:01:06 +02:00
|
|
|
|
|
|
|
if (isset($options['suffixes'])) {
|
|
|
|
$this->suffixes = (array)$options['suffixes'];
|
|
|
|
}
|
2013-05-03 17:02:53 +02:00
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
if (!empty($options['tab_width'])) {
|
|
|
|
$this->tab_width = ' --tab-width='.$options['tab_width'];
|
2013-08-15 16:23:53 +02:00
|
|
|
}
|
2013-07-30 15:58:00 +02:00
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
if (!empty($options['encoding'])) {
|
|
|
|
$this->encoding = ' --encoding=' . $options['encoding'];
|
2013-07-30 15:58:00 +02:00
|
|
|
}
|
|
|
|
|
2014-05-12 18:07:20 +02:00
|
|
|
$this->setOptions($options);
|
|
|
|
}
|
2014-04-25 11:17:39 +02:00
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Handle this plugin's options.
|
|
|
|
* @param $options
|
|
|
|
*/
|
2014-05-12 18:07:20 +02:00
|
|
|
protected function setOptions($options)
|
|
|
|
{
|
|
|
|
foreach (array('directory', 'standard', 'path', 'ignore', 'allowed_warnings', 'allowed_errors') as $key) {
|
|
|
|
if (array_key_exists($key, $options)) {
|
|
|
|
$this->{$key} = $options[$key];
|
|
|
|
}
|
2014-04-25 11:17:39 +02:00
|
|
|
}
|
2013-10-10 02:01:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs PHP Code Sniffer in a specified directory, to a specified standard.
|
|
|
|
*/
|
|
|
|
public function execute()
|
|
|
|
{
|
|
|
|
list($ignore, $standard, $suffixes) = $this->getFlags();
|
|
|
|
|
2013-10-08 09:50:10 +02:00
|
|
|
$phpcs = $this->phpci->findBinary('phpcs');
|
|
|
|
|
2014-04-30 15:15:25 +02:00
|
|
|
$this->phpci->logExecOutput(false);
|
|
|
|
|
2014-04-30 15:14:44 +02:00
|
|
|
$cmd = $phpcs . ' --report=json %s %s %s %s %s "%s"';
|
2014-04-25 11:17:39 +02:00
|
|
|
$this->phpci->executeCommand(
|
2013-10-10 02:01:06 +02:00
|
|
|
$cmd,
|
|
|
|
$standard,
|
|
|
|
$suffixes,
|
|
|
|
$ignore,
|
|
|
|
$this->tab_width,
|
|
|
|
$this->encoding,
|
|
|
|
$this->phpci->buildPath . $this->path
|
|
|
|
);
|
2013-10-08 08:21:46 +02:00
|
|
|
|
|
|
|
$output = $this->phpci->getLastOutput();
|
2015-10-15 11:07:54 +02:00
|
|
|
list($errors, $warnings) = $this->processReport($output);
|
2013-10-08 08:21:46 +02:00
|
|
|
|
2014-04-30 15:15:25 +02:00
|
|
|
$this->phpci->logExecOutput(true);
|
|
|
|
|
2014-04-25 11:17:39 +02:00
|
|
|
$success = true;
|
2014-04-30 15:14:44 +02:00
|
|
|
$this->build->storeMeta('phpcs-warnings', $warnings);
|
|
|
|
$this->build->storeMeta('phpcs-errors', $errors);
|
2014-04-25 11:17:39 +02:00
|
|
|
|
2014-04-30 15:14:44 +02:00
|
|
|
if ($this->allowed_warnings != -1 && $warnings > $this->allowed_warnings) {
|
|
|
|
$success = false;
|
2013-10-08 08:21:46 +02:00
|
|
|
}
|
|
|
|
|
2014-04-30 15:14:44 +02:00
|
|
|
if ($this->allowed_errors != -1 && $errors > $this->allowed_errors) {
|
|
|
|
$success = false;
|
2013-10-08 08:21:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $success;
|
2013-05-16 16:50:19 +02:00
|
|
|
}
|
2013-10-10 02:01:06 +02:00
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Process options and produce an arguments string for PHPCS.
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-10-10 02:01:06 +02:00
|
|
|
protected function getFlags()
|
|
|
|
{
|
|
|
|
$ignore = '';
|
|
|
|
if (count($this->ignore)) {
|
|
|
|
$ignore = ' --ignore=' . implode(',', $this->ignore);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($this->standard, '/') !== false) {
|
|
|
|
$standard = ' --standard='.$this->directory.$this->standard;
|
|
|
|
} else {
|
|
|
|
$standard = ' --standard='.$this->standard;
|
|
|
|
}
|
|
|
|
|
|
|
|
$suffixes = '';
|
|
|
|
if (count($this->suffixes)) {
|
|
|
|
$suffixes = ' --extensions=' . implode(',', $this->suffixes);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array($ignore, $standard, $suffixes);
|
|
|
|
}
|
2014-04-30 15:14:44 +02:00
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Process the PHPCS output report.
|
|
|
|
* @param $output
|
|
|
|
* @return array
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2014-05-22 12:59:26 +02:00
|
|
|
protected function processReport($output)
|
2014-04-30 15:14:44 +02:00
|
|
|
{
|
2014-05-22 12:59:26 +02:00
|
|
|
$data = json_decode(trim($output), true);
|
|
|
|
|
2014-04-30 15:14:44 +02:00
|
|
|
if (!is_array($data)) {
|
2014-05-22 12:59:26 +02:00
|
|
|
$this->phpci->log($output);
|
2014-12-04 16:48:52 +01:00
|
|
|
throw new \Exception(PHPCI\Helper\Lang::get('could_not_process_report'));
|
2014-04-30 15:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$errors = $data['totals']['errors'];
|
|
|
|
$warnings = $data['totals']['warnings'];
|
|
|
|
|
|
|
|
foreach ($data['files'] as $fileName => $file) {
|
|
|
|
$fileName = str_replace($this->phpci->buildPath, '', $fileName);
|
|
|
|
|
|
|
|
foreach ($file['messages'] as $message) {
|
2015-10-15 11:07:54 +02:00
|
|
|
$this->build->reportError(
|
|
|
|
$this->phpci,
|
|
|
|
'php_code_sniffer',
|
|
|
|
'PHPCS: ' . $message['message'],
|
|
|
|
$message['type'] == 'ERROR' ? BuildError::SEVERITY_HIGH : BuildError::SEVERITY_LOW,
|
|
|
|
$fileName,
|
|
|
|
$message['line']
|
2014-04-30 15:14:44 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-15 11:07:54 +02:00
|
|
|
return array($errors, $warnings);
|
2014-04-30 15:14:44 +02:00
|
|
|
}
|
2013-05-16 16:50:19 +02:00
|
|
|
}
|