Passed
Push — master ( 902a34...c826a7 )
by Kyle
53s queued 11s
created

RuleViolation::__construct()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.2163

Importance

Changes 0
Metric Value
cc 6
nc 8
nop 4
dl 0
loc 29
ccs 9
cts 11
cp 0.8182
crap 6.2163
rs 8.8337
c 0
b 0
f 0
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 arguments for the description/message text or <b>null</b>
53
     * when the arguments are unknown.
54
     *
55
     * @var array|null
56
     */
57
    private $args = null;
58
59
    /**
60
     * The raw metric value which caused this rule violation.
61
     *
62
     * @var mixed
63
     */
64
    private $metric;
65
66
    /**
67
     * Name of the owning/context class or interface of this violation.
68
     *
69
     * @var string
70
     */
71
    private $className = null;
72
73
    /**
74
     * The name of a method or <b>null</b> when this violation has no method
75
     * context.
76
     *
77
     * @var string|null
78
     */
79
    private $methodName = null;
80
81
    /**
82
     * The name of a function or <b>null</b> when this violation has no function
83
     * context.
84
     *
85
     * @var string
86
     */
87
    private $functionName = null;
88
89 37
    /**
90
     * Constructs a new rule violation instance.
91 37
     *
92 37
     * @param \PHPMD\Rule $rule
93 37
     * @param \PHPMD\AbstractNode $node
94 37
     * @param string|array $violationMessage
95
     * @param mixed $metric
96 37
     */
97 4
    public function __construct(Rule $rule, AbstractNode $node, $violationMessage, $metric = null)
98 33
    {
99 3
        $this->rule = $rule;
100 3
        $this->node = $node;
101 30
        $this->metric = $metric;
102 4
103
        if (is_array($violationMessage) === true) {
104 37
            $search = array();
105
            $replace = array();
106
            foreach ($violationMessage['args'] as $index => $value) {
107
                $search[] = '{' . $index . '}';
108
                $replace[] = $value;
109
            }
110
111
            $this->args = $violationMessage['args'];
112
            $this->description = str_replace($search, $replace, $violationMessage['message']);
113
        } else {
114
            $this->description = $violationMessage;
115
        }
116
117
        if ($node instanceof AbstractTypeNode) {
118
            $this->className = $node->getName();
119
        } elseif ($node instanceof MethodNode) {
120
            $this->className = $node->getParentName();
121 6
            $this->methodName = $node->getName();
122
        } elseif ($node instanceof FunctionNode) {
123 6
            $this->functionName = $node->getName();
124
        }
125
    }
126
127
    /**
128
     * Returns the rule that causes this violation.
129
     *
130
     * @return \PHPMD\Rule
131
     */
132
    public function getRule()
133
    {
134
        return $this->rule;
135
    }
136
137
    /**
138
     * Returns the description/message text that describes the violation.
139
     *
140
     * @return string
141 6
     */
142
    public function getDescription()
143 6
    {
144
        return $this->description;
145
    }
146
147
    /**
148
     * Returns the arguments for the description/message text or <b>null</b>
149
     * when the arguments are unknown.
150
     *
151 6
     * @return array|null
152
     */
153 6
    public function getArgs()
154
    {
155
        return $this->args;
156
    }
157
158
    /**
159
     * Returns the raw metric value which caused this rule violation.
160
     *
161
     * @return mixed|null
162
     */
163
    public function getMetric()
164
    {
165
        return $this->metric;
166
    }
167
168
    /**
169
     * Returns the file name where this rule violation was detected.
170
     *
171
     * @return string|null
172
     */
173
    public function getFileName()
174
    {
175
        return $this->node->getFileName();
176
    }
177
178
    /**
179
     * Returns the first line of the node that causes this rule violation.
180
     *
181
     * @return integer
182
     */
183
    public function getBeginLine()
184
    {
185
        return $this->node->getBeginLine();
186
    }
187
188
    /**
189
     * Returns the last line of the node that causes this rule violation.
190
     *
191
     * @return integer
192
     */
193
    public function getEndLine()
194
    {
195
        return $this->node->getEndLine();
196
    }
197
198
    /**
199
     * Returns the name of the package that contains this violation.
200
     *
201
     * @return string
202
     */
203
    public function getNamespaceName()
204
    {
205
        return $this->node->getNamespaceName();
206
    }
207
208
    /**
209
     * Returns the name of the parent class or interface or <b>null</b> when there
210
     * is no parent class.
211
     *
212
     * @return string
213
     */
214
    public function getClassName()
215
    {
216
        return $this->className;
217
    }
218
219
    /**
220
     * Returns the name of a method or <b>null</b> when this violation has no
221
     * method context.
222
     *
223
     * @return string|null
224
     */
225
    public function getMethodName()
226
    {
227
        return $this->methodName;
228
    }
229
230
    /**
231
     * Returns the name of a function or <b>null</b> when this violation has no
232
     * function context.
233
     *
234
     * @return string
235
     */
236
    public function getFunctionName()
237
    {
238
        return $this->functionName;
239
    }
240
}
241