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
|
|
|
use PHPMD\Node\AbstractTypeNode; |
21
|
|
|
use PHPMD\Node\FunctionNode; |
22
|
|
|
use PHPMD\Node\MethodNode; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* This class is used as container for a single rule violation related to a source |
26
|
|
|
* node. |
27
|
|
|
*/ |
28
|
|
|
class RuleViolation |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* The rule that causes this violation. |
32
|
|
|
* |
33
|
|
|
* @var \PHPMD\Rule |
34
|
|
|
*/ |
35
|
|
|
private $rule; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The context code node for this rule violation. |
39
|
|
|
* |
40
|
|
|
* @var \PHPMD\AbstractNode |
41
|
|
|
*/ |
42
|
|
|
private $node; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The description/message text that describes the violation. |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
private $description; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The raw metric value which caused this rule violation. |
53
|
|
|
* |
54
|
|
|
* @var mixed |
55
|
|
|
*/ |
56
|
|
|
private $metric; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Name of the owning/context class or interface of this violation. |
60
|
|
|
* |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
private $className = null; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* The name of a method or <b>null</b> when this violation has no method |
67
|
|
|
* context. |
68
|
|
|
* |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
private $methodName = null; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The name of a function or <b>null</b> when this violation has no function |
75
|
|
|
* context. |
76
|
|
|
* |
77
|
|
|
* @var string |
78
|
|
|
*/ |
79
|
|
|
private $functionName = null; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Constructs a new rule violation instance. |
83
|
|
|
* |
84
|
|
|
* @param \PHPMD\Rule $rule |
85
|
|
|
* @param \PHPMD\AbstractNode $node |
86
|
|
|
* @param string $violationMessage |
87
|
|
|
* @param mixed $metric |
88
|
|
|
*/ |
89
|
33 |
|
public function __construct(Rule $rule, AbstractNode $node, $violationMessage, $metric = null) |
90
|
|
|
{ |
91
|
33 |
|
$this->rule = $rule; |
92
|
33 |
|
$this->node = $node; |
93
|
33 |
|
$this->metric = $metric; |
94
|
33 |
|
$this->description = $violationMessage; |
95
|
|
|
|
96
|
33 |
|
if ($node instanceof AbstractTypeNode) { |
97
|
4 |
|
$this->className = $node->getName(); |
98
|
|
|
} elseif ($node instanceof MethodNode) { |
99
|
3 |
|
$this->className = $node->getParentName(); |
100
|
3 |
|
$this->methodName = $node->getName(); |
101
|
|
|
} elseif ($node instanceof FunctionNode) { |
102
|
4 |
|
$this->functionName = $node->getName(); |
103
|
|
|
} |
104
|
33 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns the rule that causes this violation. |
108
|
|
|
* |
109
|
|
|
* @return \PHPMD\Rule |
110
|
|
|
*/ |
111
|
|
|
public function getRule() |
112
|
|
|
{ |
113
|
|
|
return $this->rule; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns the description/message text that describes the violation. |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
6 |
|
public function getDescription() |
122
|
|
|
{ |
123
|
6 |
|
return $this->description; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns the raw metric value which caused this rule violation. |
128
|
|
|
* |
129
|
|
|
* @return mixed|null |
130
|
|
|
*/ |
131
|
|
|
public function getMetric() |
132
|
|
|
{ |
133
|
|
|
return $this->metric; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Returns the file name where this rule violation was detected. |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
6 |
|
public function getFileName() |
142
|
|
|
{ |
143
|
6 |
|
return $this->node->getFileName(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Returns the first line of the node that causes this rule violation. |
148
|
|
|
* |
149
|
|
|
* @return integer |
150
|
|
|
*/ |
151
|
6 |
|
public function getBeginLine() |
152
|
|
|
{ |
153
|
6 |
|
return $this->node->getBeginLine(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Returns the last line of the node that causes this rule violation. |
158
|
|
|
* |
159
|
|
|
* @return integer |
160
|
|
|
*/ |
161
|
|
|
public function getEndLine() |
162
|
|
|
{ |
163
|
|
|
return $this->node->getEndLine(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Returns the name of the package that contains this violation. |
168
|
|
|
* |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
public function getNamespaceName() |
172
|
|
|
{ |
173
|
|
|
return $this->node->getNamespaceName(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Returns the name of the parent class or interface or <b>null</b> when there |
178
|
|
|
* is no parent class. |
179
|
|
|
* |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
public function getClassName() |
183
|
|
|
{ |
184
|
|
|
return $this->className; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Returns the name of a method or <b>null</b> when this violation has no |
189
|
|
|
* method context. |
190
|
|
|
* |
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
|
|
public function getMethodName() |
194
|
|
|
{ |
195
|
|
|
return $this->methodName; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Returns the name of a function or <b>null</b> when this violation has no |
200
|
|
|
* function context. |
201
|
|
|
* |
202
|
|
|
* @return string |
203
|
|
|
*/ |
204
|
|
|
public function getFunctionName() |
205
|
|
|
{ |
206
|
|
|
return $this->functionName; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|