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