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 |
||
29 | class OrangePaymentSlip extends PaymentSlip |
||
30 | { |
||
31 | /** |
||
32 | * The orange payment slip value object, which contains the payment slip data |
||
33 | * |
||
34 | * @var OrangePaymentSlipData |
||
35 | */ |
||
36 | protected $paymentSlipData = null; |
||
37 | |||
38 | /** |
||
39 | * The height of the slip |
||
40 | * |
||
41 | * @var int|float |
||
42 | */ |
||
43 | protected $slipHeight = 106; // default height of an orange slip |
||
44 | |||
45 | /** |
||
46 | * The width of the slip |
||
47 | * |
||
48 | * @var int|float |
||
49 | */ |
||
50 | protected $slipWidth = 210; // default width of an orange slip |
||
51 | |||
52 | /** |
||
53 | * Determines whether the reference number should be displayed |
||
54 | * |
||
55 | * @var bool |
||
56 | */ |
||
57 | protected $displayReferenceNr = true; |
||
58 | |||
59 | /** |
||
60 | * Determines whether the code line at the bottom should be displayed |
||
61 | * |
||
62 | * @var bool |
||
63 | */ |
||
64 | protected $displayCodeLine = true; |
||
65 | |||
66 | /** |
||
67 | * Whether to return the reference number formatted in blocks of five (for better readability) |
||
68 | * |
||
69 | * @var bool |
||
70 | */ |
||
71 | protected $referenceNrFormatted = true; |
||
72 | |||
73 | /** |
||
74 | * Whether to fill up the reference number with leading zeros, |
||
75 | * only applies to the case where no banking customer ID is used. |
||
76 | * |
||
77 | * @var bool |
||
78 | */ |
||
79 | protected $referenceNrFillZeros = true; |
||
80 | |||
81 | /** |
||
82 | * Attributes of the left reference number element |
||
83 | * |
||
84 | * @var array |
||
85 | */ |
||
86 | protected $referenceNumberLeftAttr = []; |
||
87 | |||
88 | /** |
||
89 | * Attributes of the right reference number element |
||
90 | * |
||
91 | * @var array |
||
92 | */ |
||
93 | protected $referenceNumberRightAttr = []; |
||
94 | |||
95 | /** |
||
96 | * Attributes of the code line element |
||
97 | * |
||
98 | * @var array |
||
99 | */ |
||
100 | protected $codeLineAttr = []; |
||
101 | |||
102 | /** |
||
103 | * Create a new orange payment slip |
||
104 | * |
||
105 | * @param OrangePaymentSlipData $paymentSlipData The orange payment slip data. |
||
106 | * @param float|null $slipPosX The optional X position of the slip. |
||
107 | * @param float|null $slipPosY The optional Y position of the slip. |
||
108 | */ |
||
109 | public function __construct(OrangePaymentSlipData $paymentSlipData, $slipPosX = null, $slipPosY = null) |
||
110 | { |
||
111 | parent::__construct($paymentSlipData, $slipPosX, $slipPosY); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Sets the default attributes of the elements for an orange slip |
||
116 | * |
||
117 | * @return $this The current instance for a fluent interface. |
||
118 | */ |
||
119 | 4 | protected function setDefaults() |
|
131 | |||
132 | /** |
||
133 | * Get the slip data object of the slip |
||
134 | * |
||
135 | * @return OrangePaymentSlipData The data object of the slip. |
||
136 | */ |
||
137 | 1 | public function getPaymentSlipData() |
|
141 | |||
142 | /** |
||
143 | * Set the left reference number attributes |
||
144 | * |
||
145 | * @param float|null $posX The X position. |
||
146 | * @param float|null $posY The Y Position. |
||
147 | * @param float|null $width The width. |
||
148 | * @param float|null $height The height. |
||
149 | * @param string|null $background The background. |
||
150 | * @param string|null $fontFamily The font family. |
||
151 | * @param float|null $fontSize The font size. |
||
152 | * @param string|null $fontColor The font color. |
||
153 | * @param float|null $lineHeight The line height. |
||
154 | * @param string|null $textAlign The text alignment. |
||
155 | * @return $this The current instance for a fluent interface. |
||
156 | */ |
||
157 | 1 | View Code Duplication | public function setReferenceNumberLeftAttr( |
185 | |||
186 | /** |
||
187 | * Set the right reference number attributes |
||
188 | * |
||
189 | * @param float|null $posX The X position. |
||
190 | * @param float|null $posY The Y Position. |
||
191 | * @param float|null $width The width. |
||
192 | * @param float|null $height The height. |
||
193 | * @param string|null $background The background. |
||
194 | * @param string|null $fontFamily The font family. |
||
195 | * @param float|null $fontSize The font size. |
||
196 | * @param string|null $fontColor The font color. |
||
197 | * @param float|null $lineHeight The line height. |
||
198 | * @param string|null $textAlign The text alignment. |
||
199 | * @return $this The current instance for a fluent interface. |
||
200 | */ |
||
201 | 1 | View Code Duplication | public function setReferenceNumberRightAttr( |
233 | |||
234 | /** |
||
235 | * Set the code line attributes |
||
236 | * |
||
237 | * @param float|null $posX The X position. |
||
238 | * @param float|null $posY The Y Position. |
||
239 | * @param float|null $width The width. |
||
240 | * @param float|null $height The height. |
||
241 | * @param string|null $background The background. |
||
242 | * @param string|null $fontFamily The font family. |
||
243 | * @param float|null $fontSize The font size. |
||
244 | * @param string|null $fontColor The font color. |
||
245 | * @param float|null $lineHeight The line height. |
||
246 | * @param string|null $textAlign The text alignment. |
||
247 | * @return $this The current instance for a fluent interface. |
||
248 | */ |
||
249 | 1 | View Code Duplication | public function setCodeLineAttr( |
281 | |||
282 | /** |
||
283 | * Get the attributes of the left reference number element |
||
284 | * |
||
285 | * @return array The attributes of the left reference number element. |
||
286 | */ |
||
287 | 1 | public function getReferenceNumberLeftAttr() |
|
291 | |||
292 | /** |
||
293 | * Get the attributes of the right reference umber element |
||
294 | * |
||
295 | * @return array The attributes of the right reference umber element. |
||
296 | */ |
||
297 | 1 | public function getReferenceNumberRightAttr() |
|
301 | |||
302 | /** |
||
303 | * Get the attributes of the code line element |
||
304 | * |
||
305 | * @return array The attributes of the code line element. |
||
306 | */ |
||
307 | 1 | public function getCodeLineAttr() |
|
311 | |||
312 | /** |
||
313 | * Set whether or not to display the reference number |
||
314 | * |
||
315 | * @param bool $displayReferenceNr True if yes, false if no |
||
316 | * @return $this The current instance for a fluent interface. |
||
317 | */ |
||
318 | 2 | public function setDisplayReferenceNr($displayReferenceNr = true) |
|
325 | |||
326 | /** |
||
327 | * Get whether or not to display the reference number |
||
328 | * |
||
329 | * @return bool True if yes, false if no. |
||
330 | */ |
||
331 | 1 | public function getDisplayReferenceNr() |
|
338 | |||
339 | /** |
||
340 | * Set whether or not to display the code line at the bottom |
||
341 | * |
||
342 | * @param bool $displayCodeLine True if yes, false if no |
||
343 | * @return $this The current instance for a fluent interface. |
||
344 | */ |
||
345 | 2 | public function setDisplayCodeLine($displayCodeLine = true) |
|
352 | |||
353 | /** |
||
354 | * Set whether to format the reference number for better readability |
||
355 | * |
||
356 | * @param bool $referenceNrFormatted True if yes, else false. |
||
357 | * @return $this The current instance for a fluent interface. |
||
358 | */ |
||
359 | 2 | public function setReferenceNrFormatted($referenceNrFormatted) |
|
365 | |||
366 | /** |
||
367 | * Get whether to format the reference number for better readability |
||
368 | * |
||
369 | * @return bool True if yes, else false. |
||
370 | */ |
||
371 | 1 | public function getReferenceNrFormatted() |
|
375 | |||
376 | /** |
||
377 | * Set whether to fill the reference number with zeros |
||
378 | * |
||
379 | * @param bool $referenceNrFillZeros True if yes, else false. |
||
380 | * @return $this The current instance for a fluent interface. |
||
381 | */ |
||
382 | 2 | public function setReferenceNrFillZeros($referenceNrFillZeros) |
|
388 | |||
389 | /** |
||
390 | * Get whether to fill the reference number with zeros |
||
391 | * |
||
392 | * @return bool True if yes, else false. |
||
393 | */ |
||
394 | 1 | public function getReferenceNrFillZeros() |
|
398 | |||
399 | /** |
||
400 | * Get whether or not to display the code line at the bottom |
||
401 | * |
||
402 | * Overwrites the parent method as it checks additional settings. |
||
403 | * |
||
404 | * @return bool True if yes, false if no. |
||
405 | */ |
||
406 | 1 | public function getDisplayCodeLine() |
|
415 | |||
416 | /** |
||
417 | * Get all elements of the slip |
||
418 | * |
||
419 | * @return array All elements with their lines and attributes. |
||
420 | */ |
||
421 | 3 | public function getAllElements() |
|
462 | } |
||
463 |
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.