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 |
||
36 | class Entity implements EntityInterface |
||
37 | { |
||
38 | /** |
||
39 | * Array of used components |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $components = array(); |
||
44 | |||
45 | /** |
||
46 | * Array of used animations |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $animations = array(); |
||
51 | |||
52 | /** |
||
53 | * Child entities |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $entities = array(); |
||
58 | |||
59 | 64 | public function __construct() |
|
72 | |||
73 | 37 | public function init() |
|
75 | |||
76 | 37 | public function defaults() |
|
78 | |||
79 | /** |
||
80 | * Child entity |
||
81 | * |
||
82 | * @param string $name |
||
83 | * @return Entity |
||
84 | */ |
||
85 | 6 | public function entity(string $name = 'untitled'): Entity |
|
89 | |||
90 | /** |
||
91 | * Position component |
||
92 | * |
||
93 | * All entities inherently have the position component. |
||
94 | * |
||
95 | * @param int|float $x_axis |
||
96 | * @param int|float $y_axis |
||
97 | * @param int|float $z_axis |
||
98 | * @return \AframeVR\Core\Entity |
||
99 | */ |
||
100 | 14 | public function position(float $x_axis = 0, float $y_axis = 0, float $z_axis = 0): Entity |
|
107 | |||
108 | /** |
||
109 | * Rotation component |
||
110 | * |
||
111 | * All entities inherently have the rotation component. |
||
112 | * |
||
113 | * @param int|float $roll |
||
114 | * @param int|float $pitch |
||
115 | * @param int|float $yaw |
||
116 | * @return \AframeVR\Core\Entity |
||
117 | */ |
||
118 | 14 | public function rotation(float $roll = 0, float $pitch = 0, float $yaw = 0): Entity |
|
125 | |||
126 | /** |
||
127 | * Scale component |
||
128 | * |
||
129 | * All entities inherently have the scale component. |
||
130 | * |
||
131 | * @param int|float $scale_x |
||
132 | * @param int|float $scale_y |
||
133 | * @param int|float $scale_z |
||
134 | * @return \AframeVR\Core\Entity |
||
135 | */ |
||
136 | 9 | public function scale(float $scale_x = 1, float $scale_y = 1, float $scale_z = 1): Entity |
|
143 | |||
144 | /** |
||
145 | * Animations |
||
146 | * |
||
147 | * @param string $name |
||
148 | * @return \AframeVR\Interfaces\AnimationInterface |
||
149 | */ |
||
150 | 1 | public function animation(string $name = 'untitled'): AnimationInterface |
|
154 | |||
155 | /** |
||
156 | * Load component for this entity |
||
157 | * |
||
158 | * @param string $component_name |
||
159 | * @throws \AframeVR\Core\Exceptions\BadComponentCallException |
||
160 | * @return object|null |
||
161 | */ |
||
162 | 64 | View Code Duplication | public function component(string $component_name) |
179 | |||
180 | /** |
||
181 | * Handle entity components |
||
182 | * |
||
183 | * Since we might need to customize these to have |
||
184 | * custom components loaded as $this->methosd aswell therefore |
||
185 | * we have these placeholder magic methods here |
||
186 | * |
||
187 | * @param string $component_name |
||
188 | * @param array $args |
||
189 | */ |
||
190 | 12 | View Code Duplication | public function __call(string $component_name, array $args) |
200 | |||
201 | /** |
||
202 | * Create and add DOM element of the entity |
||
203 | * |
||
204 | * @param \DOMDocument $aframe_dom |
||
205 | * @return \DOMElement |
||
206 | */ |
||
207 | 4 | public function domElement(\DOMDocument &$aframe_dom): DOMElement |
|
223 | |||
224 | 4 | private function appendChildren(\DOMDocument &$aframe_dom, \DOMElement &$a_entity) |
|
238 | } |
||
239 |
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.