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 |
||
37 | class Entity implements EntityInterface |
||
38 | { |
||
39 | /** |
||
40 | * Array of used components |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $components = array(); |
||
45 | |||
46 | /** |
||
47 | * Array of used animations |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $animations = array(); |
||
52 | |||
53 | /** |
||
54 | * Dom element attributes |
||
55 | * |
||
56 | * @var unknown |
||
57 | */ |
||
58 | protected $attrs = array(); |
||
59 | |||
60 | /** |
||
61 | * Children Factory |
||
62 | * |
||
63 | * @var EntityChildrenFactory |
||
64 | */ |
||
65 | protected $childrenFactory; |
||
66 | |||
67 | /** |
||
68 | * Indent used when rendering formated outout |
||
69 | * |
||
70 | * @var unknown |
||
71 | */ |
||
72 | private $indentation = 0; |
||
73 | |||
74 | /** |
||
75 | * Constructor |
||
76 | * |
||
77 | * @param string $id |
||
78 | */ |
||
79 | 76 | public function __construct(string $id = 'untitled') |
|
94 | |||
95 | 45 | public function init() |
|
98 | |||
99 | 45 | public function defaults() |
|
102 | |||
103 | /** |
||
104 | * Set DOM attributes |
||
105 | * |
||
106 | * @param string $attr |
||
107 | * @param string $val |
||
108 | * @return void |
||
109 | */ |
||
110 | 76 | public function attr(string $attr, string $val) |
|
114 | |||
115 | /** |
||
116 | * Child entity / primitive |
||
117 | * |
||
118 | * @return EntityChildrenFactory |
||
119 | */ |
||
120 | 7 | public function child(): EntityChildrenFactory |
|
124 | |||
125 | /** |
||
126 | * Position component |
||
127 | * |
||
128 | * All entities inherently have the position component. |
||
129 | * |
||
130 | * @param int|float $x_axis |
||
131 | * @param int|float $y_axis |
||
132 | * @param int|float $z_axis |
||
133 | * @return \AframeVR\Core\Entity |
||
134 | */ |
||
135 | 15 | public function position(float $x_axis = 0, float $y_axis = 0, float $z_axis = 0): Entity |
|
142 | |||
143 | /** |
||
144 | * Rotation component |
||
145 | * |
||
146 | * All entities inherently have the rotation component. |
||
147 | * |
||
148 | * @param int|float $roll |
||
149 | * @param int|float $pitch |
||
150 | * @param int|float $yaw |
||
151 | * @return \AframeVR\Core\Entity |
||
152 | */ |
||
153 | 14 | public function rotation(float $roll = 0, float $pitch = 0, float $yaw = 0): Entity |
|
160 | |||
161 | /** |
||
162 | * Scale component |
||
163 | * |
||
164 | * All entities inherently have the scale component. |
||
165 | * |
||
166 | * @param int|float $scale_x |
||
167 | * @param int|float $scale_y |
||
168 | * @param int|float $scale_z |
||
169 | * @return \AframeVR\Core\Entity |
||
170 | */ |
||
171 | 9 | public function scale(float $scale_x = 1, float $scale_y = 1, float $scale_z = 1): Entity |
|
178 | |||
179 | /** |
||
180 | * Animations |
||
181 | * |
||
182 | * @param mixed $name |
||
183 | * @return \AframeVR\Interfaces\AnimationInterface |
||
184 | */ |
||
185 | 5 | public function animation($name = 'untitled'): AnimationInterface |
|
189 | |||
190 | /** |
||
191 | * Set mixin attribute |
||
192 | * |
||
193 | * @param string $id |
||
194 | * @return \AframeVR\Core\Entity |
||
195 | */ |
||
196 | 3 | public function mixin(string $id) |
|
201 | |||
202 | /** |
||
203 | * Load component for this entity |
||
204 | * |
||
205 | * @param string $component_name |
||
206 | * @throws \AframeVR\Core\Exceptions\BadComponentCallException |
||
207 | * @return object|null |
||
208 | */ |
||
209 | 76 | public function component(string $component_name) |
|
223 | |||
224 | /** |
||
225 | * Handle entity components |
||
226 | * |
||
227 | * Since we might need to customize these to have |
||
228 | * custom components loaded as $this->methosd aswell therefore |
||
229 | * we have these placeholder magic methods here |
||
230 | * |
||
231 | * @param string $component_name |
||
232 | * @param array $args |
||
233 | */ |
||
234 | 21 | View Code Duplication | public function __call(string $component_name, array $args) |
245 | |||
246 | /** |
||
247 | * Create and add DOM element of the entity |
||
248 | * |
||
249 | * @param \DOMDocument $aframe_dom |
||
250 | * @return \DOMElement |
||
251 | */ |
||
252 | 4 | public function domElement(\DOMDocument &$aframe_dom): DOMElement |
|
271 | |||
272 | /** |
||
273 | * Append DOM attributes no set by components |
||
274 | * |
||
275 | * @param \DOMElement $a_entity |
||
276 | */ |
||
277 | 4 | private function appendAttributes(\DOMElement &$a_entity) |
|
283 | |||
284 | 4 | private function setAttribute(&$a_entity, $attr, $val) |
|
291 | |||
292 | /** |
||
293 | * Append childern to entities DOM element |
||
294 | * |
||
295 | * @param \DOMDocument $aframe_dom |
||
296 | * @param \DOMElement $a_entity |
||
297 | */ |
||
298 | 4 | private function appendChildren(\DOMDocument &$aframe_dom, \DOMElement &$a_entity) |
|
308 | |||
309 | /** |
||
310 | * Append childern to entities DOM element |
||
311 | * |
||
312 | * @param \DOMDocument $aframe_dom |
||
313 | * @param \DOMElement $a_entity |
||
314 | */ |
||
315 | 4 | private function appendAnimations(\DOMDocument &$aframe_dom, \DOMElement &$a_entity) |
|
323 | |||
324 | /** |
||
325 | * Add format comment |
||
326 | * |
||
327 | * @param \DOMDocument $aframe_dom |
||
328 | * @param \DOMElement $a_entity |
||
329 | * @param string $content |
||
330 | */ |
||
331 | 2 | private function addFormatComment(\DOMDocument &$aframe_dom, \DOMElement &$a_entity, string $content) |
|
338 | } |
||
339 |
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.