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 |
||
33 | final class Scene |
||
34 | { |
||
35 | /* All scenes can use primitives */ |
||
36 | use Primitives; |
||
37 | |||
38 | /** |
||
39 | * Name with what you can retrieve this scene while working with multiple scenes |
||
40 | * |
||
41 | * @var string $name |
||
42 | */ |
||
43 | private $keyword; |
||
44 | |||
45 | /** |
||
46 | * Is scene prepared for rendering |
||
47 | * |
||
48 | * @var bool |
||
49 | */ |
||
50 | private $prepared; |
||
51 | |||
52 | /** |
||
53 | * Assets |
||
54 | * |
||
55 | * @var \AframeVR\Core\Assets |
||
56 | */ |
||
57 | protected $assets; |
||
58 | |||
59 | /** |
||
60 | * Aframe Document Object Model |
||
61 | * |
||
62 | * @var \AframeVR\Core\DOM\AframeDOMDocument |
||
63 | */ |
||
64 | protected $aframeDomObj; |
||
65 | |||
66 | /** |
||
67 | * A-Frame scene entities |
||
68 | * |
||
69 | * @var array $entities |
||
70 | */ |
||
71 | protected $entities = array(); |
||
72 | |||
73 | /** |
||
74 | * Scene components |
||
75 | * |
||
76 | * @var array $components |
||
77 | */ |
||
78 | protected $components = array(); |
||
79 | |||
80 | /** |
||
81 | * Scene constructor |
||
82 | * |
||
83 | * @param string $keyword |
||
84 | * @param Config $config |
||
85 | */ |
||
86 | 98 | public function __construct(string $keyword, Config $config) |
|
93 | |||
94 | /** |
||
95 | * Aframe Document Object Model |
||
96 | * |
||
97 | * @api |
||
98 | * |
||
99 | * @return \AframeVR\Core\DOM\AframeDOMDocument |
||
100 | */ |
||
101 | 8 | public function dom() |
|
105 | |||
106 | /** |
||
107 | * Entity |
||
108 | * |
||
109 | * @api |
||
110 | * |
||
111 | * @param string $id |
||
112 | * @return \AframeVR\Core\Entity |
||
113 | */ |
||
114 | 47 | public function entity(string $id = 'untitled'): Entity |
|
118 | |||
119 | /** |
||
120 | * Assets |
||
121 | * |
||
122 | * @api |
||
123 | * |
||
124 | * @return \AframeVR\Core\Assets |
||
125 | */ |
||
126 | 98 | public function asset(): Assets |
|
130 | |||
131 | /** |
||
132 | * Render the A-Frame scene and return the HTML |
||
133 | * |
||
134 | * @api |
||
135 | * |
||
136 | * @param bool $only_scene |
||
137 | * @return string |
||
138 | */ |
||
139 | 9 | public function save($only_scene = false, string $file = null): string |
|
149 | |||
150 | /** |
||
151 | * Render the A-Frame scene and passthru HTML |
||
152 | * |
||
153 | * @api |
||
154 | * |
||
155 | * @param bool $only_scene |
||
156 | * @return void |
||
157 | */ |
||
158 | 1 | public function render($only_scene = false) |
|
162 | |||
163 | /** |
||
164 | * Alias of AframeDOMDocument::setTitle(string) |
||
165 | * |
||
166 | * Set <title> tag |
||
167 | * $this->dom()->setTitle(string); |
||
168 | * |
||
169 | * @api |
||
170 | * |
||
171 | * @param string $title |
||
172 | */ |
||
173 | 7 | public function title(string $title) |
|
177 | |||
178 | /** |
||
179 | * Alias of AframeDOMDocument::setDescription(string) |
||
180 | * |
||
181 | * Set <meta name="description"> tag |
||
182 | * $this->dom()->setDescription(string); |
||
183 | * |
||
184 | * @api |
||
185 | * |
||
186 | * @param string $description |
||
187 | */ |
||
188 | 7 | public function description(string $description) |
|
192 | |||
193 | /** |
||
194 | * Get scenes keyword |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | 1 | public function getKeyword() |
|
202 | |||
203 | /** |
||
204 | * Load component for this entity |
||
205 | * |
||
206 | * @param string $component_name |
||
207 | * @throws \AframeVR\Core\Exceptions\BadComponentCallException |
||
208 | * @return object|null |
||
209 | */ |
||
210 | 2 | View Code Duplication | public function component(string $component_name) |
224 | |||
225 | /** |
||
226 | * Handle scene components |
||
227 | * |
||
228 | * Since we might need to customize these to have |
||
229 | * custom components loaded as $this->methosd aswell therefore |
||
230 | * we have these placeholder magic methods here |
||
231 | * |
||
232 | * @param string $component_name |
||
233 | * @param array $args |
||
234 | */ |
||
235 | 2 | View Code Duplication | public function __call(string $component_name, array $args) |
246 | |||
247 | |||
248 | /** |
||
249 | * Add everyting to DOM |
||
250 | * |
||
251 | * @return void |
||
252 | */ |
||
253 | 9 | protected function prepare() |
|
270 | } |
||
271 |
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.