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 |
||
34 | View Code Duplication | class Position implements ComponentInterface |
|
|
|||
35 | { |
||
36 | |||
37 | /** |
||
38 | * Negative X axis extends left. |
||
39 | * Positive X Axis extends right. |
||
40 | * |
||
41 | * @var int $x |
||
42 | */ |
||
43 | protected $x; |
||
44 | |||
45 | /** |
||
46 | * Negative Y axis extends up. |
||
47 | * Positive Y Axis extends down. |
||
48 | * |
||
49 | * @var int $y |
||
50 | */ |
||
51 | protected $y; |
||
52 | |||
53 | /** |
||
54 | * Negative Z axis extends in. |
||
55 | * Positive Z Axis extends out. |
||
56 | * |
||
57 | * @var int $z |
||
58 | */ |
||
59 | protected $z; |
||
60 | |||
61 | /** |
||
62 | * Set initial coordinates |
||
63 | * |
||
64 | * @param string $coordinates |
||
65 | */ |
||
66 | 8 | public function __construct($x = 0, $y = 0, $z = 0) |
|
67 | { |
||
68 | 8 | $this->update($x, $y, $z); |
|
69 | 8 | } |
|
70 | |||
71 | /** |
||
72 | * Get Component scripts |
||
73 | * |
||
74 | * {@inheritdoc} |
||
75 | * |
||
76 | * @return array |
||
77 | */ |
||
78 | public function getScripts(): array |
||
82 | |||
83 | /** |
||
84 | * Does component have DOM Atributes |
||
85 | * |
||
86 | * {@inheritdoc} |
||
87 | * |
||
88 | * @return bool |
||
89 | */ |
||
90 | public function hasDOMAttributes(): bool |
||
94 | |||
95 | /** |
||
96 | * Remove default DOMElement Attributes which are not required |
||
97 | * |
||
98 | * @return void |
||
99 | */ |
||
100 | public function removeDefaultDOMAttributes() |
||
108 | |||
109 | /** |
||
110 | * Get DOMAttr for the entity |
||
111 | * |
||
112 | * @return DOMAttr |
||
113 | */ |
||
114 | public function getDOMAttributes(): \DOMAttr |
||
118 | |||
119 | /** |
||
120 | * Update coordinates |
||
121 | * |
||
122 | * @param string $coordinates |
||
123 | */ |
||
124 | 8 | public function update($x = 0, $y = 0, $z = 0) |
|
130 | } |
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.