php-censor/src/Plugin/PhpCodeSniffer.php

292 lines
7.4 KiB
PHP
Raw Normal View History

2013-05-03 17:02:53 +02:00
<?php
2016-07-19 20:28:11 +02:00
namespace PHPCensor\Plugin;
2013-05-03 17:02:53 +02:00
2016-07-19 20:28:11 +02:00
use PHPCensor;
use PHPCensor\Builder;
use PHPCensor\Model\Build;
use PHPCensor\Model\BuildError;
2016-07-11 18:00:04 +02:00
use PHPCensor\Plugin;
2017-01-13 16:35:41 +01:00
use PHPCensor\ZeroConfigPluginInterface;
2013-10-10 02:01:06 +02:00
/**
* PHP Code Sniffer Plugin - Allows PHP Code Sniffer testing.
2018-02-04 08:22:07 +01:00
*
2017-03-04 16:39:56 +01:00
* @author Dan Cryer <dan@block8.co.uk>
*/
2017-01-13 16:35:41 +01:00
class PhpCodeSniffer extends Plugin implements ZeroConfigPluginInterface
2013-05-03 17:02:53 +02:00
{
/**
* @var array
*/
protected $suffixes;
/**
* @var string
*/
protected $directory;
/**
* @var string
*/
protected $standard;
/**
* @var string
*/
2018-02-04 08:22:07 +01:00
protected $tabWidth;
/**
* @var string
*/
protected $encoding;
/**
* @var int
*/
2018-02-04 08:22:07 +01:00
protected $allowedErrors;
/**
* @var int
*/
2018-02-04 08:22:07 +01:00
protected $allowedWarnings;
/**
2017-03-15 17:27:24 +01:00
* @var string, based on the assumption the root may not hold the code to be tested, extends the base path
*/
protected $path;
/**
* @var array - paths to ignore
*/
protected $ignore;
2017-03-24 19:03:08 +01:00
/**
* @var int
*/
protected $severity = null;
/**
* @var null|int
*/
2018-02-04 08:22:07 +01:00
protected $errorSeverity = null;
2017-03-24 19:03:08 +01:00
/**
* @var null|int
*/
2018-02-04 08:22:07 +01:00
protected $warningSeverity = null;
2017-03-24 19:03:08 +01:00
2017-01-11 16:15:54 +01:00
/**
* @return string
*/
public static function pluginName()
{
return 'php_code_sniffer';
}
/**
2016-07-11 18:00:04 +02:00
* {@inheritdoc}
*/
public function __construct(Builder $builder, Build $build, array $options = [])
{
parent::__construct($builder, $build, $options);
2016-07-11 18:00:04 +02:00
$this->suffixes = ['php'];
$this->directory = $this->builder->buildPath;
2016-07-11 18:00:04 +02:00
$this->standard = 'PSR2';
$this->tabWidth = '';
2016-07-11 18:00:04 +02:00
$this->encoding = '';
$this->path = '';
$this->ignore = $this->builder->ignore;
$this->allowedWarnings = 0;
$this->allowedErrors = 0;
if (isset($options['zero_config']) && $options['zero_config']) {
2018-02-04 08:22:07 +01:00
$this->allowedWarnings = -1;
$this->allowedErrors = -1;
}
2013-10-10 02:01:06 +02:00
if (!empty($options['allowed_errors']) && is_int($options['allowed_errors'])) {
2018-02-04 08:22:07 +01:00
$this->allowedErrors = $options['allowed_errors'];
}
if (!empty($options['allowed_warnings']) && is_int($options['allowed_warnings'])) {
2018-02-04 08:22:07 +01:00
$this->allowedWarnings = $options['allowed_warnings'];
}
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'])) {
2018-02-04 08:22:07 +01:00
$this->tabWidth = ' --tab-width='.$options['tab_width'];
}
2013-10-10 02:01:06 +02:00
if (!empty($options['encoding'])) {
$this->encoding = ' --encoding=' . $options['encoding'];
}
if (!empty($options['ignore'])) {
$this->ignore = (array)$options['ignore'];
}
if (!empty($options['standard'])) {
2017-03-16 15:31:57 +01:00
$this->standard = $options['standard'];
}
2017-03-24 19:03:08 +01:00
if (!empty($options['path'])) {
$this->path = $options['path'];
}
2017-03-24 19:03:08 +01:00
if (isset($options['severity']) && is_int($options['severity'])) {
$this->severity = $options['severity'];
}
if (isset($options['error_severity']) && is_int($options['error_severity'])) {
2018-02-04 08:22:07 +01:00
$this->errorSeverity = $options['error_severity'];
2017-03-24 19:03:08 +01:00
}
if (isset($options['warning_severity']) && is_int($options['warning_severity'])) {
2018-02-04 08:22:07 +01:00
$this->warningSeverity = $options['warning_severity'];
2017-03-24 19:03:08 +01:00
}
}
/**
2016-07-11 18:00:04 +02:00
* Check if this plugin can be executed.
*
2016-07-11 18:00:04 +02:00
* @param $stage
* @param Builder $builder
* @param Build $build
*
2016-07-11 18:00:04 +02:00
* @return bool
*/
2016-07-11 18:00:04 +02:00
public static function canExecute($stage, Builder $builder, Build $build)
{
2017-03-16 15:54:25 +01:00
if ($stage == Build::STAGE_TEST) {
2016-07-11 18:00:04 +02:00
return true;
}
2016-07-11 18:00:04 +02:00
return false;
2013-10-10 02:01:06 +02:00
}
/**
* Runs PHP Code Sniffer in a specified directory, to a specified standard.
*/
public function execute()
{
2017-03-24 19:03:08 +01:00
list($ignore, $standard, $suffixes, $severity, $errorSeverity, $warningSeverity) = $this->getFlags();
2013-10-10 02:01:06 +02:00
$phpcs = $this->findBinary('phpcs');
2013-10-08 09:50:10 +02:00
$this->builder->logExecOutput(false);
2014-04-30 15:15:25 +02:00
2017-03-24 19:03:08 +01:00
$cmd = $phpcs . ' --report=json %s %s %s %s %s "%s" %s %s %s';
$this->builder->executeCommand(
2013-10-10 02:01:06 +02:00
$cmd,
$standard,
$suffixes,
$ignore,
2018-02-04 08:22:07 +01:00
$this->tabWidth,
2013-10-10 02:01:06 +02:00
$this->encoding,
2017-03-24 19:03:08 +01:00
$this->builder->buildPath . $this->path,
$severity,
$errorSeverity,
$warningSeverity
2013-10-10 02:01:06 +02:00
);
$output = $this->builder->getLastOutput();
list($errors, $warnings) = $this->processReport($output);
$this->builder->logExecOutput(true);
2014-04-30 15:15:25 +02:00
$success = true;
2014-04-30 15:14:44 +02:00
$this->build->storeMeta('phpcs-warnings', $warnings);
$this->build->storeMeta('phpcs-errors', $errors);
2018-02-04 08:22:07 +01:00
if ($this->allowedWarnings != -1 && $warnings > $this->allowedWarnings) {
2014-04-30 15:14:44 +02:00
$success = false;
}
2018-02-04 08:22:07 +01:00
if ($this->allowedErrors != -1 && $errors > $this->allowedErrors) {
2014-04-30 15:14:44 +02:00
$success = false;
}
return $success;
}
2013-10-10 02:01:06 +02: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) {
2017-03-15 17:27:24 +01:00
$standard = ' --standard=' . $this->directory.$this->standard;
2013-10-10 02:01:06 +02:00
} else {
2017-03-15 17:27:24 +01:00
$standard = ' --standard=' . $this->standard;
2013-10-10 02:01:06 +02:00
}
$suffixes = '';
if (count($this->suffixes)) {
$suffixes = ' --extensions=' . implode(',', $this->suffixes);
}
2017-03-24 19:03:08 +01:00
$severity = '';
if ($this->severity !== null) {
$severity = ' --severity=' . $this->severity;
}
$errorSeverity = '';
2018-02-04 08:22:07 +01:00
if ($this->errorSeverity !== null) {
$errorSeverity = ' --error-severity=' . $this->errorSeverity;
2017-03-24 19:03:08 +01:00
}
$warningSeverity = '';
2018-02-04 08:22:07 +01:00
if ($this->warningSeverity !== null) {
$warningSeverity = ' --warning-severity=' . $this->warningSeverity;
2017-03-24 19:03:08 +01:00
}
return [$ignore, $standard, $suffixes, $severity, $errorSeverity, $warningSeverity];
2013-10-10 02:01:06 +02:00
}
2014-04-30 15:14:44 +02:00
/**
* Process the PHPCS output report.
* @param $output
* @return array
* @throws \Exception
*/
protected function processReport($output)
2014-04-30 15:14:44 +02:00
{
$data = json_decode(trim($output), true);
2014-04-30 15:14:44 +02:00
if (!is_array($data)) {
$this->builder->log($output);
throw new \Exception('Could not process the report generated by PHP Code Sniffer.');
2014-04-30 15:14:44 +02:00
}
2017-03-15 17:27:24 +01:00
$errors = $data['totals']['errors'];
2014-04-30 15:14:44 +02:00
$warnings = $data['totals']['warnings'];
foreach ($data['files'] as $fileName => $file) {
$fileName = str_replace($this->builder->buildPath, '', $fileName);
2014-04-30 15:14:44 +02:00
foreach ($file['messages'] as $message) {
$this->build->reportError(
$this->builder,
'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
);
}
}
2016-04-21 06:58:09 +02:00
return [$errors, $warnings];
2014-04-30 15:14:44 +02:00
}
}