1
|
|
|
<?php |
2
|
|
|
namespace Finder\Logic; |
3
|
|
|
|
4
|
|
|
use Finder\Logic\Output\AbstractOutput; |
5
|
|
|
use Finder\Logic\Output\Filter\OutputFilterInterface; |
6
|
|
|
use Finder\Logic\Output\TriggerableInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Run all script analysers and outputs their result. |
10
|
|
|
* |
11
|
|
|
* @package qa |
12
|
|
|
*/ |
13
|
|
|
class Analyser |
14
|
|
|
{ |
15
|
|
|
const EVENT_STARTING_ANALYSIS = 0; |
16
|
|
|
const EVENT_STARTING_TOOL = 1; |
17
|
|
|
const EVENT_FINISHED_TOOL = 2; |
18
|
|
|
const EVENT_FINISHED_ANALYSIS = 3; |
19
|
|
|
|
20
|
|
|
const VERSION = '0.7.1'; |
21
|
|
|
|
22
|
|
|
protected $defaultLanguage = 'Php'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Composer binaries path. |
26
|
|
|
* |
27
|
|
|
* @var string directory path. |
28
|
|
|
*/ |
29
|
|
|
protected $binariesPath; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Analysis target. |
33
|
|
|
* |
34
|
|
|
* @var string[] file or directory path. |
35
|
|
|
*/ |
36
|
|
|
protected $analysedPaths; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Ignored paths. |
40
|
|
|
* |
41
|
|
|
* @var string[] comma separated list of directories to ignore. |
42
|
|
|
*/ |
43
|
|
|
protected $ignoredPaths; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Output service. |
47
|
|
|
* |
48
|
|
|
* @var AbstractOutput output instance. |
49
|
|
|
*/ |
50
|
|
|
protected $output; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Analysis result filter. |
54
|
|
|
* |
55
|
|
|
* @var OutputFilterInterface filter instance. |
56
|
|
|
*/ |
57
|
|
|
protected $resultsFilter; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set dependencies and initialize CLI. |
61
|
|
|
* |
62
|
|
|
* @param AbstractOutput $output Output target. |
63
|
|
|
* @param string $binariesPath Composer binaries path. |
64
|
|
|
* @param string[] $analysedPaths target file or directory path. |
65
|
|
|
* @param string[] $ignoredPaths comma separated list of ignored directories. |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
public function __construct(AbstractOutput $output, $binariesPath, $analysedPaths, $ignoredPaths, $project = false) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$this->output = $output; |
70
|
|
|
$this->binariesPath = $binariesPath; |
71
|
|
|
$this->analysedPaths = $analysedPaths; |
72
|
|
|
$this->ignoredPaths = $ignoredPaths; |
73
|
|
|
|
74
|
|
|
if (!$project) { |
75
|
|
|
$this->languageClass = 'Finder\\Logic\\Language\\'.$this->defaultLanguage; |
|
|
|
|
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Run each configured PHP analysis tool. |
81
|
|
|
* |
82
|
|
|
* @return boolean true if it didn't find code issues. |
83
|
|
|
*/ |
84
|
|
|
public function run() |
85
|
|
|
{ |
86
|
|
|
$result = $this->createResult(); |
87
|
|
|
$this->trigger( |
88
|
|
|
self::EVENT_STARTING_ANALYSIS, |
89
|
|
|
['ignoredPaths' => $this->ignoredPaths] |
|
|
|
|
90
|
|
|
); |
91
|
|
|
|
92
|
|
View Code Duplication |
foreach ($this->getAnalysisTools() as $tool) { |
|
|
|
|
93
|
|
|
$message = ['description' => $tool->getDescription()]; |
94
|
|
|
$this->trigger(self::EVENT_STARTING_TOOL, $message); |
|
|
|
|
95
|
|
|
$tool->run($this->getAnalysedPaths()); |
96
|
|
|
$result->mergeWith($tool->getAnalysisResult()); |
97
|
|
|
$this->trigger(self::EVENT_FINISHED_TOOL); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if ($this->resultsFilter) { |
101
|
|
|
$result->setResultsFilter($this->resultsFilter); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$this->output->result($result); |
105
|
|
|
$this->trigger(self::EVENT_FINISHED_ANALYSIS); |
106
|
|
|
|
107
|
|
|
return !$result->hasIssues(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Call an output trigger if supported. |
112
|
|
|
* |
113
|
|
|
* @param int $event occurred event. |
114
|
|
|
* @param string|null $message optional message. |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
protected function trigger($event, $message = null) |
118
|
|
|
{ |
119
|
|
|
if ($this->output instanceof TriggerableInterface) { |
120
|
|
|
$this->output->trigger($event, $message); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get a list of paths to be ignored by the analysis. |
126
|
|
|
* |
127
|
|
|
* @return string[] a list of file and/or directory paths. |
128
|
|
|
*/ |
129
|
|
|
public function getIgnoredPaths() |
130
|
|
|
{ |
131
|
|
|
return $this->ignoredPaths; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Analysis target path. |
136
|
|
|
* |
137
|
|
|
* @return string[] target path. |
138
|
|
|
*/ |
139
|
|
|
public function getAnalysedPaths() |
140
|
|
|
{ |
141
|
|
|
return $this->analysedPaths; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Add an output filter to delegate to the analysis result object. |
146
|
|
|
* |
147
|
|
|
* @param OutputFilterInterface $filter filter instance. |
148
|
|
|
*/ |
149
|
|
|
public function setResultsFilter(OutputFilterInterface $filter) |
150
|
|
|
{ |
151
|
|
|
$this->resultsFilter = $filter; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Set target files and/or directories to be analysed. |
156
|
|
|
* |
157
|
|
|
* @param string[] $paths target paths. |
158
|
|
|
* @return void |
159
|
|
|
*/ |
160
|
|
|
public function setAnalysedPaths(array $paths) |
161
|
|
|
{ |
162
|
|
|
$this->analysedPaths = $paths; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* List of PHP analys integration classes. |
167
|
|
|
* |
168
|
|
|
* @return string[] array of class names. |
169
|
|
|
*/ |
170
|
|
|
protected function getAnalysisToolsClasses() |
171
|
|
|
{ |
172
|
|
|
return $this->languageClass::getAnalysisToolsClasses(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Set of PHP analys integration objects. |
177
|
|
|
* |
178
|
|
|
* @return Finder\Logic\Tools\AbstractIntegrationTool[] set of objects. |
179
|
|
|
*/ |
180
|
|
|
protected function getAnalysisTools() |
181
|
|
|
{ |
182
|
|
|
$objects = []; |
183
|
|
|
$tools = $this->getAnalysisToolsClasses(); |
184
|
|
|
|
185
|
|
|
if (empty($tools)) { |
186
|
|
|
return $objects; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
foreach ($tools as $className) { |
190
|
|
|
$tool = new $className($this->binariesPath, sys_get_temp_dir()); |
191
|
|
|
$tool->setIgnoredPaths($this->getIgnoredPaths()); |
192
|
|
|
$objects[] = $tool; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $objects; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Create an empty analysis result. |
200
|
|
|
* |
201
|
|
|
* @return AnalysisResult instance. |
202
|
|
|
*/ |
203
|
|
|
protected function createResult() |
204
|
|
|
{ |
205
|
|
|
return new AnalysisResult; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
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.