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 |
||
30 | View Code Duplication | class PositionComponent extends ComponentAbstract implements PositionCMPTIF |
|
|
|||
31 | { |
||
32 | use ComponentHelper; |
||
33 | |||
34 | /** |
||
35 | * Initialize Position Component |
||
36 | * |
||
37 | * The position component defines where an entity is placed in the scene's world space. |
||
38 | * It takes a coordinate value as three space-delimited numbers. |
||
39 | * |
||
40 | * {@inheritdoc} |
||
41 | * |
||
42 | * @return bool |
||
43 | * |
||
44 | */ |
||
45 | 67 | public function initializeComponent(): bool |
|
51 | |||
52 | /** |
||
53 | * Return DOM attribute contents |
||
54 | * |
||
55 | * Position Components dom atribute contains coordinates to point in world |
||
56 | * Ex: position="1 1 1" |
||
57 | * |
||
58 | * @return string |
||
59 | */ |
||
60 | 6 | public function getDomAttributeString(): string |
|
66 | |||
67 | /** |
||
68 | * Get current position coordinates |
||
69 | * |
||
70 | * {@inheritdoc} |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | 1 | public function getPosition(): string |
|
78 | |||
79 | /** |
||
80 | * Negative X axis extends left. |
||
81 | * Positive X Axis extends right. |
||
82 | * |
||
83 | * @param float $x_axis |
||
84 | * @return void |
||
85 | */ |
||
86 | 15 | public function positionX(float $x_axis) |
|
91 | |||
92 | /** |
||
93 | * Negative Y axis extends up. |
||
94 | * Positive Y Axis extends down. |
||
95 | * |
||
96 | * @param float $y_axis |
||
97 | * @return void |
||
98 | */ |
||
99 | 14 | public function positionY(float $y_axis) |
|
104 | |||
105 | /** |
||
106 | * Negative Z axis extends in. |
||
107 | * Positive Z Axis extends out. |
||
108 | * |
||
109 | * @param float $z_axis |
||
110 | * @return void |
||
111 | */ |
||
112 | 14 | public function positionZ(float $z_axis) |
|
117 | |||
118 | /** |
||
119 | * When any position component methods are called then init others |
||
120 | * |
||
121 | * @return void |
||
122 | */ |
||
123 | 15 | private function init() |
|
129 | } |
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.