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 class is a collection of concrete source analysis rules. |
22
|
|
|
*/ |
23
|
|
|
class RuleSet implements \IteratorAggregate |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Should this rule set force the strict mode. |
27
|
|
|
* |
28
|
|
|
* @var boolean |
29
|
|
|
* @since 1.2.0 |
30
|
|
|
*/ |
31
|
|
|
private $strict = false; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The name of the file where this set is specified. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $fileName = ''; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The name of this rule-set. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $name = ''; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* An optional description for this rule-set. |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $description = ''; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The violation report used by the rule-set. |
56
|
|
|
* |
57
|
|
|
* @var \PHPMD\Report |
58
|
|
|
*/ |
59
|
|
|
private $report; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Mapping between marker interfaces and concrete context code node classes. |
63
|
|
|
* |
64
|
|
|
* @var array(string=>string) |
65
|
|
|
*/ |
66
|
|
|
private $applyTo = array( |
67
|
|
|
'PHPMD\\Rule\\ClassAware' => 'PHPMD\\Node\\ClassNode', |
68
|
|
|
'PHPMD\\Rule\\FunctionAware' => 'PHPMD\\Node\\FunctionNode', |
69
|
|
|
'PHPMD\\Rule\\InterfaceAware' => 'PHPMD\\Node\\InterfaceNode', |
70
|
|
|
'PHPMD\\Rule\\MethodAware' => 'PHPMD\\Node\\MethodNode', |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Mapping of rules that apply to a concrete code node type. |
75
|
|
|
* |
76
|
|
|
* @var array(string=>array) |
77
|
|
|
*/ |
78
|
|
|
private $rules = array( |
79
|
|
|
'PHPMD\\Node\\ClassNode' => array(), |
80
|
|
|
'PHPMD\\Node\\FunctionNode' => array(), |
81
|
|
|
'PHPMD\\Node\\InterfaceNode' => array(), |
82
|
|
|
'PHPMD\\Node\\MethodNode' => array(), |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns the file name where the definition of this rule-set comes from. |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
6 |
|
public function getFileName() |
91
|
|
|
{ |
92
|
6 |
|
return $this->fileName; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Sets the file name where the definition of this rule-set comes from. |
97
|
|
|
* |
98
|
|
|
* @param string $fileName The file name. |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
6 |
|
public function setFileName($fileName) |
102
|
|
|
{ |
103
|
6 |
|
$this->fileName = $fileName; |
104
|
6 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns the name of this rule-set. |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
6 |
|
public function getName() |
112
|
|
|
{ |
113
|
6 |
|
return $this->name; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Sets the name of this rule-set. |
118
|
|
|
* |
119
|
|
|
* @param string $name The name of this rule-set. |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
6 |
|
public function setName($name) |
123
|
|
|
{ |
124
|
6 |
|
$this->name = $name; |
125
|
6 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Returns the description text for this rule-set instance. |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function getDescription() |
133
|
|
|
{ |
134
|
|
|
return $this->description; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Sets the description text for this rule-set instance. |
139
|
|
|
* |
140
|
|
|
* @param string $description The description text. |
141
|
|
|
* @return void |
142
|
|
|
*/ |
143
|
6 |
|
public function setDescription($description) |
144
|
|
|
{ |
145
|
6 |
|
$this->description = $description; |
146
|
6 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Activates the strict mode for this rule set instance. |
150
|
|
|
* |
151
|
|
|
* @return void |
152
|
|
|
* @since 1.2.0 |
153
|
|
|
*/ |
154
|
1 |
|
public function setStrict() |
155
|
|
|
{ |
156
|
1 |
|
$this->strict = true; |
157
|
1 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Returns the violation report used by the rule-set. |
161
|
|
|
* |
162
|
|
|
* @return \PHPMD\Report |
163
|
|
|
*/ |
164
|
|
|
public function getReport() |
165
|
|
|
{ |
166
|
|
|
return $this->report; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Sets the violation report used by the rule-set. |
171
|
|
|
* |
172
|
|
|
* @param \PHPMD\Report $report |
173
|
|
|
* @return void |
174
|
|
|
*/ |
175
|
8 |
|
public function setReport(Report $report) |
176
|
|
|
{ |
177
|
8 |
|
$this->report = $report; |
178
|
8 |
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* This method returns a rule by its name or <b>null</b> if it doesn't exist. |
182
|
|
|
* |
183
|
|
|
* @param string $name |
184
|
|
|
* @return \PHPMD\Rule |
185
|
|
|
*/ |
186
|
4 |
|
public function getRuleByName($name) |
187
|
|
|
{ |
188
|
4 |
|
foreach ($this->getRules() as $rule) { |
189
|
3 |
|
if ($rule->getName() === $name) { |
190
|
3 |
|
return $rule; |
191
|
|
|
} |
192
|
|
|
} |
193
|
1 |
|
return null; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* This method returns an iterator will all rules that belong to this |
198
|
|
|
* rule-set. |
199
|
|
|
* |
200
|
|
|
* @return \Iterator |
201
|
|
|
*/ |
202
|
4 |
|
public function getRules() |
203
|
|
|
{ |
204
|
4 |
|
$result = array(); |
205
|
4 |
|
foreach ($this->rules as $rules) { |
206
|
4 |
|
foreach ($rules as $rule) { |
207
|
3 |
|
if (in_array($rule, $result, true) === false) { |
208
|
4 |
|
$result[] = $rule; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
4 |
|
return new \ArrayIterator($result); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Adds a new rule to this rule-set. |
218
|
|
|
* |
219
|
|
|
* @param \PHPMD\Rule $rule |
220
|
|
|
* @return void |
221
|
|
|
*/ |
222
|
9 |
|
public function addRule(Rule $rule) |
223
|
|
|
{ |
224
|
9 |
|
foreach ($this->applyTo as $applyTo => $type) { |
225
|
9 |
|
if ($rule instanceof $applyTo) { |
226
|
9 |
|
$this->rules[$type][] = $rule; |
227
|
|
|
} |
228
|
|
|
} |
229
|
9 |
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Applies all registered rules that match against the concrete node type. |
233
|
|
|
* |
234
|
|
|
* @param \PHPMD\AbstractNode $node |
235
|
|
|
* @return void |
236
|
|
|
*/ |
237
|
8 |
|
public function apply(AbstractNode $node) |
238
|
|
|
{ |
239
|
|
|
// Current node type |
240
|
8 |
|
$className = get_class($node); |
241
|
|
|
|
242
|
|
|
// Check for valid node type |
243
|
8 |
|
if (!isset($this->rules[$className])) { |
244
|
|
|
return; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
// Apply all rules to this node |
248
|
8 |
|
foreach ($this->rules[$className] as $rule) { |
249
|
|
|
/** @var $rule Rule */ |
250
|
8 |
|
if ($node->hasSuppressWarningsAnnotationFor($rule) && !$this->strict) { |
251
|
2 |
|
continue; |
252
|
|
|
} |
253
|
7 |
|
$rule->setReport($this->report); |
254
|
7 |
|
$rule->apply($node); |
255
|
|
|
} |
256
|
8 |
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Returns an iterator with all rules that are part of this rule-set. |
260
|
|
|
* |
261
|
|
|
* @return \Iterator |
262
|
|
|
*/ |
263
|
|
|
public function getIterator() |
264
|
|
|
{ |
265
|
|
|
return $this->getRules(); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|