Complex classes like Includes 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 Includes, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | trait Includes { |
||
31 | use |
||
32 | Cache, |
||
33 | Collecting, |
||
34 | RequireJS; |
||
35 | protected $extension_to_as = [ |
||
36 | 'jpeg' => 'image', |
||
37 | 'jpe' => 'image', |
||
38 | 'jpg' => 'image', |
||
39 | 'gif' => 'image', |
||
40 | 'png' => 'image', |
||
41 | 'svg' => 'image', |
||
42 | 'svgz' => 'image', |
||
43 | 'woff' => 'font', |
||
44 | //'woff2' => 'font', |
||
45 | 'css' => 'style', |
||
46 | 'js' => 'script', |
||
47 | 'html' => 'document' |
||
48 | ]; |
||
49 | /** |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $core_html; |
||
53 | /** |
||
54 | * @var array |
||
55 | */ |
||
56 | protected $core_js; |
||
57 | /** |
||
58 | * @var array |
||
59 | */ |
||
60 | protected $core_css; |
||
61 | /** |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $core_config; |
||
65 | /** |
||
66 | * @var array |
||
67 | */ |
||
68 | protected $html; |
||
69 | /** |
||
70 | * @var array |
||
71 | */ |
||
72 | protected $js; |
||
73 | /** |
||
74 | * @var array |
||
75 | */ |
||
76 | protected $css; |
||
77 | /** |
||
78 | * @var string |
||
79 | */ |
||
80 | protected $config; |
||
81 | /** |
||
82 | * Base name is used as prefix when creating CSS/JS/HTML cache files in order to avoid collisions when having several themes and languages |
||
83 | * @var string |
||
84 | */ |
||
85 | protected $pcache_basename_path; |
||
86 | 36 | protected function init_includes () { |
|
97 | /** |
||
98 | * Including of Web Components |
||
99 | * |
||
100 | * @param string|string[] $add Path to including file, or code |
||
101 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
102 | * |
||
103 | * @return \cs\Page |
||
104 | */ |
||
105 | public function html ($add, $mode = 'file') { |
||
108 | /** |
||
109 | * @param string|string[] $add |
||
110 | * @param string $mode |
||
111 | * @param bool $core |
||
112 | * |
||
113 | * @return \cs\Page |
||
114 | */ |
||
115 | 4 | protected function html_internal ($add, $mode = 'file', $core = false) { |
|
118 | /** |
||
119 | * Including of JavaScript |
||
120 | * |
||
121 | * @param string|string[] $add Path to including file, or code |
||
122 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
123 | * |
||
124 | * @return \cs\Page |
||
125 | */ |
||
126 | public function js ($add, $mode = 'file') { |
||
129 | /** |
||
130 | * @param string|string[] $add |
||
131 | * @param string $mode |
||
132 | * @param bool $core |
||
133 | * |
||
134 | * @return \cs\Page |
||
135 | */ |
||
136 | 4 | protected function js_internal ($add, $mode = 'file', $core = false) { |
|
139 | /** |
||
140 | * Including of CSS |
||
141 | * |
||
142 | * @param string|string[] $add Path to including file, or code |
||
143 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
144 | * |
||
145 | * @return \cs\Page |
||
146 | */ |
||
147 | public function css ($add, $mode = 'file') { |
||
150 | /** |
||
151 | * @param string|string[] $add |
||
152 | * @param string $mode |
||
153 | * @param bool $core |
||
154 | * |
||
155 | * @return \cs\Page |
||
156 | */ |
||
157 | 4 | protected function css_internal ($add, $mode = 'file', $core = false) { |
|
160 | /** |
||
161 | * @param string $what |
||
162 | * @param string|string[] $add |
||
163 | * @param string $mode |
||
164 | * @param bool $core |
||
165 | * |
||
166 | * @return \cs\Page |
||
167 | */ |
||
168 | 4 | protected function include_common ($what, $add, $mode, $core) { |
|
189 | /** |
||
190 | * Add config on page to make it available on frontend |
||
191 | * |
||
192 | * @param mixed $config_structure Any scalar type or array |
||
193 | * @param string $target Target is property of `window` object where config will be inserted as value, nested properties like `cs.sub.prop` |
||
194 | * are supported and all nested properties are created on demand. It is recommended to use sub-properties of `cs` |
||
195 | * |
||
196 | * @return \cs\Page |
||
197 | */ |
||
198 | 4 | public function config ($config_structure, $target) { |
|
201 | /** |
||
202 | * @param mixed $config_structure |
||
203 | * @param string $target |
||
204 | * @param bool $core |
||
205 | * |
||
206 | * @return \cs\Page |
||
207 | */ |
||
208 | 4 | protected function config_internal ($config_structure, $target, $core = false) { |
|
224 | /** |
||
225 | * Getting of HTML, JS and CSS includes |
||
226 | * |
||
227 | * @return \cs\Page |
||
228 | */ |
||
229 | 4 | protected function add_includes_on_page () { |
|
269 | /** |
||
270 | * @param Config $Config |
||
271 | * @param Request $Request |
||
272 | * |
||
273 | * @return bool |
||
274 | */ |
||
275 | 4 | protected function page_compression_usage ($Config, $Request) { |
|
278 | /** |
||
279 | * Add JS polyfills for IE/Edge |
||
280 | */ |
||
281 | 4 | protected function ie_edge () { |
|
291 | /** |
||
292 | * Hack: Add WebComponents Polyfill for browsers without native Shadow DOM support |
||
293 | * |
||
294 | * @param Request $Request |
||
295 | * @param Config $Config |
||
296 | * @param bool $with_compression |
||
297 | */ |
||
298 | 4 | protected function webcomponents_polyfill ($Request, $Config, $with_compression) { |
|
309 | /** |
||
310 | * @param Config $Config |
||
311 | * @param string $content |
||
312 | */ |
||
313 | 4 | protected function add_script_imports_to_document ($Config, $content) { |
|
320 | /** |
||
321 | * @param Request $Request |
||
322 | * |
||
323 | * @return array[] |
||
324 | */ |
||
325 | 4 | protected function get_includes_and_preload_resource_for_page_with_compression ($Request) { |
|
337 | /** |
||
338 | * @param array $dependencies |
||
339 | * @param string[][] $includes_map |
||
340 | * @param Request $Request |
||
341 | * |
||
342 | * @return string[][] |
||
343 | */ |
||
344 | 4 | protected function get_normalized_includes ($dependencies, $includes_map, $Request) { |
|
384 | /** |
||
385 | * @param array $dependencies |
||
386 | * @param string $url |
||
387 | * @param Request $Request |
||
388 | * |
||
389 | * @return false|string |
||
390 | */ |
||
391 | 4 | protected function get_dependency_component ($dependencies, $url, $Request) { |
|
403 | /** |
||
404 | * @param Config $Config |
||
405 | * @param Request $Request |
||
406 | * |
||
407 | * @return string[][] |
||
408 | */ |
||
409 | 2 | protected function get_includes_for_page_without_compression ($Config, $Request) { |
|
415 | /** |
||
416 | * @param string[]|string[][] $path |
||
417 | * |
||
418 | * @return string[]|string[][] |
||
419 | */ |
||
420 | 4 | protected function absolute_path_to_relative ($path) { |
|
423 | /** |
||
424 | * @param string[][] $includes |
||
425 | * |
||
426 | * @return string[][] |
||
427 | */ |
||
428 | 2 | protected function add_versions_hash ($includes) { |
|
444 | /** |
||
445 | * @param Config $Config |
||
446 | * @param Request $Request |
||
447 | * @param string[] $preload |
||
448 | */ |
||
449 | 4 | protected function add_includes_on_page_manually_added ($Config, $Request, $preload) { |
|
465 | /** |
||
466 | * @param Config $Config |
||
467 | * @param Request $Request |
||
468 | * @param string[] $preload |
||
469 | */ |
||
470 | 2 | protected function add_includes_on_page_manually_added_normal ($Config, $Request, $preload) { |
|
492 | /** |
||
493 | * @param string[] $preload |
||
494 | * @param Request $Request |
||
495 | */ |
||
496 | 4 | protected function add_preload ($preload, $Request) { |
|
497 | 4 | if ($Request->cookie('pushed')) { |
|
498 | return; |
||
499 | } |
||
500 | 4 | $Response = Response::instance(); |
|
501 | 4 | $Response->cookie('pushed', 1, 0, true); |
|
502 | 4 | foreach ($preload as $resource) { |
|
503 | 4 | $extension = explode('?', file_extension($resource))[0]; |
|
504 | 4 | $as = $this->extension_to_as[$extension]; |
|
505 | 4 | $resource = str_replace(' ', '%20', $resource); |
|
506 | 4 | $Response->header('Link', "<$resource>; rel=preload; as=$as", false); |
|
507 | } |
||
508 | 4 | } |
|
509 | /** |
||
510 | * @param Config $Config |
||
511 | * @param Request $Request |
||
512 | */ |
||
513 | 4 | protected function add_includes_on_page_manually_added_frontend_load_optimization ($Config, $Request) { |
|
549 | } |
||
550 |