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 $javascript = []; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $stylesheets = []; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $build = ''; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $appendedJs = [ |
||
45 | 'top' => [], |
||
46 | 'bottom' => [], |
||
47 | ]; |
||
48 | |||
49 | /** |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $appendedCss = []; |
||
53 | |||
54 | /** |
||
55 | * Assets constructor. |
||
56 | * @author Sang Nguyen |
||
57 | * @param Repository $config |
||
58 | * @param HtmlBuilder $htmlBuilder |
||
59 | */ |
||
60 | public function __construct(Repository $config, HtmlBuilder $htmlBuilder) |
||
70 | |||
71 | /** |
||
72 | * Add Javascript to current module |
||
73 | * |
||
74 | * @param array $assets |
||
75 | * @return $this |
||
76 | * @author Sang Nguyen |
||
77 | */ |
||
78 | public function addJavascript($assets) |
||
86 | |||
87 | /** |
||
88 | * Add Css to current module |
||
89 | * |
||
90 | * @param array $assets |
||
91 | * @return $this |
||
92 | * @author Sang Nguyen |
||
93 | */ |
||
94 | public function addStylesheets($assets) |
||
102 | |||
103 | /** |
||
104 | * @param $assets |
||
105 | * @return $this |
||
106 | * @author Sang Nguyen |
||
107 | */ |
||
108 | View Code Duplication | public function addStylesheetsDirectly($assets) |
|
121 | |||
122 | /** |
||
123 | * @param $assets |
||
124 | * @param string $location |
||
125 | * @return $this |
||
126 | * @author Sang Nguyen |
||
127 | */ |
||
128 | View Code Duplication | public function addJavascriptDirectly($assets, $location = 'bottom') |
|
142 | |||
143 | /** |
||
144 | * Remove Css to current module |
||
145 | * |
||
146 | * @param array $assets |
||
147 | * @return $this |
||
148 | * @author Sang Nguyen |
||
149 | */ |
||
150 | View Code Duplication | public function removeStylesheets($assets) |
|
160 | |||
161 | /** |
||
162 | * Add Javascript to current module |
||
163 | * |
||
164 | * @param array $assets |
||
165 | * @return $this |
||
166 | * @author Sang Nguyen |
||
167 | */ |
||
168 | View Code Duplication | public function removeJavascript($assets) |
|
178 | |||
179 | /** |
||
180 | * Get All Javascript in current module |
||
181 | * |
||
182 | * @param string $location : top or bottom |
||
183 | * @param boolean $version : show version? |
||
184 | * @return array |
||
185 | * @author Sang Nguyen |
||
186 | */ |
||
187 | public function getJavascript($location = null, $version = true) |
||
245 | |||
246 | /** |
||
247 | * Get All CSS in current module |
||
248 | * |
||
249 | * @param array $lastModules : append last CSS to current module |
||
250 | * @param boolean $version : show version? |
||
251 | * @return array |
||
252 | * @author Sang Nguyen |
||
253 | */ |
||
254 | public function getStylesheets($lastModules = [], $version = true) |
||
293 | |||
294 | /** |
||
295 | * @param $name |
||
296 | * @param bool $version |
||
297 | * @author Sang Nguyen |
||
298 | */ |
||
299 | public function javascriptToHtml($name, $version = true) |
||
303 | |||
304 | /** |
||
305 | * @param $name |
||
306 | * @param bool $version |
||
307 | * @author Sang Nguyen |
||
308 | */ |
||
309 | public function stylesheetToHtml($name, $version = true) |
||
313 | |||
314 | /** |
||
315 | * @param $name |
||
316 | * @param bool $version |
||
317 | * @param string $type |
||
318 | * @return null|string |
||
319 | */ |
||
320 | protected function itemToHtml($name, $version = true, $type = 'style') |
||
352 | |||
353 | /** |
||
354 | * @return string |
||
355 | * @throws \Throwable |
||
356 | * @author Sang Nguyen |
||
357 | */ |
||
358 | public function renderHeader() |
||
364 | |||
365 | /** |
||
366 | * @return string |
||
367 | * @throws \Throwable |
||
368 | * @author Sang Nguyen |
||
369 | */ |
||
370 | public function renderFooter() |
||
375 | } |
||
376 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..