2013-05-22 22:43:19 +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/
|
|
|
|
*/
|
2013-05-22 22:43:19 +02:00
|
|
|
|
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
|
|
|
/**
|
2015-09-08 01:03:43 +02:00
|
|
|
* PHP CS Fixer - Works with the PHP Coding Standards Fixer for testing coding standards.
|
2013-05-22 22:43:19 +02:00
|
|
|
* @author Gabriel Baker <gabriel@autonomicpilot.co.uk>
|
|
|
|
* @package PHPCI
|
|
|
|
* @subpackage Plugins
|
|
|
|
*/
|
2016-05-09 08:20:26 +02:00
|
|
|
class PhpCsFixer implements Plugin
|
2013-05-22 22:43:19 +02:00
|
|
|
{
|
2014-05-02 15:48:40 +02:00
|
|
|
/**
|
2016-07-21 19:20:59 +02:00
|
|
|
* @var \PHPCensor\Builder
|
2014-05-02 15:48:40 +02:00
|
|
|
*/
|
2013-05-22 22:43:19 +02:00
|
|
|
protected $phpci;
|
|
|
|
|
2014-05-02 15:48:40 +02:00
|
|
|
/**
|
2016-07-21 19:20:59 +02:00
|
|
|
* @var \PHPCensor\Model\Build
|
2014-05-02 15:48:40 +02:00
|
|
|
*/
|
|
|
|
protected $build;
|
|
|
|
|
2013-05-22 22:53:56 +02:00
|
|
|
protected $workingDir = '';
|
2015-10-23 05:18:22 +02:00
|
|
|
protected $level = ' --level=psr2';
|
|
|
|
protected $verbose = '';
|
|
|
|
protected $diff = '';
|
2016-04-21 06:58:09 +02:00
|
|
|
protected $levels = ['psr0', 'psr1', 'psr2', 'symfony'];
|
2013-05-22 22:53:56 +02:00
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Standard Constructor
|
|
|
|
*
|
|
|
|
* $options['directory'] Output Directory. Default: %BUILDPATH%
|
|
|
|
* $options['filename'] Phar Filename. Default: build.phar
|
|
|
|
* $options['regexp'] Regular Expression Filename Capture. Default: /\.php$/
|
|
|
|
* $options['stub'] Stub Content. No Default Value
|
|
|
|
*
|
|
|
|
* @param Builder $phpci
|
|
|
|
* @param Build $build
|
|
|
|
* @param array $options
|
|
|
|
*/
|
2016-04-21 06:58:09 +02:00
|
|
|
public function __construct(Builder $phpci, Build $build, array $options = [])
|
2013-05-22 22:43:19 +02:00
|
|
|
{
|
|
|
|
$this->phpci = $phpci;
|
2014-05-02 15:48:40 +02:00
|
|
|
$this->build = $build;
|
|
|
|
|
2013-05-22 22:43:19 +02:00
|
|
|
$this->workingdir = $this->phpci->buildPath;
|
|
|
|
$this->buildArgs($options);
|
2016-07-10 10:05:10 +02:00
|
|
|
|
|
|
|
$this->phpci->logDebug('Plugin options: ' . json_encode($options));
|
2013-05-22 22:43:19 +02:00
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Run PHP CS Fixer.
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-05-22 22:43:19 +02:00
|
|
|
public function execute()
|
|
|
|
{
|
2013-05-22 22:53:56 +02:00
|
|
|
$curdir = getcwd();
|
2013-05-22 22:43:19 +02:00
|
|
|
chdir($this->workingdir);
|
|
|
|
|
2013-10-08 09:50:10 +02:00
|
|
|
$phpcsfixer = $this->phpci->findBinary('php-cs-fixer');
|
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
$cmd = $phpcsfixer . ' fix . %s %s %s';
|
|
|
|
$success = $this->phpci->executeCommand($cmd, $this->verbose, $this->diff, $this->level);
|
2013-05-22 22:43:19 +02:00
|
|
|
|
|
|
|
chdir($curdir);
|
|
|
|
|
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Build an args string for PHPCS Fixer.
|
|
|
|
* @param $options
|
|
|
|
*/
|
2013-05-22 22:43:19 +02:00
|
|
|
public function buildArgs($options)
|
|
|
|
{
|
2013-10-10 02:01:06 +02:00
|
|
|
if (isset($options['verbose']) && $options['verbose']) {
|
|
|
|
$this->verbose = ' --verbose';
|
2013-05-22 22:53:56 +02:00
|
|
|
}
|
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
if (isset($options['diff']) && $options['diff']) {
|
|
|
|
$this->diff = ' --diff';
|
2013-05-22 22:53:56 +02:00
|
|
|
}
|
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
if (isset($options['level']) && in_array($options['level'], $this->levels)) {
|
|
|
|
$this->level = ' --level='.$options['level'];
|
2013-05-22 22:53:56 +02:00
|
|
|
}
|
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
if (isset($options['workingdir']) && $options['workingdir']) {
|
|
|
|
$this->workingdir = $this->phpci->buildPath . $options['workingdir'];
|
2013-05-22 22:53:56 +02:00
|
|
|
}
|
2013-05-22 22:43:19 +02:00
|
|
|
|
|
|
|
}
|
2013-10-10 02:01:06 +02:00
|
|
|
}
|