diff --git a/docs/en/plugins/php_cs_fixes.md b/docs/en/plugins/php_cs_fixes.md index c11f80b9..17a3491d 100644 --- a/docs/en/plugins/php_cs_fixes.md +++ b/docs/en/plugins/php_cs_fixes.md @@ -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" +``` diff --git a/src/PHPCensor/Plugin/PhpCsFixer.php b/src/PHPCensor/Plugin/PhpCsFixer.php index 76b65858..a001a898 100644 --- a/src/PHPCensor/Plugin/PhpCsFixer.php +++ b/src/PHPCensor/Plugin/PhpCsFixer.php @@ -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']; - } - - } }