From 9c1dcbf6a9c13a447df45379d1f5a80f37c9dd76 Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Fri, 14 Apr 2017 23:04:37 +0700 Subject: [PATCH 1/2] Refactored plugin PhpCsFixer. Issue #63. --- docs/en/plugins/php_cs_fixes.md | 22 ++++++------ src/PHPCensor/Plugin/PhpCsFixer.php | 56 +++++++++-------------------- 2 files changed, 27 insertions(+), 51 deletions(-) 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']; - } - - } } From e2b6f5aefdb3ad8c1a830ab3367011060185b863 Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Wed, 19 Apr 2017 19:07:53 +0700 Subject: [PATCH 2/2] Refactored plugin PhpCsFixer (Added more backward compatibility). Issue #63. --- docs/en/plugins/php_cs_fixes.md | 7 ++++++- src/PHPCensor/Plugin/PhpCsFixer.php | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/docs/en/plugins/php_cs_fixes.md b/docs/en/plugins/php_cs_fixes.md index 17a3491d..babb8160 100644 --- a/docs/en/plugins/php_cs_fixes.md +++ b/docs/en/plugins/php_cs_fixes.md @@ -8,7 +8,10 @@ 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) * **directory** [string, optional] - The directory in which PHP CS Fixer should work (default: `%BUILD_PATH%`) +* **rules** [string, optional] - Fixer rules (default: `@PSR2`) * **args** [string, optional] - Command line args (in string format) to pass to PHP Coding Standards Fixer (default: ``) ### Examples @@ -24,5 +27,7 @@ test: test: php_cs_fixer: directory: "%BUILD_PATH%/my/dir/path" - args: "--rules=@PSR2" + verbose: true + diff: true + rules: "@PSR2" ``` diff --git a/src/PHPCensor/Plugin/PhpCsFixer.php b/src/PHPCensor/Plugin/PhpCsFixer.php index a001a898..2cbc7bf1 100644 --- a/src/PHPCensor/Plugin/PhpCsFixer.php +++ b/src/PHPCensor/Plugin/PhpCsFixer.php @@ -13,8 +13,8 @@ use PHPCensor\Plugin; */ class PhpCsFixer extends Plugin { - protected $directory = null; - protected $args = ''; + protected $directory = null; + protected $args = ''; /** * @return string @@ -34,6 +34,18 @@ class PhpCsFixer extends Plugin if (!empty($options['args'])) { $this->args = $options['args']; } + + if (isset($options['verbose']) && $options['verbose']) { + $this->args .= ' --verbose'; + } + + if (isset($options['diff']) && $options['diff']) { + $this->args .= ' --diff'; + } + + if (isset($options['rules']) && $options['rules']) { + $this->args .= ' --rules=' . $options['rules']; + } if (isset($options['directory']) && $options['directory']) { $this->directory = $builder->interpolate($options['directory']);