|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fabrica\Tools\Plugin; |
|
4
|
|
|
|
|
5
|
|
|
use Fabrica\Tools\Builder; |
|
6
|
|
|
use Fabrica\Models\Infra\Ci\Build; |
|
7
|
|
|
use Fabrica\Models\Infra\Ci\BuildError; |
|
8
|
|
|
use Fabrica\Tools\Plugin; |
|
9
|
|
|
use SebastianBergmann\Diff\Line; |
|
10
|
|
|
use SebastianBergmann\Diff\Parser; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* PHP CS Fixer - Works with the PHP Coding Standards Fixer for testing coding standards. |
|
14
|
|
|
* |
|
15
|
|
|
* @author Gabriel Baker <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class PhpCsFixer extends Plugin |
|
18
|
|
|
{ |
|
19
|
|
|
protected $args = ''; |
|
20
|
|
|
|
|
21
|
|
|
protected $config = false; |
|
22
|
|
|
protected $configs = [ |
|
23
|
|
|
'.php_cs', |
|
24
|
|
|
'.php_cs.dist', |
|
25
|
|
|
]; |
|
26
|
|
|
|
|
27
|
|
|
protected $errors = false; |
|
28
|
|
|
protected $reportErrors = false; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var int |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $allowedWarnings; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var bool |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $supportsUdiff = false; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return string |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function pluginName() |
|
44
|
|
|
{ |
|
45
|
|
|
return 'php_cs_fixer'; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct(Builder $builder, Build $build, array $options = []) |
|
52
|
|
|
{ |
|
53
|
|
|
parent::__construct($builder, $build, $options); |
|
54
|
|
|
|
|
55
|
|
|
if (!empty($options['args'])) { |
|
56
|
|
|
$this->args = $options['args']; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$this->executable = $this->findBinary('php-cs-fixer'); |
|
60
|
|
|
|
|
61
|
|
|
if (isset($options['verbose']) && $options['verbose']) { |
|
62
|
|
|
$this->args .= ' --verbose'; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (isset($options['diff']) && $options['diff']) { |
|
66
|
|
|
$this->args .= ' --diff'; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (isset($options['rules']) && $options['rules']) { |
|
70
|
|
|
$this->args .= ' --rules=' . $options['rules']; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if (isset($options['config']) && $options['config']) { |
|
74
|
|
|
$this->config = true; |
|
75
|
|
|
$this->args .= ' --config=' . $builder->interpolate($options['config']); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if (isset($options['errors']) && $options['errors']) { |
|
79
|
|
|
$this->errors = true; |
|
80
|
|
|
$this->args .= ' --dry-run'; |
|
81
|
|
|
|
|
82
|
|
|
if (isset($options['report_errors']) && $options['report_errors']) { |
|
83
|
|
|
$this->reportErrors = true; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$this->allowedWarnings = isset($options['allowed_warnings']) ? (int) $options['allowed_warnings'] : 0; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Run PHP CS Fixer. |
|
92
|
|
|
* |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
|
|
public function execute() |
|
96
|
|
|
{ |
|
97
|
|
|
$directory = ''; |
|
98
|
|
|
if (!empty($this->directory)) { |
|
99
|
|
|
$directory = $this->directory; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if (!$this->config) { |
|
103
|
|
|
foreach ($this->configs as $config) { |
|
104
|
|
|
if (file_exists($this->builder->buildPath . $config)) { |
|
105
|
|
|
$this->config = true; |
|
106
|
|
|
$this->args .= ' --config=./' . $config; |
|
107
|
|
|
break; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if (!$this->config && !$directory) { |
|
113
|
|
|
$directory = '.'; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$phpCsFixer = $this->executable; |
|
117
|
|
|
|
|
118
|
|
|
// Determine the version of PHP CS Fixer |
|
119
|
|
|
$cmd = $phpCsFixer . ' --version'; |
|
120
|
|
|
$success = $this->builder->executeCommand($cmd); |
|
|
|
|
|
|
121
|
|
|
$output = $this->builder->getLastOutput(); |
|
122
|
|
|
$matches = []; |
|
123
|
|
|
if (preg_match('/(\d+\.\d+\.\d+)/', $output, $matches)) { |
|
124
|
|
|
$version = $matches[1]; |
|
125
|
|
|
// Appeared in PHP CS Fixer 2.8.0 |
|
126
|
|
|
// https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/2.12/CHANGELOG.md#changelog-for-v280 |
|
127
|
|
|
$this->supportsUdiff = version_compare($version, '2.8.0', '>='); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
if ($this->errors) { |
|
131
|
|
|
$this->args .= ' --verbose --format json --diff'; |
|
132
|
|
|
if ($this->supportsUdiff) { |
|
133
|
|
|
$this->args .= ' --diff-format udiff'; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$cmd = $phpCsFixer . ' fix ' . $directory . ' %s'; |
|
138
|
|
|
$success = $this->builder->executeCommand($cmd, $this->args); |
|
139
|
|
|
$output = $this->builder->getLastOutput(); |
|
140
|
|
|
|
|
141
|
|
|
if ($this->errors) { |
|
142
|
|
|
$warningCount = $this->processReport($output); |
|
143
|
|
|
|
|
144
|
|
|
$this->build->storeMeta((self::pluginName() . '-warnings'), $warningCount); |
|
145
|
|
|
|
|
146
|
|
|
if (-1 != $this->allowedWarnings && $warningCount > $this->allowedWarnings) { |
|
147
|
|
|
$success = false; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $success; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Process the PHP CS Fixer report. |
|
156
|
|
|
* |
|
157
|
|
|
* @param string $output |
|
158
|
|
|
* |
|
159
|
|
|
* @return int |
|
160
|
|
|
* |
|
161
|
|
|
* @throws \Exception |
|
162
|
|
|
*/ |
|
163
|
|
|
protected function processReport($output) |
|
164
|
|
|
{ |
|
165
|
|
|
$data = json_decode(trim($output), true); |
|
166
|
|
|
|
|
167
|
|
|
if (!is_array($data)) { |
|
168
|
|
|
$this->builder->log($output); |
|
169
|
|
|
throw new \Exception('Could not process the report generated by PHP CS Fixer.'); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
$warnings = 0; |
|
173
|
|
|
|
|
174
|
|
|
foreach ($data['files'] as $item) { |
|
175
|
|
|
$filename = $item['name']; |
|
176
|
|
|
$appliedFixers = isset($item['appliedFixers']) ? $item['appliedFixers'] : []; |
|
177
|
|
|
|
|
178
|
|
|
$parser = new Parser(); |
|
179
|
|
|
$parsed = $parser->parse($item['diff']); |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @var \SebastianBergmann\Diff\Diff $diffItem |
|
183
|
|
|
*/ |
|
184
|
|
|
$diffItem = $parsed[0]; |
|
185
|
|
|
foreach ($diffItem->getChunks() as $chunk) { |
|
186
|
|
|
$firstModifiedLine = $chunk->getStart(); |
|
187
|
|
|
$foundChanges = false; |
|
188
|
|
|
if (0 === $firstModifiedLine) { |
|
189
|
|
|
$firstModifiedLine = null; |
|
190
|
|
|
$foundChanges = true; |
|
191
|
|
|
} |
|
192
|
|
|
$chunkDiff = []; |
|
193
|
|
|
foreach ($chunk->getLines() as $line) { |
|
194
|
|
|
/** |
|
195
|
|
|
* @var Line $line |
|
196
|
|
|
*/ |
|
197
|
|
|
switch ($line->getType()) { |
|
198
|
|
|
case Line::ADDED: |
|
199
|
|
|
$symbol = '+'; |
|
200
|
|
|
break; |
|
201
|
|
|
case Line::REMOVED: |
|
202
|
|
|
$symbol = '-'; |
|
203
|
|
|
break; |
|
204
|
|
|
default: |
|
205
|
|
|
$symbol = ' '; |
|
206
|
|
|
break; |
|
207
|
|
|
} |
|
208
|
|
|
$chunkDiff[] = $symbol . $line->getContent(); |
|
209
|
|
|
if ($foundChanges) { |
|
210
|
|
|
continue; |
|
211
|
|
|
} |
|
212
|
|
|
if (Line::UNCHANGED === $line->getType()) { |
|
213
|
|
|
++$firstModifiedLine; |
|
214
|
|
|
continue; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
$foundChanges = true; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
$warnings++; |
|
221
|
|
|
|
|
222
|
|
View Code Duplication |
if ($this->reportErrors) { |
|
|
|
|
|
|
223
|
|
|
$this->build->reportError( |
|
224
|
|
|
$this->builder, |
|
225
|
|
|
self::pluginName(), |
|
226
|
|
|
"PHP CS Fixer suggestion:\r\n```diff\r\n" . implode("\r\n", $chunkDiff) . "\r\n```", |
|
227
|
|
|
BuildError::SEVERITY_LOW, |
|
228
|
|
|
$filename, |
|
229
|
|
|
$firstModifiedLine |
|
230
|
|
|
); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
View Code Duplication |
if ($this->reportErrors && !empty($appliedFixers)) { |
|
|
|
|
|
|
235
|
|
|
$this->build->reportError( |
|
236
|
|
|
$this->builder, |
|
237
|
|
|
self::pluginName(), |
|
238
|
|
|
'PHP CS Fixer failed fixers: ' . implode(', ', $appliedFixers), |
|
239
|
|
|
BuildError::SEVERITY_LOW, |
|
240
|
|
|
$filename |
|
241
|
|
|
); |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
return $warnings; |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.