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 |
||
32 | trait Includes { |
||
33 | use |
||
34 | Cache, |
||
35 | Collecting, |
||
36 | RequireJS; |
||
37 | protected $extension_to_as = [ |
||
38 | 'jpeg' => 'image', |
||
39 | 'jpe' => 'image', |
||
40 | 'jpg' => 'image', |
||
41 | 'gif' => 'image', |
||
42 | 'png' => 'image', |
||
43 | 'svg' => 'image', |
||
44 | 'svgz' => 'image', |
||
45 | 'woff' => 'font', |
||
46 | //'woff2' => 'font', |
||
47 | 'css' => 'style', |
||
48 | 'js' => 'script', |
||
49 | 'html' => 'document' |
||
50 | ]; |
||
51 | /** |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $core_html; |
||
55 | /** |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $core_js; |
||
59 | /** |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $core_css; |
||
63 | /** |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $core_config; |
||
67 | /** |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $html; |
||
71 | /** |
||
72 | * @var array |
||
73 | */ |
||
74 | protected $js; |
||
75 | /** |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $css; |
||
79 | /** |
||
80 | * @var string |
||
81 | */ |
||
82 | protected $config; |
||
83 | /** |
||
84 | * Base name is used as prefix when creating CSS/JS/HTML cache files in order to avoid collisions when having several themes and languages |
||
85 | * @var string |
||
86 | */ |
||
87 | protected $pcache_basename_path; |
||
88 | protected function init_includes () { |
||
99 | /** |
||
100 | * Including of Web Components |
||
101 | * |
||
102 | * @param string|string[] $add Path to including file, or code |
||
103 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
104 | * |
||
105 | * @return \cs\Page |
||
106 | */ |
||
107 | function html ($add, $mode = 'file') { |
||
110 | /** |
||
111 | * @param string|string[] $add |
||
112 | * @param string $mode |
||
113 | * @param bool $core |
||
114 | * |
||
115 | * @return \cs\Page |
||
116 | */ |
||
117 | protected function html_internal ($add, $mode = 'file', $core = false) { |
||
120 | /** |
||
121 | * Including of JavaScript |
||
122 | * |
||
123 | * @param string|string[] $add Path to including file, or code |
||
124 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
125 | * |
||
126 | * @return \cs\Page |
||
127 | */ |
||
128 | function js ($add, $mode = 'file') { |
||
131 | /** |
||
132 | * @param string|string[] $add |
||
133 | * @param string $mode |
||
134 | * @param bool $core |
||
135 | * |
||
136 | * @return \cs\Page |
||
137 | */ |
||
138 | protected function js_internal ($add, $mode = 'file', $core = false) { |
||
141 | /** |
||
142 | * Including of CSS |
||
143 | * |
||
144 | * @param string|string[] $add Path to including file, or code |
||
145 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
146 | * |
||
147 | * @return \cs\Page |
||
148 | */ |
||
149 | function css ($add, $mode = 'file') { |
||
152 | /** |
||
153 | * @param string|string[] $add |
||
154 | * @param string $mode |
||
155 | * @param bool $core |
||
156 | * |
||
157 | * @return \cs\Page |
||
158 | */ |
||
159 | protected function css_internal ($add, $mode = 'file', $core = false) { |
||
162 | /** |
||
163 | * @param string $what |
||
164 | * @param string|string[] $add |
||
165 | * @param string $mode |
||
166 | * @param bool $core |
||
167 | * |
||
168 | * @return \cs\Page |
||
169 | */ |
||
170 | protected function include_common ($what, $add, $mode, $core) { |
||
191 | /** |
||
192 | * Add config on page to make it available on frontend |
||
193 | * |
||
194 | * @param mixed $config_structure Any scalar type or array |
||
195 | * @param string $target Target is property of `window` object where config will be inserted as value, nested properties like `cs.sub.prop` |
||
196 | * are supported and all nested properties are created on demand. It is recommended to use sub-properties of `cs` |
||
197 | * |
||
198 | * @return \cs\Page |
||
199 | */ |
||
200 | function config ($config_structure, $target) { |
||
203 | /** |
||
204 | * @param mixed $config_structure |
||
205 | * @param string $target |
||
206 | * @param bool $core |
||
207 | * |
||
208 | * @return \cs\Page |
||
209 | */ |
||
210 | protected function config_internal ($config_structure, $target, $core = false) { |
||
226 | /** |
||
227 | * Getting of HTML, JS and CSS includes |
||
228 | * |
||
229 | * @return \cs\Page |
||
230 | */ |
||
231 | protected function add_includes_on_page () { |
||
269 | /** |
||
270 | * @param Config $Config |
||
271 | * @param Request $Request |
||
272 | * |
||
273 | * @return bool |
||
274 | */ |
||
275 | protected function page_compression_usage ($Config, $Request) { |
||
278 | /** |
||
279 | * Add JS polyfills for IE/Edge |
||
280 | */ |
||
281 | protected function ie_edge () { |
||
320 | /** |
||
321 | * Hack: Add WebComponents Polyfill for browsers without native Shadow DOM support |
||
322 | * |
||
323 | * @param Request $Request |
||
324 | * @param bool $with_compression |
||
325 | */ |
||
326 | protected function webcomponents_polyfill ($Request, $with_compression) { |
||
344 | /** |
||
345 | * @param Config $Config |
||
346 | * @param Request $Request |
||
347 | * |
||
348 | * @return array[] |
||
349 | */ |
||
350 | protected function get_includes_and_preload_resource_for_page_with_compression ($Config, $Request) { |
||
366 | /** |
||
367 | * @param array $dependencies |
||
368 | * @param string[][] $includes_map |
||
369 | * @param Request $Request |
||
370 | * |
||
371 | * @return string[][] |
||
372 | */ |
||
373 | protected function get_normalized_includes ($dependencies, $includes_map, $Request) { |
||
412 | /** |
||
413 | * @param array $dependencies |
||
414 | * @param string $url |
||
415 | * @param Request $Request |
||
416 | * |
||
417 | * @return false|string |
||
418 | */ |
||
419 | protected function get_dependency_component ($dependencies, $url, $Request) { |
||
431 | /** |
||
432 | * @param Config $Config |
||
433 | * @param Request $Request |
||
434 | * |
||
435 | * @return string[][] |
||
436 | */ |
||
437 | protected function get_includes_for_page_without_compression ($Config, $Request) { |
||
443 | /** |
||
444 | * @param string[]|string[][] $path |
||
445 | * |
||
446 | * @return string[]|string[][] |
||
447 | */ |
||
448 | protected function absolute_path_to_relative ($path) { |
||
451 | /** |
||
452 | * @param string[][] $includes |
||
453 | * |
||
454 | * @return string[][] |
||
455 | */ |
||
456 | protected function add_versions_hash ($includes) { |
||
472 | /** |
||
473 | * @param Config $Config |
||
474 | * @param Request $Request |
||
475 | * @param string[] $preload |
||
476 | */ |
||
477 | protected function add_includes_on_page_manually_added ($Config, $Request, $preload) { |
||
493 | /** |
||
494 | * @param Config $Config |
||
495 | * @param string[] $preload |
||
496 | */ |
||
497 | protected function add_includes_on_page_manually_added_normal ($Config, $preload) { |
||
526 | /** |
||
527 | * @param string[] $preload |
||
528 | */ |
||
529 | protected function add_preload ($preload) { |
||
538 | /** |
||
539 | * @param Config $Config |
||
540 | */ |
||
541 | protected function add_includes_on_page_manually_added_frontend_load_optimization ($Config) { |
||
580 | } |
||
581 |