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) |
|
124 | |||
125 | /** |
||
126 | * @param $assets |
||
127 | * @param string $location |
||
128 | * @return $this |
||
129 | * @author Sang Nguyen |
||
130 | */ |
||
131 | View Code Duplication | public function addJavascriptDirectly($assets, $location = 'bottom') |
|
145 | |||
146 | /** |
||
147 | * Remove Css to current module |
||
148 | * |
||
149 | * @param array $assets |
||
150 | * @return $this |
||
151 | * @author Sang Nguyen |
||
152 | */ |
||
153 | View Code Duplication | public function removeStylesheets($assets) |
|
163 | |||
164 | /** |
||
165 | * Add Javascript to current module |
||
166 | * |
||
167 | * @param array $assets |
||
168 | * @return $this |
||
169 | * @author Sang Nguyen |
||
170 | */ |
||
171 | View Code Duplication | public function removeJavascript($assets) |
|
181 | |||
182 | /** |
||
183 | * Get All Javascript in current module |
||
184 | * |
||
185 | * @param string $location : top or bottom |
||
186 | * @param boolean $version : show version? |
||
187 | * @return array |
||
188 | * @author Sang Nguyen |
||
189 | */ |
||
190 | public function getJavascript($location = null, $version = true) |
||
242 | |||
243 | /** |
||
244 | * Get All CSS in current module |
||
245 | * |
||
246 | * @param array $lastModules : append last CSS to current module |
||
247 | * @param boolean $version : show version? |
||
248 | * @return array |
||
249 | * @author Sang Nguyen |
||
250 | */ |
||
251 | public function getStylesheets($lastModules = [], $version = true) |
||
280 | |||
281 | /** |
||
282 | * @param $name |
||
283 | * @param bool $version |
||
284 | * @author Sang Nguyen |
||
285 | */ |
||
286 | public function javascriptToHtml($name, $version = true) |
||
290 | |||
291 | /** |
||
292 | * @param $name |
||
293 | * @param bool $version |
||
294 | * @author Sang Nguyen |
||
295 | */ |
||
296 | public function stylesheetToHtml($name, $version = true) |
||
300 | |||
301 | /** |
||
302 | * @param $name |
||
303 | * @param bool $version |
||
304 | * @param string $type |
||
305 | * @return null|string |
||
306 | */ |
||
307 | protected function itemToHtml($name, $version = true, $type = 'style') |
||
339 | |||
340 | /** |
||
341 | * @return string |
||
342 | * @throws \Throwable |
||
343 | * @author Sang Nguyen |
||
344 | */ |
||
345 | public function renderHeader() |
||
351 | |||
352 | /** |
||
353 | * @return string |
||
354 | * @throws \Throwable |
||
355 | * @author Sang Nguyen |
||
356 | */ |
||
357 | public function renderFooter() |
||
362 | } |
||
363 |
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..