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:
Complex classes like Assets often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Assets, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Assets |
||
13 | { |
||
14 | /** |
||
15 | * @var Repository |
||
16 | */ |
||
17 | protected $config; |
||
18 | |||
19 | /** |
||
20 | * @var HtmlBuilder |
||
21 | */ |
||
22 | protected $htmlBuilder; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $scripts = []; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $styles = []; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $appendedScripts = [ |
||
38 | 'header' => [], |
||
39 | 'footer' => [], |
||
40 | ]; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $appendedStyles = []; |
||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $build = ''; |
||
51 | |||
52 | const ASSETS_SCRIPT_POSITION_HEADER = 'header'; |
||
53 | |||
54 | const ASSETS_SCRIPT_POSITION_FOOTER = 'footer'; |
||
55 | |||
56 | /** |
||
57 | * Assets constructor. |
||
58 | * |
||
59 | * @param Repository $config |
||
60 | * @param HtmlBuilder $htmlBuilder |
||
61 | */ |
||
62 | public function __construct(Repository $config, HtmlBuilder $htmlBuilder) |
||
72 | |||
73 | /** |
||
74 | * Add scripts to current module. |
||
75 | * |
||
76 | * @param array $assets |
||
77 | * @return $this |
||
78 | */ |
||
79 | public function addScripts($assets) |
||
89 | |||
90 | /** |
||
91 | * Add Css to current module. |
||
92 | * |
||
93 | * @param array $assets |
||
94 | * @return $this |
||
95 | */ |
||
96 | public function addStyles($assets) |
||
106 | |||
107 | /** |
||
108 | * Add styles directly. |
||
109 | * |
||
110 | * @param array|string $assets |
||
111 | * @return $this |
||
112 | */ |
||
113 | public function addStylesDirectly($assets) |
||
130 | |||
131 | /** |
||
132 | * Add scripts directly. |
||
133 | * |
||
134 | * @param string|array $assets |
||
135 | * @param string $location |
||
136 | * @return $this |
||
137 | */ |
||
138 | public function addScriptsDirectly($assets, $location = self::ASSETS_SCRIPT_POSITION_FOOTER) |
||
155 | |||
156 | /** |
||
157 | * Remove Css to current module. |
||
158 | * |
||
159 | * @param array $assets |
||
160 | * @return $this |
||
161 | */ |
||
162 | View Code Duplication | public function removeStyles($assets) |
|
174 | |||
175 | /** |
||
176 | * Add scripts. |
||
177 | * |
||
178 | * @param array $assets |
||
179 | * @return $this |
||
180 | */ |
||
181 | View Code Duplication | public function removeScripts($assets) |
|
193 | |||
194 | /** |
||
195 | * Get All scripts in current module. |
||
196 | * |
||
197 | * @param string $location `header` or `footer` |
||
198 | * @return array |
||
199 | */ |
||
200 | public function getScripts($location = null) |
||
224 | |||
225 | /** |
||
226 | * Get All CSS in current module. |
||
227 | * |
||
228 | * @param array $lastStyles Append last CSS to current module |
||
229 | * @return array |
||
230 | */ |
||
231 | public function getStyles($lastStyles = []) |
||
265 | |||
266 | /** |
||
267 | * Convert script to html. |
||
268 | * |
||
269 | * @param string $name |
||
270 | * @return string|null |
||
271 | */ |
||
272 | public function scriptToHtml($name) |
||
276 | |||
277 | /** |
||
278 | * Convert style to html. |
||
279 | * |
||
280 | * @param string $name |
||
281 | */ |
||
282 | public function styleToHtml($name) |
||
286 | |||
287 | /** |
||
288 | * Render assets to header. |
||
289 | * |
||
290 | * @param array $lastStyles |
||
291 | * @return string |
||
292 | * @throws \Throwable |
||
293 | */ |
||
294 | public function renderHeader($lastStyles = []) |
||
302 | |||
303 | /** |
||
304 | * Render assets to footer. |
||
305 | * |
||
306 | * @return string |
||
307 | * @throws \Throwable |
||
308 | */ |
||
309 | public function renderFooter() |
||
315 | |||
316 | /** |
||
317 | * Get script item. |
||
318 | * |
||
319 | * @param string $location |
||
320 | * @param string $configName |
||
321 | * @param string $script |
||
322 | * @return array |
||
323 | */ |
||
324 | protected function getScriptItem($location, $configName, $script) |
||
369 | |||
370 | /** |
||
371 | * Fallback to local script if CDN fails. |
||
372 | * |
||
373 | * @param string $src |
||
374 | * @param string $configName |
||
375 | * @return array |
||
376 | */ |
||
377 | protected function getFallbackScript($src, $configName) |
||
385 | |||
386 | /** |
||
387 | * Convert item to html. |
||
388 | * |
||
389 | * @param string $name |
||
390 | * @param string $type |
||
391 | * @return null|string |
||
392 | */ |
||
393 | protected function itemToHtml($name, $type = 'style') |
||
425 | |||
426 | /** |
||
427 | * @return string |
||
428 | */ |
||
429 | public function getBuildVersion() |
||
433 | |||
434 | /** |
||
435 | * @return HtmlBuilder |
||
436 | */ |
||
437 | public function getHtmlBuilder() |
||
441 | } |
||
442 |
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.