AbstractRule   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 357
Duplicated Lines 3.92 %

Coupling/Cohesion

Components 3
Dependencies 2

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
dl 14
loc 357
ccs 56
cts 70
cp 0.8
rs 10
c 0
b 0
f 0
wmc 27
lcom 3
cbo 2

24 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A setName() 0 4 1
A getSince() 0 4 1
A setSince() 0 4 1
A getMessage() 0 4 1
A setMessage() 0 4 1
A setExternalInfoUrl() 0 4 1
A getDescription() 0 4 1
A setDescription() 0 4 1
A getExamples() 0 4 1
A addExample() 0 4 1
A getPriority() 0 4 1
A setPriority() 0 4 1
A getRuleSetName() 0 4 1
A setRuleSetName() 0 4 1
A getReport() 0 4 1
A setReport() 0 4 1
A addProperty() 0 4 1
A getBooleanProperty() 0 7 2
A getIntProperty() 7 7 2
A getStringProperty() 7 7 2
A addViolation() 0 17 2
apply() 0 1 ?
A getExternalInfoUrl() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 abstract base class for pmd rules.
22
 *
23
 * @SuppressWarnings(PHPMD)
24
 */
25
abstract class AbstractRule implements Rule
26
{
27
    /**
28
     * The name for this rule instance.
29
     *
30
     * @var string $_name
31
     */
32
    private $name = '';
33
34
    /**
35
     * The violation message text for this rule.
36
     *
37
     * @var string
38
     */
39
    private $message = '';
40
41
    /**
42
     * The version since when this rule is available.
43
     *
44
     * @var string
45
     */
46
    private $since = null;
47
48
    /**
49
     * An url will external information for this rule.
50
     *
51
     * @var string
52
     */
53
    private $externalInfoUrl = '';
54
55
    /**
56
     * An optional description for this rule.
57
     *
58
     * @var string
59
     */
60
    private $description = '';
61
62
    /**
63
     * A list of code examples for this rule.
64
     *
65
     * @var array(string)
66
     */
67
    private $examples = array();
68
69
    /**
70
     * The name of the parent rule-set instance.
71
     *
72
     * @var string
73
     */
74
    private $ruleSetName = '';
75
76
    /**
77
     * The priority of this rule.
78
     *
79
     * @var integer
80
     */
81
    private $priority = self::LOWEST_PRIORITY;
82
83
    /**
84
     * Configuration properties for this rule instance.
85
     *
86
     * @var array(string=>string)
87
     */
88
    private $properties = array();
89
90
    /**
91
     * The report for object for this rule.
92
     *
93
     * @var \PHPMD\Report
94
     */
95
    private $report = null;
96
97
    /**
98
     * Returns the name for this rule instance.
99
     *
100
     * @return string
101
     */
102 1
    public function getName()
103
    {
104 1
        return $this->name;
105
    }
106
107
    /**
108
     * Sets the name for this rule instance.
109
     *
110
     * @param string $name The rule name.
111
     * @return void
112
     */
113 6
    public function setName($name)
114
    {
115 6
        $this->name = $name;
116 6
    }
117
118
    /**
119
     * Returns the version since when this rule is available or <b>null</b>.
120
     *
121
     * @return string
122
     */
123
    public function getSince()
124
    {
125
        return $this->since;
126
    }
127
128
    /**
129
     * Sets the version since when this rule is available.
130
     *
131
     * @param string $since The version number.
132
     * @return void
133
     */
134 6
    public function setSince($since)
135
    {
136 6
        $this->since = $since;
137 6
    }
138
139
    /**
140
     * Returns the violation message text for this rule.
141
     *
142
     * @return string
143
     */
144
    public function getMessage()
145
    {
146
        return $this->message;
147
    }
148
149
    /**
150
     * Sets the violation message text for this rule.
151
     *
152
     * @param string $message The violation message
153
     * @return void
154
     */
155 6
    public function setMessage($message)
156
    {
157 6
        $this->message = $message;
158 6
    }
159
160
    /**
161
     * Returns an url will external information for this rule.
162
     *
163
     * @return string
164
     */
165
    public function getExternalInfoUrl()
166
    {
167
        return $this->externalInfoUrl;
168
    }
169
170
    /**
171
     * Sets an url will external information for this rule.
172
     *
173
     * @param string $externalInfoUrl The info url.
174
     * @return void
175
     */
176 6
    public function setExternalInfoUrl($externalInfoUrl)
177
    {
178 6
        $this->externalInfoUrl = $externalInfoUrl;
179 6
    }
180
181
    /**
182
     * Returns the description text for this rule instance.
183
     *
184
     * @return string
185
     */
186
    public function getDescription()
187
    {
188
        return $this->description;
189
    }
190
191
    /**
192
     * Sets the description text for this rule instance.
193
     *
194
     * @param string $description The description text.
195
     * @return void
196
     */
197 6
    public function setDescription($description)
198
    {
199 6
        $this->description = $description;
200 6
    }
201
202
    /**
203
     * Returns a list of examples for this rule.
204
     *
205
     * @return string[]
206
     */
207
    public function getExamples()
208
    {
209
        return $this->examples;
210
    }
211
212
    /**
213
     * Adds a code example for this rule.
214
     *
215
     * @param string $example The code example.
216
     * @return void
217
     */
218 6
    public function addExample($example)
219
    {
220 6
        $this->examples[] = $example;
221 6
    }
222
223
    /**
224
     * Returns the priority of this rule.
225
     *
226
     * @return integer
227
     */
228 6
    public function getPriority()
229
    {
230 6
        return $this->priority;
231
    }
232
233
    /**
234
     * Set the priority of this rule.
235
     *
236
     * @param integer $priority The rule priority
237
     * @return void
238
     */
239 6
    public function setPriority($priority)
240
    {
241 6
        $this->priority = $priority;
242 6
    }
243
244
    /**
245
     * Returns the name of the parent rule-set instance.
246
     *
247
     * @return string
248
     */
249
    public function getRuleSetName()
250
    {
251
        return $this->ruleSetName;
252
    }
253
254
    /**
255
     * Sets the name of the parent rule set instance.
256
     *
257
     * @param string $ruleSetName The rule-set name.
258
     * @return void
259
     */
260 6
    public function setRuleSetName($ruleSetName)
261
    {
262 6
        $this->ruleSetName = $ruleSetName;
263 6
    }
264
265
    /**
266
     * Returns the violation report for this rule.
267
     *
268
     * @return \PHPMD\Report
269
     */
270
    public function getReport()
271
    {
272
        return $this->report;
273
    }
274
275
    /**
276
     * Sets the violation report for this rule.
277
     *
278
     * @param \PHPMD\Report $report
279
     * @return void
280
     */
281 50
    public function setReport(Report $report)
282
    {
283 50
        $this->report = $report;
284 50
    }
285
286
    /**
287
     * Adds a configuration property to this rule instance.
288
     *
289
     * @param string $name
290
     * @param string $value
291
     * @return void
292
     */
293 14
    public function addProperty($name, $value)
294
    {
295 14
        $this->properties[$name] = $value;
296 14
    }
297
298
    /**
299
     * Returns the value of a configured property as a boolean or throws an
300
     * exception when no property with <b>$name</b> exists.
301
     *
302
     * @param string $name
303
     * @return boolean
304
     * @throws \OutOfBoundsException When no property for <b>$name</b> exists.
305
     */
306 7
    public function getBooleanProperty($name)
307
    {
308 7
        if (isset($this->properties[$name])) {
309 6
            return in_array($this->properties[$name], array('true', 'on', 1));
310
        }
311 1
        throw new \OutOfBoundsException('Property "' . $name . '" does not exist.');
312
    }
313
314
    /**
315
     * Returns the value of a configured property as an integer or throws an
316
     * exception when no property with <b>$name</b> exists.
317
     *
318
     * @param string $name
319
     * @return integer
320
     * @throws \OutOfBoundsException When no property for <b>$name</b> exists.
321
     */
322 5 View Code Duplication
    public function getIntProperty($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
323
    {
324 5
        if (isset($this->properties[$name])) {
325 4
            return (int) $this->properties[$name];
326
        }
327 2
        throw new \OutOfBoundsException('Property "' . $name . '" does not exist.');
328
    }
329
330
    /**
331
     * Returns the raw string value of a configured property or throws an
332
     * exception when no property with <b>$name</b> exists.
333
     *
334
     * @param string $name
335
     * @return string
336
     * @throws \OutOfBoundsException When no property for <b>$name</b> exists.
337
     */
338 14 View Code Duplication
    public function getStringProperty($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
339
    {
340 14
        if (isset($this->properties[$name])) {
341 8
            return $this->properties[$name];
342
        }
343 8
        throw new \OutOfBoundsException('Property "' . $name . '" does not exist.');
344
    }
345
346
    /**
347
     * This method adds a violation to all reports for this violation type and
348
     * for the given <b>$node</b> instance.
349
     *
350
     * @param \PHPMD\AbstractNode $node
351
     * @param array $args
352
     * @param mixed $metric
353
     * @return void
354
     */
355 30
    protected function addViolation(
356
        AbstractNode $node,
357
        array $args = array(),
358
        $metric = null
359
    ) {
360 30
        $search  = array();
361 30
        $replace = array();
362 30
        foreach ($args as $index => $value) {
363 30
            $search[]  = '{' . $index . '}';
364 30
            $replace[] = $value;
365
        }
366
367 30
        $message = str_replace($search, $replace, $this->message);
368
369 30
        $ruleViolation = new RuleViolation($this, $node, $message, $metric);
370 30
        $this->report->addRuleViolation($ruleViolation);
371 30
    }
372
373
    /**
374
     * This method should implement the violation analysis algorithm of concrete
375
     * rule implementations. All extending classes must implement this method.
376
     *
377
     * @param \PHPMD\AbstractNode $node
378
     * @return void
379
     */
380
    abstract public function apply(AbstractNode $node);
381
}
382