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 |
||
33 | trait Includes { |
||
34 | use |
||
35 | Cache, |
||
36 | Collecting, |
||
37 | RequireJS; |
||
38 | protected $extension_to_as = [ |
||
39 | 'jpeg' => 'image', |
||
40 | 'jpe' => 'image', |
||
41 | 'jpg' => 'image', |
||
42 | 'gif' => 'image', |
||
43 | 'png' => 'image', |
||
44 | 'svg' => 'image', |
||
45 | 'svgz' => 'image', |
||
46 | 'ttf' => 'font', |
||
47 | 'ttc' => 'font', |
||
48 | 'otf' => 'font', |
||
49 | 'woff' => 'font', |
||
50 | 'woff2' => 'font', |
||
51 | 'eot' => 'font', |
||
52 | 'css' => 'style', |
||
53 | 'js' => 'script', |
||
54 | 'html' => 'document' |
||
55 | ]; |
||
56 | /** |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $core_html; |
||
60 | /** |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $core_js; |
||
64 | /** |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $core_css; |
||
68 | /** |
||
69 | * @var string |
||
70 | */ |
||
71 | protected $core_config; |
||
72 | /** |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $html; |
||
76 | /** |
||
77 | * @var array |
||
78 | */ |
||
79 | protected $js; |
||
80 | /** |
||
81 | * @var array |
||
82 | */ |
||
83 | protected $css; |
||
84 | /** |
||
85 | * @var string |
||
86 | */ |
||
87 | protected $config; |
||
88 | /** |
||
89 | * Base name is used as prefix when creating CSS/JS/HTML cache files in order to avoid collisions when having several themes and languages |
||
90 | * @var string |
||
91 | */ |
||
92 | protected $pcache_basename_path; |
||
93 | protected function init_includes () { |
||
94 | $this->core_html = ['path' => [], 'plain' => '']; |
||
95 | $this->core_js = ['path' => [], 'plain' => '']; |
||
96 | $this->core_css = ['path' => [], 'plain' => '']; |
||
97 | $this->core_config = ''; |
||
98 | $this->html = ['path' => [], 'plain' => '']; |
||
99 | $this->js = ['path' => [], 'plain' => '']; |
||
100 | $this->css = ['path' => [], 'plain' => '']; |
||
101 | $this->config = ''; |
||
102 | $this->pcache_basename_path = ''; |
||
103 | } |
||
104 | /** |
||
105 | * Including of Web Components |
||
106 | * |
||
107 | * @param string|string[] $add Path to including file, or code |
||
108 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
109 | * |
||
110 | * @return \cs\Page |
||
111 | */ |
||
112 | function html ($add, $mode = 'file') { |
||
113 | return $this->html_internal($add, $mode); |
||
114 | } |
||
115 | /** |
||
116 | * @param string|string[] $add |
||
117 | * @param string $mode |
||
118 | * @param bool $core |
||
119 | * |
||
120 | * @return \cs\Page |
||
121 | */ |
||
122 | protected function html_internal ($add, $mode = 'file', $core = false) { |
||
123 | return $this->include_common('html', $add, $mode, $core); |
||
124 | } |
||
125 | /** |
||
126 | * Including of JavaScript |
||
127 | * |
||
128 | * @param string|string[] $add Path to including file, or code |
||
129 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
130 | * |
||
131 | * @return \cs\Page |
||
132 | */ |
||
133 | function js ($add, $mode = 'file') { |
||
134 | return $this->js_internal($add, $mode); |
||
135 | } |
||
136 | /** |
||
137 | * @param string|string[] $add |
||
138 | * @param string $mode |
||
139 | * @param bool $core |
||
140 | * |
||
141 | * @return \cs\Page |
||
142 | */ |
||
143 | protected function js_internal ($add, $mode = 'file', $core = false) { |
||
144 | return $this->include_common('js', $add, $mode, $core); |
||
145 | } |
||
146 | /** |
||
147 | * Including of CSS |
||
148 | * |
||
149 | * @param string|string[] $add Path to including file, or code |
||
150 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
151 | * |
||
152 | * @return \cs\Page |
||
153 | */ |
||
154 | function css ($add, $mode = 'file') { |
||
155 | return $this->css_internal($add, $mode); |
||
156 | } |
||
157 | /** |
||
158 | * @param string|string[] $add |
||
159 | * @param string $mode |
||
160 | * @param bool $core |
||
161 | * |
||
162 | * @return \cs\Page |
||
163 | */ |
||
164 | protected function css_internal ($add, $mode = 'file', $core = false) { |
||
165 | return $this->include_common('css', $add, $mode, $core); |
||
166 | } |
||
167 | /** |
||
168 | * @param string $what |
||
169 | * @param string|string[] $add |
||
170 | * @param string $mode |
||
171 | * @param bool $core |
||
172 | * |
||
173 | * @return \cs\Page |
||
174 | */ |
||
175 | protected function include_common ($what, $add, $mode, $core) { |
||
176 | if (!$add) { |
||
177 | return $this; |
||
178 | } |
||
179 | if (is_array($add)) { |
||
180 | foreach (array_filter($add) as $style) { |
||
181 | $this->include_common($what, $style, $mode, $core); |
||
182 | } |
||
183 | } else { |
||
184 | if ($core) { |
||
185 | $what = "core_$what"; |
||
186 | } |
||
187 | $target = &$this->$what; |
||
188 | if ($mode == 'file') { |
||
189 | $target['path'][] = $add; |
||
190 | } elseif ($mode == 'code') { |
||
191 | $target['plain'] .= "$add\n"; |
||
192 | } |
||
193 | } |
||
194 | return $this; |
||
195 | } |
||
196 | /** |
||
197 | * Add config on page to make it available on frontend |
||
198 | * |
||
199 | * @param mixed $config_structure Any scalar type or array |
||
200 | * @param string $target Target is property of `window` object where config will be inserted as value, nested properties like `cs.sub.prop` |
||
201 | * are supported and all nested properties are created on demand. It is recommended to use sub-properties of `cs` |
||
202 | * |
||
203 | * @return \cs\Page |
||
204 | */ |
||
205 | function config ($config_structure, $target) { |
||
208 | /** |
||
209 | * @param mixed $config_structure |
||
210 | * @param string $target |
||
211 | * @param bool $core |
||
212 | * |
||
213 | * @return \cs\Page |
||
214 | */ |
||
215 | protected function config_internal ($config_structure, $target, $core = false) { |
||
231 | /** |
||
232 | * Getting of HTML, JS and CSS includes |
||
233 | * |
||
234 | * @return \cs\Page |
||
235 | */ |
||
236 | protected function add_includes_on_page () { |
||
237 | $Config = Config::instance(true); |
||
238 | if (!$Config) { |
||
239 | return $this; |
||
240 | } |
||
241 | /** |
||
242 | * Base name for cache files |
||
243 | */ |
||
244 | $this->pcache_basename_path = PUBLIC_CACHE.'/'.$this->theme.'_'.Language::instance()->clang; |
||
245 | /** |
||
246 | * Some JS configs required by system |
||
247 | */ |
||
248 | $this->add_system_configs(); |
||
249 | // TODO: I hope some day we'll get rid of this sh*t :( |
||
250 | $this->ie_edge(); |
||
251 | $Request = Request::instance(); |
||
252 | /** |
||
253 | * If CSS and JavaScript compression enabled |
||
254 | */ |
||
255 | if ($Config->core['cache_compress_js_css'] && !($Request->admin_path && isset($Request->query['debug']))) { |
||
256 | $this->webcomponents_polyfill($Request, true); |
||
257 | list($includes, $preload) = $this->get_includes_and_preload_resource_for_page_with_compression($Config, $Request); |
||
258 | $this->add_preload($preload); |
||
259 | } else { |
||
260 | $this->webcomponents_polyfill($Request, false); |
||
261 | /** |
||
262 | * Language translation is added explicitly only when compression is disabled, otherwise it will be in compressed JS file |
||
263 | */ |
||
264 | $this->config_internal(Language::instance(), 'cs.Language', true); |
||
265 | $this->config_internal($this->get_requirejs_paths(), 'requirejs.paths', true); |
||
266 | $includes = $this->get_includes_for_page_without_compression($Config, $Request); |
||
267 | } |
||
268 | $this->css_internal($includes['css'], 'file', true); |
||
269 | $this->js_internal($includes['js'], 'file', true); |
||
270 | $this->html_internal($includes['html'], 'file', true); |
||
271 | $this->add_includes_on_page_manually_added($Config); |
||
272 | return $this; |
||
273 | } |
||
274 | /** |
||
275 | * Add JS polyfills for IE/Edge |
||
276 | */ |
||
277 | protected function ie_edge () { |
||
316 | /** |
||
317 | * Hack: Add WebComponents Polyfill for browsers without native Shadow DOM support |
||
318 | * |
||
319 | * @param Request $Request |
||
320 | * @param bool $with_compression |
||
321 | */ |
||
322 | protected function webcomponents_polyfill ($Request, $with_compression) { |
||
323 | if ($Request->cookie('shadow_dom') == 1) { |
||
324 | return; |
||
325 | } |
||
326 | $file = 'includes/js/WebComponents-polyfill/webcomponents-custom.min.js'; |
||
327 | if ($with_compression) { |
||
328 | $compressed_file = PUBLIC_CACHE.'/webcomponents.js'; |
||
329 | if (!file_exists($compressed_file)) { |
||
330 | $content = file_get_contents(DIR."/$file"); |
||
331 | file_put_contents($compressed_file, gzencode($content, 9), LOCK_EX | FILE_BINARY); |
||
332 | file_put_contents("$compressed_file.hash", substr(md5($content), 0, 5)); |
||
333 | } |
||
334 | $hash = file_get_contents("$compressed_file.hash"); |
||
335 | $this->js_internal("storage/pcache/webcomponents.js?$hash", 'file', true); |
||
336 | } else { |
||
337 | $this->js_internal($file, 'file', true); |
||
338 | } |
||
339 | } |
||
340 | /** |
||
341 | * @param string[] $preload |
||
342 | */ |
||
343 | protected function add_preload ($preload) { |
||
352 | /** |
||
353 | * @param Config $Config |
||
354 | * @param Request $Request |
||
355 | * |
||
356 | * @return array[] |
||
357 | */ |
||
358 | protected function get_includes_and_preload_resource_for_page_with_compression ($Config, $Request) { |
||
391 | /** |
||
392 | * @param array $dependencies |
||
393 | * @param string[][] $includes_map |
||
394 | * @param string $separator `+` or `/` |
||
395 | * @param Request $Request |
||
396 | * |
||
397 | * @return string[][] |
||
398 | */ |
||
399 | protected function get_normalized_includes ($dependencies, $includes_map, $separator, $Request) { |
||
438 | /** |
||
439 | * @param array $dependencies |
||
440 | * @param string $url |
||
441 | * @param string $separator `+` or `/` |
||
442 | * @param Request $Request |
||
443 | * |
||
444 | * @return false|string |
||
445 | */ |
||
446 | protected function get_dependency_component ($dependencies, $url, $separator, $Request) { |
||
458 | /** |
||
459 | * @param Config $Config |
||
460 | * @param Request $Request |
||
461 | * |
||
462 | * @return string[][] |
||
463 | */ |
||
464 | protected function get_includes_for_page_without_compression ($Config, $Request) { |
||
470 | /** |
||
471 | * @param string[]|string[][] $path |
||
472 | * |
||
473 | * @return string[]|string[][] |
||
474 | */ |
||
475 | protected function absolute_path_to_relative ($path) { |
||
478 | /** |
||
479 | * @param string[][] $includes |
||
480 | * |
||
481 | * @return string[][] |
||
482 | */ |
||
483 | protected function add_versions_hash ($includes) { |
||
499 | /** |
||
500 | * @param Config $Config |
||
501 | */ |
||
502 | protected function add_includes_on_page_manually_added ($Config) { |
||
537 | } |
||
538 |