phpci/PHPCI/Plugin/PhpCsFixer.php

107 lines
2.6 KiB
PHP
Raw Normal View History

2013-05-22 22:43:19 +02:00
<?php
/**
* PHPCI - Continuous Integration for PHP
*
* @copyright Copyright 2015, 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
namespace PHPCI\Plugin;
2013-10-10 02:01:06 +02:00
use PHPCI\Builder;
2014-12-04 16:48:52 +01:00
use PHPCI\Helper\Lang;
2013-10-10 02:01:06 +02:00
use PHPCI\Model\Build;
use PHPCI\PluginInterface;
2013-10-10 02:01:06 +02:00
2013-05-22 22:43:19 +02:00
/**
* PHP CS Fixer
*
* Works with the PHP CS Fixer for testing coding standards.
*
* @author Gabriel Baker <gabriel@autonomicpilot.co.uk>
* @package PHPCI
* @subpackage Plugins
*/
class PhpCsFixer implements PluginInterface
2013-05-22 22:43:19 +02:00
{
2014-05-02 15:48:40 +02:00
/**
* @var \PHPCI\Builder
*/
2013-05-22 22:43:19 +02:00
protected $phpci;
2014-05-02 15:48:40 +02:00
/**
* @var \PHPCI\Model\Build
*/
protected $build;
2013-05-22 22:53:56 +02:00
protected $workingDir = '';
2013-10-10 02:01:06 +02:00
protected $level = ' --level=all';
protected $verbose = '';
protected $diff = '';
2013-05-22 22:53:56 +02:00
protected $levels = array('psr0', 'psr1', 'psr2', 'all');
/**
* 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
*/
2013-10-10 02:01:06 +02:00
public function __construct(Builder $phpci, Build $build, array $options = array())
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);
}
/**
2015-04-28 09:12:55 +02:00
* {@inheritDocs}
*/
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;
}
/**
* Build an args string for PHPCS Fixer.
2015-04-28 09:12:55 +02:00
*
* @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
}