Skip to content

Commit bcd188b

Browse files
committed
Fix support of PHP CS Fixer 3.0
1 parent 1a84b2e commit bcd188b

2 files changed

Lines changed: 37 additions & 24 deletions

File tree

docs/en/plugins/php_cs_fixer.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ Configuration
1010

1111
* **verbose** [bool, optional] - Whether to run in verbose mode (default: false).
1212
* **diff** [bool, optional] - Whether to run with the `--diff` flag enabled (default: false).
13-
* **rules** [string, optional] - Fixer rules (default: `@PSR2`).
13+
* **rules** [string, optional] - Fixer rules (default for 3.x: `@PSR12`; for 2.x: `@PSR2`). Cannot be used simultaneously with the `config` option.
1414
* **args** [string, optional] - Command line args (in string format) to pass to PHP
1515
Coding Standards Fixer (default: ``).
16-
* **config** [string, optional] - Special config file (default: `%BUILD_PATH%./.php_cs`
17-
or `%BUILD_PATH%./.php_cs.dist`).
16+
* **config** [string, optional] - Special config file (
17+
default for 3.x: `%BUILD_PATH%./.php-cs-fixer.php` or `%BUILD_PATH%./.php-cs-fixer.dist.php`;
18+
for 2.x: `%BUILD_PATH%./.php_cs` or `%BUILD_PATH%./.php_cs.dist`). Cannot be used simultaneously with the `rules` option.
1819
* **errors** [bool, optional] - Not fix files, but get the number of files with problem (default: false).
1920
* **report_errors** [bool, optional] - With **errors**, get the list of files in "Errors" tab (default: false).
2021

@@ -24,7 +25,7 @@ Configuration
2425
test:
2526
php_cs_fixer:
2627
directory: "./my/dir/path" # == "%BUILD_PATH%/my/dir/path"
27-
args: "--rules=@PSR2 --diff --verbose"
28+
args: "--rules=@PSR12 --diff --verbose"
2829
```
2930
3031
```yaml
@@ -33,7 +34,7 @@ test:
3334
directory: "%BUILD_PATH%/my/dir/path"
3435
verbose: true
3536
diff: true
36-
rules: "@PSR2"
37+
rules: "@PSR12"
3738
```
3839
3940
```yaml

src/Plugin/PhpCsFixer.php

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ class PhpCsFixer extends Plugin
2121
protected $args = '';
2222

2323
protected $config = false;
24-
protected $configs = [
25-
'.php_cs',
26-
'.php_cs.dist',
27-
];
24+
protected $configs = [];
2825

2926
protected $errors = false;
3027
protected $reportErrors = false;
@@ -39,6 +36,11 @@ class PhpCsFixer extends Plugin
3936
*/
4037
protected $supportsUdiff = false;
4138

39+
/**
40+
* @var string|null
41+
*/
42+
protected $version;
43+
4244
/**
4345
* @return string
4446
*/
@@ -93,17 +95,41 @@ public function __construct(Builder $builder, Build $build, array $options = [])
9395
* Run PHP CS Fixer.
9496
*
9597
* @return bool
98+
*
99+
* @throws Exception
96100
*/
97101
public function execute()
98102
{
103+
$phpCsFixer = $this->executable;
104+
105+
// Determine the version of PHP CS Fixer
106+
$cmd = $phpCsFixer . ' --version';
107+
$success = $this->builder->executeCommand($cmd);
108+
$output = $this->builder->getLastOutput();
109+
$matches = [];
110+
if (!\preg_match('/(\d+\.\d+\.\d+)/', $output, $matches)) {
111+
throw new Exception('Unable to determine the version of the PHP Coding Standards Fixer.');
112+
}
113+
114+
$this->version = $matches[1];
115+
// Appeared in PHP CS Fixer 2.8.0 and used by default since 3.0.0
116+
// https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/2.19/CHANGELOG.md#changelog-for-v280
117+
$this->supportsUdiff = \version_compare($this->version, '2.8.0', '>=')
118+
&& \version_compare($this->version, '3.0.0', '<');
119+
99120
$directory = '';
100121
if (!empty($this->directory)) {
101122
$directory = $this->directory;
102123
}
103124

104125
if (!$this->config) {
126+
if (\version_compare($this->version, '3.0.0', '>=')) {
127+
$this->configs = ['.php-cs-fixer.php', '.php-cs-fixer.dist.php'];
128+
} else {
129+
$this->configs = ['.php_cs', '.php_cs.dist'];
130+
}
105131
foreach ($this->configs as $config) {
106-
if (file_exists($this->builder->buildPath . $config)) {
132+
if (\file_exists($this->builder->buildPath . $config)) {
107133
$this->config = true;
108134
$this->args .= ' --config=./' . $config;
109135
break;
@@ -115,20 +141,6 @@ public function execute()
115141
$directory = '.';
116142
}
117143

118-
$phpCsFixer = $this->executable;
119-
120-
// Determine the version of PHP CS Fixer
121-
$cmd = $phpCsFixer . ' --version';
122-
$success = $this->builder->executeCommand($cmd);
123-
$output = $this->builder->getLastOutput();
124-
$matches = [];
125-
if (preg_match('/(\d+\.\d+\.\d+)/', $output, $matches)) {
126-
$version = $matches[1];
127-
// Appeared in PHP CS Fixer 2.8.0
128-
// https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/2.12/CHANGELOG.md#changelog-for-v280
129-
$this->supportsUdiff = version_compare($version, '2.8.0', '>=');
130-
}
131-
132144
if ($this->errors) {
133145
$this->args .= ' --verbose --format json --diff';
134146
if ($this->supportsUdiff) {

0 commit comments

Comments
 (0)