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 |
||
31 | trait Includes { |
||
32 | use |
||
33 | Cache, |
||
34 | Collecting, |
||
35 | RequireJS; |
||
36 | protected $extension_to_as = [ |
||
37 | 'jpeg' => 'image', |
||
38 | 'jpe' => 'image', |
||
39 | 'jpg' => 'image', |
||
40 | 'gif' => 'image', |
||
41 | 'png' => 'image', |
||
42 | 'svg' => 'image', |
||
43 | 'svgz' => 'image', |
||
44 | 'woff' => 'font', |
||
45 | //'woff2' => 'font', |
||
46 | 'css' => 'style', |
||
47 | 'js' => 'script', |
||
48 | 'html' => 'document' |
||
49 | ]; |
||
50 | /** |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $core_html; |
||
54 | /** |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $core_js; |
||
58 | /** |
||
59 | * @var array |
||
60 | */ |
||
61 | protected $core_css; |
||
62 | /** |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $core_config; |
||
66 | /** |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $html; |
||
70 | /** |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $js; |
||
74 | /** |
||
75 | * @var array |
||
76 | */ |
||
77 | protected $css; |
||
78 | /** |
||
79 | * @var string |
||
80 | */ |
||
81 | protected $config; |
||
82 | /** |
||
83 | * Base name is used as prefix when creating CSS/JS/HTML cache files in order to avoid collisions when having several themes and languages |
||
84 | * @var string |
||
85 | */ |
||
86 | protected $pcache_basename_path; |
||
87 | protected function init_includes () { |
||
98 | /** |
||
99 | * Including of Web Components |
||
100 | * |
||
101 | * @param string|string[] $add Path to including file, or code |
||
102 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
103 | * |
||
104 | * @return \cs\Page |
||
105 | */ |
||
106 | function html ($add, $mode = 'file') { |
||
109 | /** |
||
110 | * @param string|string[] $add |
||
111 | * @param string $mode |
||
112 | * @param bool $core |
||
113 | * |
||
114 | * @return \cs\Page |
||
115 | */ |
||
116 | protected function html_internal ($add, $mode = 'file', $core = false) { |
||
119 | /** |
||
120 | * Including of JavaScript |
||
121 | * |
||
122 | * @param string|string[] $add Path to including file, or code |
||
123 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
124 | * |
||
125 | * @return \cs\Page |
||
126 | */ |
||
127 | function js ($add, $mode = 'file') { |
||
130 | /** |
||
131 | * @param string|string[] $add |
||
132 | * @param string $mode |
||
133 | * @param bool $core |
||
134 | * |
||
135 | * @return \cs\Page |
||
136 | */ |
||
137 | protected function js_internal ($add, $mode = 'file', $core = false) { |
||
140 | /** |
||
141 | * Including of CSS |
||
142 | * |
||
143 | * @param string|string[] $add Path to including file, or code |
||
144 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
145 | * |
||
146 | * @return \cs\Page |
||
147 | */ |
||
148 | function css ($add, $mode = 'file') { |
||
151 | /** |
||
152 | * @param string|string[] $add |
||
153 | * @param string $mode |
||
154 | * @param bool $core |
||
155 | * |
||
156 | * @return \cs\Page |
||
157 | */ |
||
158 | protected function css_internal ($add, $mode = 'file', $core = false) { |
||
161 | /** |
||
162 | * @param string $what |
||
163 | * @param string|string[] $add |
||
164 | * @param string $mode |
||
165 | * @param bool $core |
||
166 | * |
||
167 | * @return \cs\Page |
||
168 | */ |
||
169 | protected function include_common ($what, $add, $mode, $core) { |
||
190 | /** |
||
191 | * Add config on page to make it available on frontend |
||
192 | * |
||
193 | * @param mixed $config_structure Any scalar type or array |
||
194 | * @param string $target Target is property of `window` object where config will be inserted as value, nested properties like `cs.sub.prop` |
||
195 | * are supported and all nested properties are created on demand. It is recommended to use sub-properties of `cs` |
||
196 | * |
||
197 | * @return \cs\Page |
||
198 | */ |
||
199 | function config ($config_structure, $target) { |
||
202 | /** |
||
203 | * @param mixed $config_structure |
||
204 | * @param string $target |
||
205 | * @param bool $core |
||
206 | * |
||
207 | * @return \cs\Page |
||
208 | */ |
||
209 | protected function config_internal ($config_structure, $target, $core = false) { |
||
225 | /** |
||
226 | * Getting of HTML, JS and CSS includes |
||
227 | * |
||
228 | * @return \cs\Page |
||
229 | */ |
||
230 | protected function add_includes_on_page () { |
||
268 | /** |
||
269 | * @param Config $Config |
||
270 | * @param Request $Request |
||
271 | * |
||
272 | * @return bool |
||
273 | */ |
||
274 | protected function page_compression_usage ($Config, $Request) { |
||
277 | /** |
||
278 | * Add JS polyfills for IE/Edge |
||
279 | */ |
||
280 | protected function ie_edge () { |
||
290 | /** |
||
291 | * Hack: Add WebComponents Polyfill for browsers without native Shadow DOM support |
||
292 | * |
||
293 | * @param Request $Request |
||
294 | * @param bool $with_compression |
||
295 | */ |
||
296 | protected function webcomponents_polyfill ($Request, $with_compression) { |
||
307 | /** |
||
308 | * @param Request $Request |
||
309 | * |
||
310 | * @return array[] |
||
311 | */ |
||
312 | protected function get_includes_and_preload_resource_for_page_with_compression ($Request) { |
||
324 | /** |
||
325 | * @param array $dependencies |
||
326 | * @param string[][] $includes_map |
||
327 | * @param Request $Request |
||
328 | * |
||
329 | * @return string[][] |
||
330 | */ |
||
331 | protected function get_normalized_includes ($dependencies, $includes_map, $Request) { |
||
371 | /** |
||
372 | * @param array $dependencies |
||
373 | * @param string $url |
||
374 | * @param Request $Request |
||
375 | * |
||
376 | * @return false|string |
||
377 | */ |
||
378 | protected function get_dependency_component ($dependencies, $url, $Request) { |
||
390 | /** |
||
391 | * @param Config $Config |
||
392 | * @param Request $Request |
||
393 | * |
||
394 | * @return string[][] |
||
395 | */ |
||
396 | protected function get_includes_for_page_without_compression ($Config, $Request) { |
||
402 | /** |
||
403 | * @param string[]|string[][] $path |
||
404 | * |
||
405 | * @return string[]|string[][] |
||
406 | */ |
||
407 | protected function absolute_path_to_relative ($path) { |
||
410 | /** |
||
411 | * @param string[][] $includes |
||
412 | * |
||
413 | * @return string[][] |
||
414 | */ |
||
415 | protected function add_versions_hash ($includes) { |
||
431 | /** |
||
432 | * @param Config $Config |
||
433 | * @param Request $Request |
||
434 | * @param string[] $preload |
||
435 | */ |
||
436 | protected function add_includes_on_page_manually_added ($Config, $Request, $preload) { |
||
452 | /** |
||
453 | * @param Config $Config |
||
454 | * @param Request $Request |
||
455 | * @param string[] $preload |
||
456 | */ |
||
457 | protected function add_includes_on_page_manually_added_normal ($Config, $Request, $preload) { |
||
485 | /** |
||
486 | * Hack: jQuery is kind of special; it is only loaded directly in normal mode, during frontend load optimization it is loaded asynchronously in frontend |
||
487 | * TODO: In future we'll load jQuery as AMD module only and this thing will not be needed |
||
488 | * |
||
489 | * @param bool $with_compression |
||
490 | * |
||
491 | * @return string |
||
492 | */ |
||
493 | protected function jquery ($with_compression) { |
||
501 | /** |
||
502 | * @param string[] $preload |
||
503 | */ |
||
504 | protected function add_preload ($preload) { |
||
513 | /** |
||
514 | * @param Config $Config |
||
515 | */ |
||
516 | protected function add_includes_on_page_manually_added_frontend_load_optimization ($Config) { |
||
555 | } |
||
556 |