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 RotationComponent extends ComponentAbstract implements RotationCMPTIF |
|
|
|||
31 | { |
||
32 | use ComponentHelper; |
||
33 | |||
34 | /** |
||
35 | * Initialize Rotation Component |
||
36 | * |
||
37 | * The rotation component defines the orientation of an entity. |
||
38 | * It takes the |
||
39 | * roll (x), |
||
40 | * pitch (y), |
||
41 | * and yaw (z) |
||
42 | * as three space-delimited numbers indicating degrees of rotation. |
||
43 | * |
||
44 | * {@inheritdoc} |
||
45 | * |
||
46 | * @return bool |
||
47 | */ |
||
48 | 67 | public function initializeComponent(): bool |
|
53 | |||
54 | /** |
||
55 | * Get Rotation |
||
56 | * |
||
57 | * {@inheritdoc} |
||
58 | * |
||
59 | * @return string |
||
60 | */ |
||
61 | 1 | public function getRotation(): string |
|
65 | |||
66 | /** |
||
67 | * Return DOM attribute contents |
||
68 | * |
||
69 | * Scale Components dom atribute contains roll, pitch, yaw |
||
70 | * Ex: rotation="1 1 1" |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | 6 | public function getDomAttributeString(): string |
|
79 | |||
80 | /** |
||
81 | * Roll, rotation about the X-axis. |
||
82 | * |
||
83 | * {@inheritdoc} |
||
84 | * |
||
85 | * @param double $roll |
||
86 | * @return void |
||
87 | */ |
||
88 | 15 | public function roll(float $roll) |
|
93 | |||
94 | /** |
||
95 | * Pitch, rotation about the Y-axis. |
||
96 | * |
||
97 | * {@inheritdoc} |
||
98 | * |
||
99 | * @param double $pitch |
||
100 | * @return void |
||
101 | */ |
||
102 | 14 | public function pitch(float $pitch) |
|
107 | |||
108 | /** |
||
109 | * Yaw, rotation about the Z-axis. |
||
110 | * |
||
111 | * {@inheritdoc} |
||
112 | * |
||
113 | * @param double $yaw |
||
114 | * @return void |
||
115 | */ |
||
116 | 14 | public function yaw(float $yaw) |
|
121 | |||
122 | /** |
||
123 | * When any rotation component methods are called then init others |
||
124 | * |
||
125 | * {@inheritdoc} |
||
126 | * |
||
127 | * @param array $dom_attributes |
||
128 | * @return void |
||
129 | */ |
||
130 | 15 | private function init() |
|
136 | } |
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.