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 |
||
| 41 | class Entity implements EntityInterface |
||
| 42 | { |
||
| 43 | use MeshAttributes; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Array of used components |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $components = array(); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Array of used animations |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $animations = array(); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Dom element attributes |
||
| 61 | * |
||
| 62 | * @var unknown |
||
| 63 | */ |
||
| 64 | protected $attrs = array(); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Children Factory |
||
| 68 | * |
||
| 69 | * @var EntityChildrenFactory |
||
| 70 | */ |
||
| 71 | protected $childrenFactory; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Indent used when rendering formated outout |
||
| 75 | * |
||
| 76 | * @var unknown |
||
| 77 | */ |
||
| 78 | private $indentation = 0; |
||
| 79 | 110 | ||
| 80 | /** |
||
| 81 | 110 | * Constructor |
|
| 82 | * |
||
| 83 | * @param string $id |
||
| 84 | 110 | */ |
|
| 85 | 110 | public function __construct(string $id = 'untitled') |
|
| 92 | 110 | ||
| 93 | 110 | /** |
|
| 94 | * Reset primitive default attributtes |
||
| 95 | 60 | * |
|
| 96 | * {@inheritdoc} |
||
| 97 | 60 | * |
|
| 98 | * @return void |
||
| 99 | 73 | */ |
|
| 100 | public function reset() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Set DOM attributes |
||
| 110 | 110 | * |
|
| 111 | * @param string $attr |
||
| 112 | 110 | * @param string $val |
|
| 113 | 110 | * @return void |
|
| 114 | */ |
||
| 115 | public function attr(string $attr, string $val) |
||
| 119 | |||
| 120 | 7 | /** |
|
| 121 | * Child entity / primitive |
||
| 122 | 7 | * |
|
| 123 | * @return EntityChildrenFactory |
||
|
|
|||
| 124 | */ |
||
| 125 | public function child(): EntityChildrenFactory |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Position component |
||
| 132 | * |
||
| 133 | * All entities inherently have the position component. |
||
| 134 | * |
||
| 135 | 21 | * @param int|float $x_axis |
|
| 136 | * @param int|float $y_axis |
||
| 137 | 21 | * @param int|float $z_axis |
|
| 138 | 21 | * @return EntityInterface |
|
| 139 | 21 | */ |
|
| 140 | 21 | public function position(float $x_axis = 0, float $y_axis = 0, float $z_axis = 0): EntityInterface |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Rotation component |
||
| 150 | * |
||
| 151 | * All entities inherently have the rotation component. |
||
| 152 | * |
||
| 153 | 20 | * @param int|float $roll |
|
| 154 | * @param int|float $pitch |
||
| 155 | 20 | * @param int|float $yaw |
|
| 156 | 20 | * @return EntityInterface |
|
| 157 | 20 | */ |
|
| 158 | 20 | public function rotation(float $roll = 0, float $pitch = 0, float $yaw = 0): EntityInterface |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Scale component |
||
| 168 | * |
||
| 169 | * All entities inherently have the scale component. |
||
| 170 | * |
||
| 171 | 17 | * @param int|float $scale_x |
|
| 172 | * @param int|float $scale_y |
||
| 173 | 17 | * @param int|float $scale_z |
|
| 174 | 17 | * @return EntityInterface |
|
| 175 | 17 | */ |
|
| 176 | 17 | public function scale(float $scale_x = 1, float $scale_y = 1, float $scale_z = 1): EntityInterface |
|
| 183 | |||
| 184 | /** |
||
| 185 | 5 | * Animations |
|
| 186 | * |
||
| 187 | 5 | * @param mixed $name |
|
| 188 | * @return \AframeVR\Interfaces\AnimationInterface |
||
| 189 | */ |
||
| 190 | public function animation($name = 'untitled'): AnimationInterface |
||
| 194 | |||
| 195 | /** |
||
| 196 | 3 | * Set mixin attribute |
|
| 197 | * |
||
| 198 | 3 | * @param string $id |
|
| 199 | 3 | * @return \AframeVR\Core\Entity |
|
| 200 | */ |
||
| 201 | public function mixin(string $id) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Load component for this entity |
||
| 209 | 110 | * |
|
| 210 | * @param string $component_name |
||
| 211 | 110 | * @throws \AframeVR\Core\Exceptions\BadComponentCallException |
|
| 212 | 110 | * @return object|null |
|
| 213 | */ |
||
| 214 | 110 | View Code Duplication | public function component(string $component_name) |
| 228 | |||
| 229 | /** |
||
| 230 | * Handle entity components |
||
| 231 | * |
||
| 232 | * Since we might need to customize these to have |
||
| 233 | * custom components loaded as $this->methosd aswell therefore |
||
| 234 | 32 | * we have these placeholder magic methods here |
|
| 235 | * |
||
| 236 | 32 | * @param string $component_name |
|
| 237 | 32 | * @param array $args |
|
| 238 | 32 | */ |
|
| 239 | 32 | View Code Duplication | public function __call(string $component_name, array $args) |
| 250 | |||
| 251 | /** |
||
| 252 | 5 | * Create and add DOM element of the entity |
|
| 253 | * |
||
| 254 | 5 | * @param \DOMDocument $aframe_dom |
|
| 255 | * @return \DOMElement |
||
| 256 | 5 | */ |
|
| 257 | public function domElement(\DOMDocument &$aframe_dom): DOMElement |
||
| 276 | |||
| 277 | 5 | /** |
|
| 278 | * Append DOM attributes no set by components |
||
| 279 | 5 | * |
|
| 280 | 5 | * @param \DOMElement $a_entity |
|
| 281 | */ |
||
| 282 | 5 | private function appendAttributes(\DOMElement &$a_entity) |
|
| 288 | |||
| 289 | 2 | View Code Duplication | private function setAttribute(&$a_entity, $attr, $val) |
| 296 | |||
| 297 | /** |
||
| 298 | 5 | * Append childern to entities DOM element |
|
| 299 | * |
||
| 300 | 5 | * @param \DOMDocument $aframe_dom |
|
| 301 | 2 | * @param \DOMElement $a_entity |
|
| 302 | 2 | */ |
|
| 303 | 2 | private function appendChildren(\DOMDocument &$aframe_dom, \DOMElement &$a_entity) |
|
| 313 | |||
| 314 | /** |
||
| 315 | 5 | * Append childern to entities DOM element |
|
| 316 | * |
||
| 317 | 5 | * @param \DOMDocument $aframe_dom |
|
| 318 | 2 | * @param \DOMElement $a_entity |
|
| 319 | 2 | */ |
|
| 320 | 2 | private function appendAnimations(\DOMDocument &$aframe_dom, \DOMElement &$a_entity) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Add format comment |
||
| 331 | 2 | * |
|
| 332 | * @param \DOMDocument $aframe_dom |
||
| 333 | 2 | * @param \DOMElement $a_entity |
|
| 334 | 2 | * @param string $content |
|
| 335 | 2 | */ |
|
| 336 | private function addFormatComment(\DOMDocument &$aframe_dom, \DOMElement &$a_entity, string $content) |
||
| 343 | } |
||
| 344 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.