|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of PHP Mess Detector. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) Manuel Pichler <[email protected]>. |
|
6
|
|
|
* All rights reserved. |
|
7
|
|
|
* |
|
8
|
|
|
* Licensed under BSD License |
|
9
|
|
|
* For full copyright and license information, please see the LICENSE file. |
|
10
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
11
|
|
|
* |
|
12
|
|
|
* @author Manuel Pichler <[email protected]> |
|
13
|
|
|
* @copyright Manuel Pichler. All rights reserved. |
|
14
|
|
|
* @license https://opensource.org/licenses/bsd-license.php BSD License |
|
15
|
|
|
* @link http://phpmd.org/ |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace PHPMD; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* This is the main facade of the PHP PMD application |
|
22
|
|
|
*/ |
|
23
|
|
|
class PHPMD |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* The current PHPMD version. |
|
27
|
|
|
*/ |
|
28
|
|
|
const VERSION = '@project.version@'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* List of valid file extensions for analyzed files. |
|
32
|
|
|
* |
|
33
|
|
|
* @var array(string) |
|
34
|
|
|
*/ |
|
35
|
|
|
private $fileExtensions = array('php', 'php3', 'php4', 'php5', 'inc'); |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* List of exclude directory patterns. |
|
39
|
|
|
* |
|
40
|
|
|
* @var array(string) |
|
41
|
|
|
*/ |
|
42
|
|
|
private $ignorePatterns = array('.git', '.svn', 'CVS', '.bzr', '.hg', 'SCCS'); |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The input source file or directory. |
|
46
|
|
|
* |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
private $input; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* This property will be set to <b>true</b> when an error or a violation |
|
53
|
|
|
* was found in the processed source code. |
|
54
|
|
|
* |
|
55
|
|
|
* @var boolean |
|
56
|
|
|
* @since 0.2.5 |
|
57
|
|
|
*/ |
|
58
|
|
|
private $violations = false; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Additional options for PHPMD or one of it's parser backends. |
|
62
|
|
|
* |
|
63
|
|
|
* @var array |
|
64
|
|
|
* @since 1.2.0 |
|
65
|
|
|
*/ |
|
66
|
|
|
private $options = array(); |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* This method will return <b>true</b> when the processed source code |
|
70
|
|
|
* contains violations. |
|
71
|
|
|
* |
|
72
|
|
|
* @return boolean |
|
73
|
|
|
* @since 0.2.5 |
|
74
|
|
|
*/ |
|
75
|
7 |
|
public function hasViolations() |
|
76
|
|
|
{ |
|
77
|
7 |
|
return $this->violations; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Returns the input source file or directory path. |
|
82
|
|
|
* |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
11 |
|
public function getInput() |
|
86
|
|
|
{ |
|
87
|
11 |
|
return $this->input; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Returns an array with valid php source file extensions. |
|
92
|
|
|
* |
|
93
|
|
|
* @return string[] |
|
94
|
|
|
* @since 0.2.0 |
|
95
|
|
|
*/ |
|
96
|
11 |
|
public function getFileExtensions() |
|
97
|
|
|
{ |
|
98
|
11 |
|
return $this->fileExtensions; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Sets a list of filename extensions for valid php source code files. |
|
103
|
|
|
* |
|
104
|
|
|
* @param array(string) $fileExtensions Extensions without leading dot. |
|
|
|
|
|
|
105
|
|
|
* @return void |
|
106
|
|
|
*/ |
|
107
|
|
|
public function setFileExtensions(array $fileExtensions) |
|
108
|
|
|
{ |
|
109
|
|
|
$this->fileExtensions = $fileExtensions; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Returns an array with string patterns that mark a file path as invalid. |
|
114
|
|
|
* |
|
115
|
|
|
* @return string[] |
|
116
|
|
|
* @since 0.2.0 |
|
117
|
|
|
*/ |
|
118
|
11 |
|
public function getIgnorePattern() |
|
119
|
|
|
{ |
|
120
|
11 |
|
return $this->ignorePatterns; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Sets a list of ignore patterns that is used to exclude directories from |
|
125
|
|
|
* the source analysis. |
|
126
|
|
|
* |
|
127
|
|
|
* @param array(string) $ignorePatterns List of ignore patterns. |
|
|
|
|
|
|
128
|
|
|
* @return void |
|
129
|
|
|
*/ |
|
130
|
|
|
public function setIgnorePattern(array $ignorePatterns) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->ignorePatterns = array_merge( |
|
133
|
|
|
$this->ignorePatterns, |
|
134
|
|
|
$ignorePatterns |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Returns additional options for PHPMD or one of it's parser backends. |
|
140
|
|
|
* |
|
141
|
|
|
* @return array |
|
142
|
|
|
*/ |
|
143
|
11 |
|
public function getOptions() |
|
144
|
|
|
{ |
|
145
|
11 |
|
return $this->options; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Sets additional options for PHPMD or one of it's parser backends. |
|
150
|
|
|
* |
|
151
|
|
|
* @param array $options Additional backend or PHPMD options. |
|
152
|
|
|
* @return void |
|
153
|
|
|
*/ |
|
154
|
4 |
|
public function setOptions(array $options) |
|
155
|
|
|
{ |
|
156
|
4 |
|
$this->options = $options; |
|
157
|
4 |
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* This method will process all files that can be found in the given input |
|
161
|
|
|
* path. It will apply rules defined in the comma-separated <b>$ruleSets</b> |
|
162
|
|
|
* argument. The result will be passed to all given renderer instances. |
|
163
|
|
|
* |
|
164
|
|
|
* @param string $inputPath |
|
165
|
|
|
* @param string $ruleSets |
|
166
|
|
|
* @param \PHPMD\AbstractRenderer[] $renderers |
|
167
|
|
|
* @param \PHPMD\RuleSetFactory $ruleSetFactory |
|
168
|
|
|
* @return void |
|
169
|
|
|
*/ |
|
170
|
11 |
|
public function processFiles( |
|
171
|
|
|
$inputPath, |
|
172
|
|
|
$ruleSets, |
|
173
|
|
|
array $renderers, |
|
174
|
|
|
RuleSetFactory $ruleSetFactory |
|
175
|
|
|
) { |
|
176
|
|
|
|
|
177
|
|
|
// Merge parsed excludes |
|
178
|
11 |
|
$this->ignorePatterns = array_merge($this->ignorePatterns, $ruleSetFactory->getIgnorePattern($ruleSets)); |
|
179
|
|
|
|
|
180
|
11 |
|
$this->input = $inputPath; |
|
181
|
|
|
|
|
182
|
11 |
|
$report = new Report(); |
|
183
|
|
|
|
|
184
|
11 |
|
$factory = new ParserFactory(); |
|
185
|
11 |
|
$parser = $factory->create($this); |
|
186
|
|
|
|
|
187
|
11 |
|
foreach ($ruleSetFactory->createRuleSets($ruleSets) as $ruleSet) { |
|
188
|
11 |
|
$parser->addRuleSet($ruleSet); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
11 |
|
$report->start(); |
|
192
|
11 |
|
$parser->parse($report); |
|
193
|
11 |
|
$report->end(); |
|
194
|
|
|
|
|
195
|
11 |
|
foreach ($renderers as $renderer) { |
|
196
|
11 |
|
$renderer->start(); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
11 |
|
foreach ($renderers as $renderer) { |
|
200
|
11 |
|
$renderer->renderReport($report); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
11 |
|
foreach ($renderers as $renderer) { |
|
204
|
11 |
|
$renderer->end(); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
11 |
|
$this->violations = !$report->isEmpty(); |
|
208
|
11 |
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.