2013-05-22 22:43:19 +02:00
|
|
|
<?php
|
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
namespace PHPCensor\Plugin;
|
2013-05-22 22:43:19 +02:00
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
use PHPCensor\Builder;
|
|
|
|
use PHPCensor\Model\Build;
|
|
|
|
use PHPCensor\Plugin;
|
2013-10-10 02:01:06 +02:00
|
|
|
|
2013-05-22 22:43:19 +02:00
|
|
|
/**
|
2017-01-10 18:46:19 +01:00
|
|
|
* PHP CS Fixer - Works with the PHP Coding Standards Fixer for testing coding standards.
|
|
|
|
*
|
2017-03-04 16:39:56 +01:00
|
|
|
* @author Gabriel Baker <gabriel@autonomicpilot.co.uk>
|
2017-01-10 18:46:19 +01:00
|
|
|
*/
|
2016-07-11 18:00:04 +02:00
|
|
|
class PhpCsFixer extends Plugin
|
2013-05-22 22:43:19 +02:00
|
|
|
{
|
2017-04-19 14:07:53 +02:00
|
|
|
protected $directory = null;
|
|
|
|
protected $args = '';
|
2017-05-31 16:19:22 +02:00
|
|
|
|
|
|
|
protected $config = false;
|
|
|
|
protected $configs = [
|
|
|
|
'.php_cs',
|
|
|
|
'.php_cs.dist',
|
|
|
|
];
|
2013-05-22 22:53:56 +02:00
|
|
|
|
2017-01-11 16:15:54 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function pluginName()
|
|
|
|
{
|
|
|
|
return 'php_cs_fixer';
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
2016-07-11 18:00:04 +02:00
|
|
|
* {@inheritdoc}
|
2014-12-08 12:25:33 +01:00
|
|
|
*/
|
2016-07-22 09:05:34 +02:00
|
|
|
public function __construct(Builder $builder, Build $build, array $options = [])
|
2013-05-22 22:43:19 +02:00
|
|
|
{
|
2016-07-22 09:05:34 +02:00
|
|
|
parent::__construct($builder, $build, $options);
|
2014-05-02 15:48:40 +02:00
|
|
|
|
2017-04-14 18:04:37 +02:00
|
|
|
if (!empty($options['args'])) {
|
|
|
|
$this->args = $options['args'];
|
|
|
|
}
|
2017-05-31 16:19:22 +02:00
|
|
|
|
2017-04-19 14:07:53 +02:00
|
|
|
if (isset($options['verbose']) && $options['verbose']) {
|
|
|
|
$this->args .= ' --verbose';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['diff']) && $options['diff']) {
|
|
|
|
$this->args .= ' --diff';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['rules']) && $options['rules']) {
|
|
|
|
$this->args .= ' --rules=' . $options['rules'];
|
|
|
|
}
|
2017-04-14 18:04:37 +02:00
|
|
|
|
2017-05-31 16:19:22 +02:00
|
|
|
if (isset($options['config']) && $options['config']) {
|
|
|
|
$this->config = true;
|
|
|
|
$this->args .= ' --config=' . $builder->interpolate($options['config']);
|
|
|
|
}
|
|
|
|
|
2017-04-14 18:04:37 +02:00
|
|
|
if (isset($options['directory']) && $options['directory']) {
|
|
|
|
$this->directory = $builder->interpolate($options['directory']);
|
|
|
|
}
|
2013-05-22 22:43:19 +02:00
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Run PHP CS Fixer.
|
2017-04-14 18:04:37 +02:00
|
|
|
*
|
2017-05-31 16:19:22 +02:00
|
|
|
* @return boolean
|
2014-12-08 12:25:33 +01:00
|
|
|
*/
|
2013-05-22 22:43:19 +02:00
|
|
|
public function execute()
|
|
|
|
{
|
2017-05-31 16:19:22 +02:00
|
|
|
$directory = '';
|
2017-04-14 18:04:37 +02:00
|
|
|
if (!empty($this->directory)) {
|
2017-05-31 16:19:22 +02:00
|
|
|
$directory = $this->directory;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->config) {
|
|
|
|
foreach ($this->configs as $config) {
|
|
|
|
if (file_exists($this->builder->buildPath . '/' . $config)) {
|
|
|
|
$this->config = true;
|
|
|
|
$this->args .= ' --config=./' . $config;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->config && !$directory) {
|
|
|
|
$directory = '.';
|
2013-05-22 22:53:56 +02:00
|
|
|
}
|
|
|
|
|
2017-04-14 18:04:37 +02:00
|
|
|
$phpCsFixer = $this->builder->findBinary('php-cs-fixer');
|
2017-05-31 16:19:22 +02:00
|
|
|
$cmd = $phpCsFixer . ' fix ' . $directory . ' %s';
|
2017-04-14 18:04:37 +02:00
|
|
|
$success = $this->builder->executeCommand($cmd, $this->args);
|
2013-05-22 22:43:19 +02:00
|
|
|
|
2017-04-14 18:04:37 +02:00
|
|
|
return $success;
|
2013-05-22 22:43:19 +02:00
|
|
|
}
|
2013-10-10 02:01:06 +02:00
|
|
|
}
|