|
1
|
|
|
<?php |
|
2
|
|
|
namespace Finder\Console\External\Explorer; |
|
3
|
|
|
|
|
4
|
|
|
use League\CLImate\CLImate; |
|
5
|
|
|
use Finder\Logic\Output\AbstractOutput; |
|
6
|
|
|
use Finder\Logic\Output\Filter\DiffOutputFilter; |
|
7
|
|
|
use ReflectionMethod; |
|
8
|
|
|
use SebastianBergmann\Diff\Parser; |
|
9
|
|
|
use SebastianBergmann\Git\Git; |
|
10
|
|
|
use UnexpectedValueException; |
|
11
|
|
|
use Finder\Spider\SpiderLinuxCommand; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Command line tool that run all script analyzers. |
|
15
|
|
|
*/ |
|
16
|
|
|
class DirectoryExplorer |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* CLI tool. |
|
20
|
|
|
* |
|
21
|
|
|
* @var CLImate CLImate instance. |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $cli; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Analyser. |
|
27
|
|
|
* |
|
28
|
|
|
* @var SpiderLinuxCommand analyser instance. |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $analyser; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Command line arguments. |
|
34
|
|
|
* |
|
35
|
|
|
* @var array list of arguments. |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $arguments; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Analysis targets paths. |
|
41
|
|
|
* |
|
42
|
|
|
* @var array list of files and directories paths. |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $analysedPaths; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Composer binaries directory path. |
|
48
|
|
|
* |
|
49
|
|
|
* @var string directory path. |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $binariesPath; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Set dependencies and initialize CLI. |
|
55
|
|
|
* |
|
56
|
|
|
* @param CLImate $climate CLImate instance. |
|
57
|
|
|
* @param string $binariesPath Composer binaries path. |
|
58
|
|
|
* @param array $arguments command line arguments. |
|
59
|
|
|
*/ |
|
60
|
|
View Code Duplication |
public function __construct(CLImate $climate, $binariesPath, array $arguments) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
$this->cli = $climate; |
|
63
|
|
|
$this->cli->description($this->getDescription()); |
|
64
|
|
|
$this->cli->arguments->add($this->getArguments()); |
|
65
|
|
|
$this->cli->arguments->parse($arguments); |
|
66
|
|
|
|
|
67
|
|
|
$this->arguments = $arguments; |
|
68
|
|
|
$this->binariesPath = $binariesPath; |
|
69
|
|
|
$this->setAnalysedPathsFromString($this->getArgumentValue('path')); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Run Code-Analyser command. |
|
74
|
|
|
* |
|
75
|
|
|
* @return boolean true if it didn't find code issues or ran successfully. |
|
76
|
|
|
*/ |
|
77
|
|
View Code Duplication |
public function run() |
|
|
|
|
|
|
78
|
|
|
{ |
|
79
|
|
|
if ($this->hasArgumentValue('help')) { |
|
80
|
|
|
$this->cli->usage(); |
|
81
|
|
|
return true; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if ($this->hasArgumentValue('version')) { |
|
85
|
|
|
$this->cli->out($this->getDescription()); |
|
86
|
|
|
return true; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if ($this->hasArgumentValue('git-diff')) { |
|
90
|
|
|
$gitDiff = $this->getArgumentValue('git-diff'); |
|
91
|
|
|
$filter = $this->getGitDiffFilter($gitDiff); |
|
92
|
|
|
$analysedFiles = $filter->getFilesWithAddedCode(); |
|
93
|
|
|
$this->setAnalysedPaths($analysedFiles); |
|
94
|
|
|
$this->getAnalyser()->setResultsFilter($filter); |
|
95
|
|
|
$this->getAnalyser()->setAnalysedPaths($analysedFiles); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $this->getAnalyser()->run(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Create a DiffOutputFilter based on a git-diff param. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $gitDiff git diff arguments. |
|
105
|
|
|
* @return DiffOutputFilter filter instance. |
|
106
|
|
|
*/ |
|
107
|
|
View Code Duplication |
protected function getGitDiffFilter($gitDiff) |
|
|
|
|
|
|
108
|
|
|
{ |
|
109
|
|
|
$analysedPaths = $this->getAnalysedPaths(); |
|
110
|
|
|
$gitPath = array_shift($analysedPaths); |
|
111
|
|
|
if (!is_dir($gitPath)) { |
|
112
|
|
|
$gitPath = dirname($gitPath); |
|
113
|
|
|
} |
|
114
|
|
|
$git = new Git($gitPath); |
|
115
|
|
|
$executeMethod = new ReflectionMethod($git, 'execute'); |
|
116
|
|
|
$executeMethod->setAccessible(true); |
|
117
|
|
|
$gitRoot = trim(implode("\n", $executeMethod->invoke($git, 'git rev-parse --show-toplevel'))); |
|
118
|
|
|
list($base, $changed) = explode('..', $gitDiff); |
|
119
|
|
|
$diff = $git->getDiff($base, $changed); |
|
120
|
|
|
$diffParser = new Parser; |
|
121
|
|
|
return new DiffOutputFilter($gitRoot, $diffParser->parse($diff)); |
|
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Initialize output. |
|
126
|
|
|
* |
|
127
|
|
|
* @throws UnexpectedValueException on invalid format value. |
|
128
|
|
|
* @return AbstractOutput |
|
129
|
|
|
*/ |
|
130
|
|
View Code Duplication |
protected function getOutput() |
|
|
|
|
|
|
131
|
|
|
{ |
|
132
|
|
|
$format = $this->getOutputFormat(); |
|
133
|
|
|
$formatClasses = $this->getOutputFormatClasses(); |
|
134
|
|
|
|
|
135
|
|
|
if (!isset($formatClasses[$format])) { |
|
136
|
|
|
throw new UnexpectedValueException( |
|
137
|
|
|
'Invalid format: "' . $format . '"' |
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$outputClassName = $formatClasses[$format]; |
|
142
|
|
|
|
|
143
|
|
|
return new $outputClassName( |
|
144
|
|
|
$this->cli, |
|
145
|
|
|
$this->getWorkingDirectory() |
|
146
|
|
|
); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Command line arguments list for CLImate. |
|
151
|
|
|
* |
|
152
|
|
|
* @return array CLI list of arguments. |
|
153
|
|
|
*/ |
|
154
|
|
View Code Duplication |
protected function getArguments() |
|
|
|
|
|
|
155
|
|
|
{ |
|
156
|
|
|
return [ |
|
157
|
|
|
'help' => [ |
|
158
|
|
|
'prefix' => 'h', |
|
159
|
|
|
'longPrefix' => 'help', |
|
160
|
|
|
'description' => 'Prints a usage statement', |
|
161
|
|
|
'noValue' => true, |
|
162
|
|
|
], |
|
163
|
|
|
'version' => [ |
|
164
|
|
|
'prefix' => 'v', |
|
165
|
|
|
'longPrefix' => 'version', |
|
166
|
|
|
'description' => 'Prints installed version', |
|
167
|
|
|
'noValue' => true, |
|
168
|
|
|
], |
|
169
|
|
|
'ignore' => [ |
|
170
|
|
|
'prefix' => 'i', |
|
171
|
|
|
'longPrefix' => 'ignore', |
|
172
|
|
|
'description' => 'Ignore a comma-separated list of directories', |
|
173
|
|
|
'castTo' => 'string', |
|
174
|
|
|
'defaultValue' => 'vendor,tests,features,spec', |
|
175
|
|
|
], |
|
176
|
|
|
'format' => [ |
|
177
|
|
|
'prefix' => 'f', |
|
178
|
|
|
'longPrefix' => 'format', |
|
179
|
|
|
'description' => 'Output format', |
|
180
|
|
|
'castTo' => 'string', |
|
181
|
|
|
'defaultValue' => 'text', |
|
182
|
|
|
], |
|
183
|
|
|
'git-diff' => [ |
|
184
|
|
|
'prefix' => 'g', |
|
185
|
|
|
'longPrefix' => 'git-diff', |
|
186
|
|
|
'description' => 'Limit to files and lines changed between two ' |
|
187
|
|
|
. 'commits or branches, e.g., "master..other".', |
|
188
|
|
|
'castTo' => 'string', |
|
189
|
|
|
], |
|
190
|
|
|
'path' => [ |
|
191
|
|
|
'description' => 'File or directory path to analyze', |
|
192
|
|
|
'defaultValue' => '.', |
|
193
|
|
|
], |
|
194
|
|
|
]; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Get a list of paths to be ignored by the analysis. |
|
199
|
|
|
* |
|
200
|
|
|
* @return string[] a list of file and/or directory paths. |
|
201
|
|
|
*/ |
|
202
|
|
|
public function getIgnoredPaths() |
|
203
|
|
|
{ |
|
204
|
|
|
$ignoredArgument = $this->getArgumentValue('ignore'); |
|
205
|
|
|
$ignoredPaths = explode(',', $ignoredArgument); |
|
206
|
|
|
return array_filter($ignoredPaths); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Parse a string of comma separated files and/or directories to be analysed. |
|
211
|
|
|
* |
|
212
|
|
|
* @param string $pathsString the path argument value. |
|
213
|
|
|
* @return void |
|
214
|
|
|
*/ |
|
215
|
|
|
protected function setAnalysedPathsFromString($pathsString) |
|
216
|
|
|
{ |
|
217
|
|
|
$rawAnalysedPaths = explode(',', $pathsString); |
|
218
|
|
|
$analysedPaths = array_filter($rawAnalysedPaths); |
|
|
|
|
|
|
219
|
|
|
$this->setAnalysedPaths(array_filter($rawAnalysedPaths)); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Set target files and/or directories to be analysed. Fix relative paths. |
|
224
|
|
|
* |
|
225
|
|
|
* @param string[] $paths target paths. |
|
226
|
|
|
* @return void |
|
227
|
|
|
*/ |
|
228
|
|
View Code Duplication |
protected function setAnalysedPaths(array $paths) |
|
|
|
|
|
|
229
|
|
|
{ |
|
230
|
|
|
foreach ($paths as &$path) { |
|
231
|
|
|
if (0 === strpos($path, DIRECTORY_SEPARATOR)) { |
|
232
|
|
|
continue; |
|
233
|
|
|
} |
|
234
|
|
|
$path = $this->getWorkingDirectory() . DIRECTORY_SEPARATOR . $path; |
|
235
|
|
|
} |
|
236
|
|
|
$this->analysedPaths = $paths; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Analysis target paths. |
|
241
|
|
|
* |
|
242
|
|
|
* @return string[] a list of analysed paths (usually just one). |
|
243
|
|
|
*/ |
|
244
|
|
|
public function getAnalysedPaths() |
|
245
|
|
|
{ |
|
246
|
|
|
return $this->analysedPaths; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Running script path. |
|
251
|
|
|
* |
|
252
|
|
|
* @return string current script directory. |
|
253
|
|
|
*/ |
|
254
|
|
|
public function getWorkingDirectory() |
|
255
|
|
|
{ |
|
256
|
|
|
return getcwd(); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Output format. |
|
261
|
|
|
* |
|
262
|
|
|
* @return string format type. |
|
263
|
|
|
*/ |
|
264
|
|
|
public function getOutputFormat() |
|
265
|
|
|
{ |
|
266
|
|
|
return $this->getArgumentValue('format'); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* CLI output description. |
|
271
|
|
|
* |
|
272
|
|
|
* @return string description. |
|
273
|
|
|
*/ |
|
274
|
|
|
public function getDescription() |
|
275
|
|
|
{ |
|
276
|
|
|
return 'BOSS SPIDER ' . SpiderLinuxCommand::VERSION; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* SpiderLinuxCommand instance. |
|
281
|
|
|
* |
|
282
|
|
|
* @return SpiderLinuxCommand instance. |
|
283
|
|
|
*/ |
|
284
|
|
|
public function getAnalyser() |
|
285
|
|
|
{ |
|
286
|
|
|
$paths = $this->getAnalysedPaths(); |
|
287
|
|
|
// $pipeline = \Finder\Pipelines\Finder\Directory::pipeline(); |
|
288
|
|
|
foreach ($paths as $path) { |
|
289
|
|
|
$spider = new \Finder\Spider\Directory($path); |
|
290
|
|
|
dd('GetAnaliser',$spider->run()); |
|
291
|
|
|
// $spider = new \Finder\Pipelines\Finder\Directory($path); |
|
292
|
|
|
// $pipeline->process( |
|
293
|
|
|
// \Fabrica\Entities\DirectoryEntity::make($path) |
|
294
|
|
|
// ); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
View Code Duplication |
if (null === $this->analyser) { |
|
|
|
|
|
|
298
|
|
|
$this->analyser = new SpiderLinuxCommand( |
|
299
|
|
|
$this->getOutput(), |
|
300
|
|
|
$this->binariesPath, |
|
301
|
|
|
$this->getAnalysedPaths(), |
|
302
|
|
|
$this->getIgnoredPaths() |
|
303
|
|
|
); |
|
304
|
|
|
} |
|
305
|
|
|
return $this->analyser; |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* List of output format classes. |
|
310
|
|
|
* |
|
311
|
|
|
* @return array array where the key is a format and its value the class. |
|
312
|
|
|
*/ |
|
313
|
|
View Code Duplication |
protected function getOutputFormatClasses() |
|
|
|
|
|
|
314
|
|
|
{ |
|
315
|
|
|
return [ |
|
316
|
|
|
'text' => 'Finder\Logic\Output\TextOutput', |
|
317
|
|
|
'json' => 'Finder\Logic\Output\JsonOutput', |
|
318
|
|
|
'xml' => 'Finder\Logic\Output\XmlOutput', |
|
319
|
|
|
'csv' => 'Finder\Logic\Output\CsvOutput', |
|
320
|
|
|
'html' => 'Finder\Logic\Output\HtmlOutput', |
|
321
|
|
|
]; |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* Get argument value from user informed arguments. |
|
326
|
|
|
* |
|
327
|
|
|
* @param string $name argument name. |
|
328
|
|
|
* @return Mixed argument value. |
|
329
|
|
|
*/ |
|
330
|
|
|
protected function getArgumentValue($name) |
|
331
|
|
|
{ |
|
332
|
|
|
return $this->cli->arguments->get($name); |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* Check if the user supplied an argument. |
|
337
|
|
|
* |
|
338
|
|
|
* @param string $name argument name. |
|
339
|
|
|
* @return boolean if the argument has informed or not. |
|
340
|
|
|
*/ |
|
341
|
|
|
protected function hasArgumentValue($name) |
|
342
|
|
|
{ |
|
343
|
|
|
return $this->cli->arguments->defined($name, $this->arguments); |
|
344
|
|
|
} |
|
345
|
|
|
} |
|
346
|
|
|
|
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.