Refactored plugin PhpCsFixer. Issue #63.
This commit is contained in:
parent
3ef02202d3
commit
9c1dcbf6a9
|
@ -8,23 +8,21 @@ Configuration
|
||||||
|
|
||||||
### Options
|
### Options
|
||||||
|
|
||||||
* **verbose** [bool, optional] - Whether to run in verbose mode (default: false)
|
* **directory** [string, optional] - The directory in which PHP CS Fixer should work (default: `%BUILD_PATH%`)
|
||||||
* **diff** [bool, optional] - Whether to run with the `--diff` flag enabled (default: false)
|
* **args** [string, optional] - Command line args (in string format) to pass to PHP Coding Standards Fixer (default: ``)
|
||||||
* **level** [string, optional] - `psr0`, `psr1`, `psr2`, or `symphony` (default: all)
|
|
||||||
* **workingdir** [string, optional] - The directory in which PHP CS Fixer should work (default: build root)
|
|
||||||
|
|
||||||
### Examples
|
### Examples
|
||||||
|
|
||||||
```yml
|
```yml
|
||||||
test:
|
test:
|
||||||
php_cs_fixer:
|
php_cs_fixer:
|
||||||
verbose: true
|
directory: "./my/dir/path" # == "%BUILD_PATH%/my/dir/path"
|
||||||
diff: true
|
args: "--rules=@PSR2 --diff --verbose"
|
||||||
level: "psr2"
|
|
||||||
workingdir: "my/dir/path"
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Warning
|
```yml
|
||||||
-------
|
test:
|
||||||
|
php_cs_fixer:
|
||||||
There is currently a bug with this plugin that will cause an error if you leave the level to default to `all`. That level does not exist and will cause the build to fail. Instead specify the level explicitly until this is fixed.
|
directory: "%BUILD_PATH%/my/dir/path"
|
||||||
|
args: "--rules=@PSR2"
|
||||||
|
```
|
||||||
|
|
|
@ -13,11 +13,8 @@ use PHPCensor\Plugin;
|
||||||
*/
|
*/
|
||||||
class PhpCsFixer extends Plugin
|
class PhpCsFixer extends Plugin
|
||||||
{
|
{
|
||||||
protected $workingDir = '';
|
protected $directory = null;
|
||||||
protected $level = ' --level=psr2';
|
protected $args = '';
|
||||||
protected $verbose = '';
|
|
||||||
protected $diff = '';
|
|
||||||
protected $levels = ['psr0', 'psr1', 'psr2', 'symfony'];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
|
@ -34,50 +31,31 @@ class PhpCsFixer extends Plugin
|
||||||
{
|
{
|
||||||
parent::__construct($builder, $build, $options);
|
parent::__construct($builder, $build, $options);
|
||||||
|
|
||||||
$this->workingDir = $this->builder->buildPath;
|
if (!empty($options['args'])) {
|
||||||
$this->buildArgs($options);
|
$this->args = $options['args'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($options['directory']) && $options['directory']) {
|
||||||
|
$this->directory = $builder->interpolate($options['directory']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run PHP CS Fixer.
|
* Run PHP CS Fixer.
|
||||||
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function execute()
|
public function execute()
|
||||||
{
|
{
|
||||||
$curdir = getcwd();
|
$cmd = '';
|
||||||
chdir($this->workingDir);
|
if (!empty($this->directory)) {
|
||||||
|
$cmd = 'cd ' . $this->directory . ' && ';
|
||||||
|
}
|
||||||
|
|
||||||
$phpcsfixer = $this->builder->findBinary('php-cs-fixer');
|
$phpCsFixer = $this->builder->findBinary('php-cs-fixer');
|
||||||
|
$cmd .= $phpCsFixer . ' fix . %s';
|
||||||
$cmd = $phpcsfixer . ' fix . %s %s %s';
|
$success = $this->builder->executeCommand($cmd, $this->args);
|
||||||
$success = $this->builder->executeCommand($cmd, $this->verbose, $this->diff, $this->level);
|
|
||||||
|
|
||||||
chdir($curdir);
|
|
||||||
|
|
||||||
return $success;
|
return $success;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Build an args string for PHPCS Fixer.
|
|
||||||
* @param $options
|
|
||||||
*/
|
|
||||||
public function buildArgs($options)
|
|
||||||
{
|
|
||||||
if (isset($options['verbose']) && $options['verbose']) {
|
|
||||||
$this->verbose = ' --verbose';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($options['diff']) && $options['diff']) {
|
|
||||||
$this->diff = ' --diff';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($options['level']) && in_array($options['level'], $this->levels)) {
|
|
||||||
$this->level = ' --level='.$options['level'];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($options['workingdir']) && $options['workingdir']) {
|
|
||||||
$this->workingDir = $this->builder->buildPath . $options['workingdir'];
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue