php-censor/src/PHPCensor/Plugin/PhpCsFixer.php

99 lines
2.4 KiB
PHP
Raw Normal View History

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
/**
* 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>
*/
2016-07-11 18:00:04 +02:00
class PhpCsFixer extends Plugin
2013-05-22 22:43:19 +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';
}
/**
2016-07-11 18:00:04 +02:00
* {@inheritdoc}
*/
public function __construct(Builder $builder, Build $build, array $options = [])
2013-05-22 22:43:19 +02:00
{
parent::__construct($builder, $build, $options);
2014-05-02 15:48:40 +02:00
if (!empty($options['args'])) {
$this->args = $options['args'];
}
2017-05-31 16:19:22 +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-05-31 16:19:22 +02:00
if (isset($options['config']) && $options['config']) {
$this->config = true;
$this->args .= ' --config=' . $builder->interpolate($options['config']);
}
if (isset($options['directory']) && $options['directory']) {
$this->directory = $builder->interpolate($options['directory']);
}
2013-05-22 22:43:19 +02:00
}
/**
* Run PHP CS Fixer.
*
2017-05-31 16:19:22 +02:00
* @return boolean
*/
2013-05-22 22:43:19 +02:00
public function execute()
{
2017-05-31 16:19:22 +02:00
$directory = '';
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
}
$phpCsFixer = $this->builder->findBinary('php-cs-fixer');
2017-05-31 16:19:22 +02:00
$cmd = $phpCsFixer . ' fix ' . $directory . ' %s';
$success = $this->builder->executeCommand($cmd, $this->args);
2013-05-22 22:43:19 +02:00
return $success;
2013-05-22 22:43:19 +02:00
}
2013-10-10 02:01:06 +02:00
}