|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fabrica\Tools\Plugin; |
|
4
|
|
|
|
|
5
|
|
|
use Fabrica\Tools; |
|
6
|
|
|
use Fabrica\Tools\Builder; |
|
7
|
|
|
use Fabrica\Models\Infra\Ci\Build; |
|
8
|
|
|
use Fabrica\Tools\Plugin; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* PHPTAL Lint Plugin - Provides access to PHPTAL lint functionality. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Stephen Ball <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class PhpTalLint extends Plugin |
|
16
|
|
|
{ |
|
17
|
|
|
protected $recursive = true; |
|
18
|
|
|
protected $suffixes; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @return string |
|
22
|
|
|
*/ |
|
23
|
|
|
public static function pluginName() |
|
24
|
|
|
{ |
|
25
|
|
|
return 'php_tal_lint'; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string The path to a file contain custom phptal_tales_ functions |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $tales; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var int |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $allowedWarnings; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var int |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $allowedErrors; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var array The results of the lint scan |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $failedPaths = []; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct(Builder $builder, Build $build, array $options = []) |
|
52
|
|
|
{ |
|
53
|
|
|
parent::__construct($builder, $build, $options); |
|
54
|
|
|
|
|
55
|
|
|
$this->suffixes = ['zpt']; |
|
56
|
|
|
|
|
57
|
|
|
$this->allowedWarnings = 0; |
|
58
|
|
|
$this->allowedErrors = 0; |
|
59
|
|
|
|
|
60
|
|
|
if (isset($options['suffixes'])) { |
|
61
|
|
|
$this->suffixes = (array)$options['suffixes']; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Executes phptal lint |
|
67
|
|
|
*/ |
|
68
|
|
|
public function execute() |
|
69
|
|
|
{ |
|
70
|
|
|
$this->lintDirectory($this->directory); |
|
71
|
|
|
|
|
72
|
|
|
$errors = 0; |
|
73
|
|
|
$warnings = 0; |
|
74
|
|
|
|
|
75
|
|
|
foreach ($this->failedPaths as $path) { |
|
76
|
|
|
if ($path['type'] == 'error') { |
|
77
|
|
|
$errors++; |
|
78
|
|
|
} else { |
|
79
|
|
|
$warnings++; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$this->build->storeMeta((self::pluginName() . '-warnings'), $warnings); |
|
84
|
|
|
$this->build->storeMeta((self::pluginName() . '-errors'), $errors); |
|
85
|
|
|
$this->build->storeMeta((self::pluginName() . '-data'), $this->failedPaths); |
|
86
|
|
|
|
|
87
|
|
|
$success = true; |
|
88
|
|
|
|
|
89
|
|
|
if ($this->allowedWarnings != -1 && $warnings > $this->allowedWarnings) { |
|
90
|
|
|
$success = false; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if ($this->allowedErrors != -1 && $errors > $this->allowedErrors) { |
|
94
|
|
|
$success = false; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $success; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Lint an item (file or directory) by calling the appropriate method. |
|
102
|
|
|
* |
|
103
|
|
|
* @param $item |
|
104
|
|
|
* @param $itemPath |
|
105
|
|
|
* @return bool |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function lintItem($item, $itemPath) |
|
108
|
|
|
{ |
|
109
|
|
|
$success = true; |
|
110
|
|
|
|
|
111
|
|
|
if ($item->isFile() && in_array(strtolower($item->getExtension()), $this->suffixes)) { |
|
112
|
|
|
if (!$this->lintFile($itemPath)) { |
|
113
|
|
|
$success = false; |
|
114
|
|
|
} |
|
115
|
|
View Code Duplication |
} elseif ($item->isDir() && $this->recursive && !$this->lintDirectory($itemPath . '/')) { |
|
|
|
|
|
|
116
|
|
|
$success = false; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $success; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Run phptal lint against a directory of files. |
|
124
|
|
|
* |
|
125
|
|
|
* @param $path |
|
126
|
|
|
* @return bool |
|
127
|
|
|
*/ |
|
128
|
|
View Code Duplication |
protected function lintDirectory($path) |
|
|
|
|
|
|
129
|
|
|
{ |
|
130
|
|
|
$success = true; |
|
131
|
|
|
$directory = new \DirectoryIterator($this->builder->buildPath . $path); |
|
132
|
|
|
|
|
133
|
|
|
foreach ($directory as $item) { |
|
134
|
|
|
if ($item->isDot()) { |
|
135
|
|
|
continue; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$itemPath = $path . $item->getFilename(); |
|
139
|
|
|
|
|
140
|
|
|
if (in_array($itemPath, $this->ignore)) { |
|
141
|
|
|
continue; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
if (!$this->lintItem($item, $itemPath)) { |
|
145
|
|
|
$success = false; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $success; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Run phptal lint against a specific file. |
|
154
|
|
|
* |
|
155
|
|
|
* @param $path |
|
156
|
|
|
* @return bool |
|
157
|
|
|
*/ |
|
158
|
|
|
protected function lintFile($path) |
|
159
|
|
|
{ |
|
160
|
|
|
$success = true; |
|
161
|
|
|
|
|
162
|
|
|
list($suffixes, $tales) = $this->getFlags(); |
|
163
|
|
|
|
|
164
|
|
|
$lint = __DIR__ . '/'; |
|
165
|
|
|
$lint .= 'vendor/phptal/phptal/'; |
|
166
|
|
|
$lint .= 'tools/phptal_lint.php'; |
|
167
|
|
|
$cmd = 'php ' . $lint . ' %s %s "%s"'; |
|
168
|
|
|
|
|
169
|
|
|
$this->builder->executeCommand($cmd, $suffixes, $tales, $this->builder->buildPath . $path); |
|
170
|
|
|
|
|
171
|
|
|
$output = $this->builder->getLastOutput(); |
|
172
|
|
|
|
|
173
|
|
|
if (preg_match('/Found (.+?) (error|warning)/i', $output, $matches)) { |
|
174
|
|
|
$rows = explode(PHP_EOL, $output); |
|
175
|
|
|
|
|
176
|
|
|
unset($rows[0]); |
|
177
|
|
|
unset($rows[1]); |
|
178
|
|
|
unset($rows[2]); |
|
179
|
|
|
unset($rows[3]); |
|
180
|
|
|
|
|
181
|
|
|
foreach ($rows as $row) { |
|
182
|
|
|
$name = basename($path); |
|
183
|
|
|
|
|
184
|
|
|
$row = str_replace('(use -i to include your custom modifier functions)', '', $row); |
|
185
|
|
|
$message = str_replace($name . ': ', '', $row); |
|
186
|
|
|
|
|
187
|
|
|
$parts = explode(' (line ', $message); |
|
188
|
|
|
|
|
189
|
|
|
$message = trim($parts[0]); |
|
190
|
|
|
$line = str_replace(')', '', $parts[1]); |
|
191
|
|
|
|
|
192
|
|
|
$this->failedPaths[] = [ |
|
193
|
|
|
'file' => $path, |
|
194
|
|
|
'line' => $line, |
|
195
|
|
|
'type' => $matches[2], |
|
196
|
|
|
'message' => $message |
|
197
|
|
|
]; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
$success = false; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
return $success; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Process options and produce an arguments string for PHPTAL Lint. |
|
208
|
|
|
* |
|
209
|
|
|
* @return array |
|
210
|
|
|
*/ |
|
211
|
|
|
protected function getFlags() |
|
212
|
|
|
{ |
|
213
|
|
|
$tales = ''; |
|
214
|
|
|
if (!empty($this->tales)) { |
|
215
|
|
|
$tales = ' -i ' . $this->builder->buildPath . $this->tales; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
$suffixes = ''; |
|
219
|
|
View Code Duplication |
if (is_array($this->suffixes) && count($this->suffixes) > 0) { |
|
|
|
|
|
|
220
|
|
|
$suffixes = ' -e ' . implode(',', $this->suffixes); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
return [$suffixes, $tales]; |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.