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 |
||
14 | class Assets |
||
15 | { |
||
16 | /** |
||
17 | * @var Repository |
||
18 | */ |
||
19 | protected $config; |
||
20 | |||
21 | /** |
||
22 | * @var HtmlBuilder |
||
23 | */ |
||
24 | protected $htmlBuilder; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $scripts = []; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $styles = []; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $appendedScripts = [ |
||
40 | 'header' => [], |
||
41 | 'footer' => [], |
||
42 | ]; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $appendedStyles = []; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $build = ''; |
||
53 | |||
54 | const ASSETS_SCRIPT_POSITION_HEADER = 'header'; |
||
55 | const ASSETS_SCRIPT_POSITION_FOOTER = 'footer'; |
||
56 | |||
57 | /** |
||
58 | * Assets constructor. |
||
59 | * @author Sang Nguyen |
||
60 | * @param Repository $config |
||
61 | * @param HtmlBuilder $htmlBuilder |
||
62 | */ |
||
63 | public function __construct(Repository $config, HtmlBuilder $htmlBuilder) |
||
73 | |||
74 | /** |
||
75 | * Add Javascript to current module |
||
76 | * |
||
77 | * @param array $assets |
||
78 | * @return $this |
||
79 | * @author Sang Nguyen |
||
80 | */ |
||
81 | public function addScripts($assets) |
||
89 | |||
90 | /** |
||
91 | * Add Css to current module |
||
92 | * |
||
93 | * @param array $assets |
||
94 | * @return $this |
||
95 | * @author Sang Nguyen |
||
96 | */ |
||
97 | public function addStyles($assets) |
||
105 | |||
106 | /** |
||
107 | * @param $assets |
||
108 | * @return $this |
||
109 | * @author Sang Nguyen |
||
110 | */ |
||
111 | public function addStylesDirectly($assets) |
||
127 | |||
128 | /** |
||
129 | * @param $assets |
||
130 | * @param string $location |
||
131 | * @return $this |
||
132 | * @author Sang Nguyen |
||
133 | */ |
||
134 | public function addScriptsDirectly($assets, $location = self::ASSETS_SCRIPT_POSITION_FOOTER) |
||
151 | |||
152 | /** |
||
153 | * Remove Css to current module |
||
154 | * |
||
155 | * @param array $assets |
||
156 | * @return $this |
||
157 | * @author Sang Nguyen |
||
158 | */ |
||
159 | View Code Duplication | public function removeStyles($assets) |
|
169 | |||
170 | /** |
||
171 | * Add Javascript |
||
172 | * |
||
173 | * @param array $assets |
||
174 | * @return $this |
||
175 | * @author Sang Nguyen |
||
176 | */ |
||
177 | View Code Duplication | public function removeScripts($assets) |
|
187 | |||
188 | /** |
||
189 | * Get All Javascript in current module |
||
190 | * |
||
191 | * @param string $location : top or bottom |
||
192 | * @return array |
||
193 | * @author Sang Nguyen |
||
194 | */ |
||
195 | public function getScripts($location = null) |
||
217 | |||
218 | /** |
||
219 | * Get All CSS in current module |
||
220 | * |
||
221 | * @param array $lastModules : append last CSS to current module |
||
222 | * @return array |
||
223 | * @author Sang Nguyen |
||
224 | */ |
||
225 | public function getStyles($lastModules = []) |
||
254 | |||
255 | /** |
||
256 | * @param $name |
||
257 | * @author Sang Nguyen |
||
258 | */ |
||
259 | public function scriptToHtml($name) |
||
263 | |||
264 | /** |
||
265 | * @param $name |
||
266 | * @author Sang Nguyen |
||
267 | */ |
||
268 | public function styleToHtml($name) |
||
272 | |||
273 | /** |
||
274 | * @return string |
||
275 | * @throws \Throwable |
||
276 | * @author Sang Nguyen |
||
277 | */ |
||
278 | public function renderHeader() |
||
284 | |||
285 | /** |
||
286 | * @return string |
||
287 | * @throws \Throwable |
||
288 | * @author Sang Nguyen |
||
289 | */ |
||
290 | public function renderFooter() |
||
295 | |||
296 | /** |
||
297 | * @param $location |
||
298 | * @param $configName |
||
299 | * @param $script |
||
300 | * @return array |
||
301 | */ |
||
302 | protected function getScriptItem($location, $configName, $script) |
||
336 | |||
337 | /** |
||
338 | * Fallback to local script if CDN fails |
||
339 | * @param $src |
||
340 | * @param $configName |
||
341 | * @return array |
||
342 | */ |
||
343 | protected function getFallbackScript($src, $configName) |
||
351 | |||
352 | /** |
||
353 | * @param $name |
||
354 | * @param string $type |
||
355 | * @return null|string |
||
356 | */ |
||
357 | protected function itemToHtml($name, $type = 'style') |
||
389 | } |
||
390 |
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.