Refactored plugin PhpCsFixer. Issue #63.

This commit is contained in:
Dmitry Khomutov 2017-04-14 23:04:37 +07:00
parent 3ef02202d3
commit 9c1dcbf6a9
No known key found for this signature in database
GPG key ID: 7EB36C9576F9ECB9
2 changed files with 27 additions and 51 deletions

View file

@ -8,23 +8,21 @@ Configuration
### Options
* **verbose** [bool, optional] - Whether to run in verbose mode (default: false)
* **diff** [bool, optional] - Whether to run with the `--diff` flag enabled (default: false)
* **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)
* **directory** [string, optional] - The directory in which PHP CS Fixer should work (default: `%BUILD_PATH%`)
* **args** [string, optional] - Command line args (in string format) to pass to PHP Coding Standards Fixer (default: ``)
### Examples
```yml
test:
php_cs_fixer:
verbose: true
diff: true
level: "psr2"
workingdir: "my/dir/path"
directory: "./my/dir/path" # == "%BUILD_PATH%/my/dir/path"
args: "--rules=@PSR2 --diff --verbose"
```
Warning
-------
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.
```yml
test:
php_cs_fixer:
directory: "%BUILD_PATH%/my/dir/path"
args: "--rules=@PSR2"
```

View file

@ -13,11 +13,8 @@ use PHPCensor\Plugin;
*/
class PhpCsFixer extends Plugin
{
protected $workingDir = '';
protected $level = ' --level=psr2';
protected $verbose = '';
protected $diff = '';
protected $levels = ['psr0', 'psr1', 'psr2', 'symfony'];
protected $directory = null;
protected $args = '';
/**
* @return string
@ -34,50 +31,31 @@ class PhpCsFixer extends Plugin
{
parent::__construct($builder, $build, $options);
$this->workingDir = $this->builder->buildPath;
$this->buildArgs($options);
if (!empty($options['args'])) {
$this->args = $options['args'];
}
if (isset($options['directory']) && $options['directory']) {
$this->directory = $builder->interpolate($options['directory']);
}
}
/**
* Run PHP CS Fixer.
*
* @return bool
*/
public function execute()
{
$curdir = getcwd();
chdir($this->workingDir);
$cmd = '';
if (!empty($this->directory)) {
$cmd = 'cd ' . $this->directory . ' && ';
}
$phpcsfixer = $this->builder->findBinary('php-cs-fixer');
$cmd = $phpcsfixer . ' fix . %s %s %s';
$success = $this->builder->executeCommand($cmd, $this->verbose, $this->diff, $this->level);
chdir($curdir);
$phpCsFixer = $this->builder->findBinary('php-cs-fixer');
$cmd .= $phpCsFixer . ' fix . %s';
$success = $this->builder->executeCommand($cmd, $this->args);
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'];
}
}
}