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 |
||
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() |
|
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) |
|
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() |
||
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) |
|
138 | |||
139 | /** |
||
140 | * Returns the violation message text for this rule. |
||
141 | * |
||
142 | * @return string |
||
143 | */ |
||
144 | public function getMessage() |
||
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) |
|
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) |
|
180 | |||
181 | /** |
||
182 | * Returns the description text for this rule instance. |
||
183 | * |
||
184 | * @return string |
||
185 | */ |
||
186 | public function getDescription() |
||
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) |
|
201 | |||
202 | /** |
||
203 | * Returns a list of examples for this rule. |
||
204 | * |
||
205 | * @return string[] |
||
206 | */ |
||
207 | public function getExamples() |
||
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) |
|
222 | |||
223 | /** |
||
224 | * Returns the priority of this rule. |
||
225 | * |
||
226 | * @return integer |
||
227 | */ |
||
228 | 6 | public function getPriority() |
|
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) |
|
243 | |||
244 | /** |
||
245 | * Returns the name of the parent rule-set instance. |
||
246 | * |
||
247 | * @return string |
||
248 | */ |
||
249 | public function getRuleSetName() |
||
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) |
|
264 | |||
265 | /** |
||
266 | * Returns the violation report for this rule. |
||
267 | * |
||
268 | * @return \PHPMD\Report |
||
269 | */ |
||
270 | public function getReport() |
||
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) |
|
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) |
|
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) |
|
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) |
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) |
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( |
|
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 |
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.