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 |
||
30 | trait Assets { |
||
31 | protected $extension_to_as = [ |
||
32 | 'jpeg' => 'image', |
||
33 | 'jpe' => 'image', |
||
34 | 'jpg' => 'image', |
||
35 | 'gif' => 'image', |
||
36 | 'png' => 'image', |
||
37 | 'svg' => 'image', |
||
38 | 'svgz' => 'image', |
||
39 | 'woff2' => 'font', |
||
40 | 'css' => 'style', |
||
41 | 'js' => 'script', |
||
42 | 'html' => 'document' |
||
43 | ]; |
||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $core_html; |
||
48 | /** |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $core_js; |
||
52 | /** |
||
53 | * @var array |
||
54 | */ |
||
55 | protected $core_css; |
||
56 | /** |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $core_config; |
||
60 | /** |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $html; |
||
64 | /** |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $js; |
||
68 | /** |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $css; |
||
72 | /** |
||
73 | * @var string |
||
74 | */ |
||
75 | protected $config; |
||
76 | 34 | protected function init_assets () { |
|
86 | /** |
||
87 | * @param string|string[] $add |
||
88 | * |
||
89 | * @return \cs\Page |
||
90 | */ |
||
91 | 4 | protected function core_html ($add) { |
|
94 | /** |
||
95 | * @param string|string[] $add |
||
96 | * |
||
97 | * @return \cs\Page |
||
98 | */ |
||
99 | 4 | protected function core_js ($add) { |
|
102 | /** |
||
103 | * @param string|string[] $add |
||
104 | * |
||
105 | * @return \cs\Page |
||
106 | */ |
||
107 | 4 | protected function core_css ($add) { |
|
110 | /** |
||
111 | * Including of Web Components |
||
112 | * |
||
113 | * @param string|string[] $add Path to including file, or code |
||
114 | * |
||
115 | * @return \cs\Page |
||
116 | */ |
||
117 | 2 | public function html ($add) { |
|
120 | /** |
||
121 | * Including of JavaScript |
||
122 | * |
||
123 | * @param string|string[] $add Path to including file, or code |
||
124 | * |
||
125 | * @return \cs\Page |
||
126 | */ |
||
127 | 2 | public function js ($add) { |
|
130 | /** |
||
131 | * Including of CSS |
||
132 | * |
||
133 | * @param string|string[] $add Path to including file, or code |
||
134 | * |
||
135 | * @return \cs\Page |
||
136 | */ |
||
137 | 2 | public function css ($add) { |
|
140 | /** |
||
141 | * @param string $what |
||
142 | * @param string|string[] $add |
||
143 | * @param bool $core |
||
144 | * |
||
145 | * @return \cs\Page |
||
146 | */ |
||
147 | 4 | protected function include_common ($what, $add, $core) { |
|
164 | /** |
||
165 | * Add config on page to make it available on frontend |
||
166 | * |
||
167 | * @param mixed $config_structure Any scalar type or array |
||
168 | * @param string $target Target is property of `window` object where config will be inserted as value, nested properties like `cs.sub.prop` |
||
169 | * are supported and all nested properties are created on demand. It is recommended to use sub-properties of `cs` |
||
170 | * |
||
171 | * @return \cs\Page |
||
172 | */ |
||
173 | 4 | public function config ($config_structure, $target) { |
|
176 | /** |
||
177 | * @param mixed $config_structure |
||
178 | * @param string $target |
||
179 | * @param bool $core |
||
180 | * |
||
181 | * @return \cs\Page |
||
182 | */ |
||
183 | 4 | protected function config_internal ($config_structure, $target, $core = false) { |
|
199 | /** |
||
200 | * Getting of HTML, JS and CSS assets |
||
201 | * |
||
202 | * @return \cs\Page |
||
203 | */ |
||
204 | 4 | protected function add_assets_on_page () { |
|
249 | /** |
||
250 | * @param Config $Config |
||
251 | * @param Request $Request |
||
252 | * |
||
253 | * @return bool |
||
254 | */ |
||
255 | 4 | protected function page_compression_usage ($Config, $Request) { |
|
258 | /** |
||
259 | * Hack: Add WebComponents Polyfill for browsers without native Shadow DOM support |
||
260 | * |
||
261 | * @param Request $Request |
||
262 | * @param Config $Config |
||
263 | * @param bool $with_compression |
||
264 | */ |
||
265 | 4 | protected function webcomponents_polyfill ($Request, $Config, $with_compression) { |
|
276 | /** |
||
277 | * @param Config $Config |
||
278 | * @param string $content |
||
279 | */ |
||
280 | 4 | protected function add_script_imports_to_document ($Config, $content) { |
|
287 | /** |
||
288 | * @param Request $Request |
||
289 | * |
||
290 | * @return array[] |
||
291 | */ |
||
292 | 4 | protected function get_assets_and_preload_resource_for_page_with_compression ($Request) { |
|
304 | /** |
||
305 | * @param array $dependencies |
||
306 | * @param string[][] $assets_map |
||
307 | * @param Request $Request |
||
308 | * |
||
309 | * @return string[][] |
||
310 | */ |
||
311 | 4 | protected function get_normalized_assets ($dependencies, $assets_map, $Request) { |
|
351 | /** |
||
352 | * @param array $dependencies |
||
353 | * @param string $url |
||
354 | * @param Request $Request |
||
355 | * |
||
356 | * @return false|string |
||
357 | */ |
||
358 | 4 | protected function get_dependency_component ($dependencies, $url, $Request) { |
|
370 | /** |
||
371 | * @param Config $Config |
||
372 | * @param Request $Request |
||
373 | * |
||
374 | * @return string[][] |
||
375 | */ |
||
376 | 2 | protected function get_assets_for_page_without_compression ($Config, $Request) { |
|
382 | /** |
||
383 | * @param string[][] $assets |
||
384 | * |
||
385 | * @return string[][] |
||
386 | */ |
||
387 | 2 | protected function add_versions_hash ($assets) { |
|
388 | 2 | $content = array_reduce( |
|
389 | 2 | get_files_list(DIR.'/components', '/^meta\.json$/', 'f', true, true), |
|
390 | function ($content, $file) { |
||
391 | return $content.file_get_contents($file); |
||
392 | 2 | } |
|
393 | ); |
||
394 | 2 | $content_hash = substr(md5($content), 0, 5); |
|
395 | 2 | foreach ($assets as &$files) { |
|
396 | 2 | foreach ($files as &$file) { |
|
397 | 2 | $file .= "?$content_hash"; |
|
398 | } |
||
399 | 2 | unset($file); |
|
400 | } |
||
401 | 2 | return $assets; |
|
402 | } |
||
403 | /** |
||
404 | * @param Config $Config |
||
405 | * @param Request $Request |
||
406 | * @param string[] $preload |
||
407 | */ |
||
408 | 4 | protected function add_assets_on_page_manually_added ($Config, $Request, $preload) { |
|
422 | /** |
||
423 | * @param Config $Config |
||
424 | * @param Request $Request |
||
425 | * @param string[] $preload |
||
426 | */ |
||
427 | 2 | protected function add_assets_on_page_manually_added_normal ($Config, $Request, $preload) { |
|
445 | /** |
||
446 | * @param string[] $preload |
||
447 | * @param Request $Request |
||
448 | */ |
||
449 | 4 | protected function add_preload ($preload, $Request) { |
|
462 | /** |
||
463 | * @param Config $Config |
||
464 | * @param Request $Request |
||
465 | */ |
||
466 | 4 | protected function add_assets_on_page_manually_added_frontend_load_optimization ($Config, $Request) { |
|
495 | } |
||
496 |