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 |
||
47 | trait Includes { |
||
48 | /** |
||
49 | * @var array[] |
||
50 | */ |
||
51 | protected $core_html; |
||
52 | /** |
||
53 | * @var array[] |
||
54 | */ |
||
55 | protected $core_js; |
||
56 | /** |
||
57 | * @var array[] |
||
58 | */ |
||
59 | protected $core_css; |
||
60 | /** |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $core_config; |
||
64 | /** |
||
65 | * @var array[] |
||
66 | */ |
||
67 | protected $html; |
||
68 | /** |
||
69 | * @var array[] |
||
70 | */ |
||
71 | protected $js; |
||
72 | /** |
||
73 | * @var array[] |
||
74 | */ |
||
75 | protected $css; |
||
76 | /** |
||
77 | * @var string |
||
78 | */ |
||
79 | protected $config; |
||
80 | /** |
||
81 | * Base name is used as prefix when creating CSS/JS/HTML cache files in order to avoid collisions when having several themes and languages |
||
82 | * @var string |
||
83 | */ |
||
84 | protected $pcache_basename; |
||
85 | protected function init_includes () { |
||
96 | /** |
||
97 | * Including of Web Components |
||
98 | * |
||
99 | * @param string|string[] $add Path to including file, or code |
||
100 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
101 | * |
||
102 | * @return \cs\Page |
||
103 | */ |
||
104 | function html ($add, $mode = 'file') { |
||
107 | /** |
||
108 | * @param string|string[] $add |
||
109 | * @param string $mode |
||
110 | * @param bool $core |
||
111 | * |
||
112 | * @return \cs\Page |
||
113 | */ |
||
114 | protected function html_internal ($add, $mode = 'file', $core = false) { |
||
141 | /** |
||
142 | * Including of JavaScript |
||
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 js ($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 js_internal ($add, $mode = 'file', $core = false) { |
||
185 | /** |
||
186 | * Including of CSS |
||
187 | * |
||
188 | * @param string|string[] $add Path to including file, or code |
||
189 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
190 | * |
||
191 | * @return \cs\Page |
||
192 | */ |
||
193 | function css ($add, $mode = 'file') { |
||
196 | /** |
||
197 | * @param string|string[] $add |
||
198 | * @param string $mode |
||
199 | * @param bool $core |
||
200 | * |
||
201 | * @return \cs\Page |
||
202 | */ |
||
203 | protected function css_internal ($add, $mode = 'file', $core = false) { |
||
231 | /** |
||
232 | * Add config on page to make it available on frontend |
||
233 | * |
||
234 | * @param mixed $config_structure Any scalar type or array |
||
235 | * @param string $target Target is property of `window` object where config will be inserted as value, nested properties like `cs.sub.prop` |
||
236 | * are supported and all nested properties are created on demand. It is recommended to use sub-properties of `cs` |
||
237 | * |
||
238 | * @return \cs\Page |
||
239 | */ |
||
240 | function config ($config_structure, $target) { |
||
243 | /** |
||
244 | * @param mixed $config_structure |
||
245 | * @param string $target |
||
246 | * @param bool $core |
||
247 | * |
||
248 | * @return \cs\Page |
||
249 | */ |
||
250 | protected function config_internal ($config_structure, $target, $core = false) { |
||
266 | /** |
||
267 | * Getting of HTML, JS and CSS includes |
||
268 | * |
||
269 | * @return \cs\Page |
||
270 | */ |
||
271 | protected function add_includes_on_page () { |
||
272 | $Config = Config::instance(true); |
||
273 | if (!$Config) { |
||
274 | return $this; |
||
275 | } |
||
276 | /** |
||
277 | * Base name for cache files |
||
278 | */ |
||
279 | $this->pcache_basename = "_{$this->theme}_".Language::instance()->clang; |
||
280 | /** |
||
281 | * Some JS configs required by system |
||
282 | */ |
||
283 | $this->add_system_configs(); |
||
284 | // TODO: I hope some day we'll get rid of this sh*t :( |
||
285 | $this->ie_edge(); |
||
286 | $Request = Request::instance(); |
||
287 | /** |
||
288 | * If CSS and JavaScript compression enabled |
||
289 | */ |
||
290 | if ($Config->core['cache_compress_js_css'] && !($Request->admin_path && isset($Request->query['debug']))) { |
||
291 | $this->webcomponents_polyfill($Request, true); |
||
292 | $includes = $this->get_includes_for_page_with_compression(); |
||
293 | } else { |
||
294 | $this->webcomponents_polyfill($Request, false); |
||
295 | /** |
||
296 | * Language translation is added explicitly only when compression is disabled, otherwise it will be in compressed JS file |
||
297 | */ |
||
298 | /** |
||
299 | * @var \cs\Page $this |
||
300 | */ |
||
301 | $this->config_internal(Language::instance(), 'cs.Language', true); |
||
302 | $this->config_internal($this->get_requirejs_paths(), 'requirejs.paths', true); |
||
303 | $includes = $this->get_includes_for_page_without_compression($Config); |
||
304 | } |
||
305 | $this->css_internal($includes['css'], 'file', true); |
||
306 | $this->js_internal($includes['js'], 'file', true); |
||
307 | $this->html_internal($includes['html'], 'file', true); |
||
308 | $this->add_includes_on_page_manually_added($Config); |
||
309 | return $this; |
||
310 | } |
||
311 | /** |
||
312 | * @return string[] |
||
313 | */ |
||
314 | protected function get_requirejs_paths () { |
||
315 | $Config = Config::instance(); |
||
316 | $paths = []; |
||
317 | foreach ($Config->components['modules'] as $module_name => $module_data) { |
||
318 | if ($module_data['active'] == Config\Module_Properties::UNINSTALLED) { |
||
319 | continue; |
||
320 | } |
||
321 | $this->get_requirejs_paths_add_aliases(MODULES."/$module_name", $paths); |
||
322 | } |
||
323 | foreach ($Config->components['plugins'] as $plugin_name) { |
||
324 | $this->get_requirejs_paths_add_aliases(PLUGINS."/$plugin_name", $paths); |
||
325 | } |
||
326 | $directories_to_browse = [ |
||
327 | DIR.'/bower_components', |
||
328 | DIR.'/node_modules' |
||
329 | ]; |
||
330 | Event::instance()->fire( |
||
331 | 'System/Page/requirejs', |
||
332 | [ |
||
333 | 'paths' => &$paths, |
||
334 | 'directories_to_browse' => &$directories_to_browse |
||
335 | ] |
||
336 | ); |
||
337 | foreach ($directories_to_browse as $dir) { |
||
338 | foreach (get_files_list($dir, false, 'd', true) as $d) { |
||
339 | $this->get_requirejs_paths_find_package($d, $paths); |
||
340 | } |
||
341 | } |
||
342 | return $paths; |
||
343 | } |
||
344 | /** |
||
345 | * @param string $dir |
||
346 | * @param string[] $paths |
||
347 | */ |
||
348 | protected function get_requirejs_paths_add_aliases ($dir, &$paths) { |
||
349 | if (is_dir("$dir/includes/js")) { |
||
350 | $name = basename($dir); |
||
351 | $paths[$name] = $this->absolute_path_to_relative("$dir/includes/js"); |
||
352 | foreach ((array)@file_get_json("$dir/meta.json")['provide'] as $p) { |
||
353 | if (strpos($p, '/') !== false) { |
||
354 | $paths[$p] = $paths[$name]; |
||
355 | } |
||
356 | } |
||
357 | } |
||
358 | } |
||
359 | /** |
||
360 | * @param string $dir |
||
361 | * @param string[] $paths |
||
362 | */ |
||
363 | protected function get_requirejs_paths_find_package ($dir, &$paths) { |
||
364 | $path = $this->get_requirejs_paths_find_package_bower($dir) ?: $this->get_requirejs_paths_find_package_npm($dir); |
||
365 | if ($path) { |
||
366 | $paths[basename($dir)] = $this->absolute_path_to_relative(substr($path, 0, -3)); |
||
367 | } |
||
368 | } |
||
369 | /** |
||
370 | * @param string $dir |
||
371 | * |
||
372 | * @return string |
||
373 | */ |
||
374 | protected function get_requirejs_paths_find_package_bower ($dir) { |
||
375 | $bower = @file_get_json("$dir/bower.json"); |
||
376 | foreach (@(array)$bower['main'] as $main) { |
||
377 | if (preg_match('/\.js$/', $main)) { |
||
378 | $main = substr($main, 0, -3); |
||
379 | // There is a chance that minified file is present |
||
380 | $main = file_exists_with_extension("$dir/$main", ['min.js', 'js']); |
||
381 | if ($main) { |
||
382 | return $main; |
||
383 | } |
||
384 | } |
||
385 | } |
||
386 | return null; |
||
387 | } |
||
388 | /** |
||
389 | * @param string $dir |
||
390 | * |
||
391 | * @return false|string |
||
392 | */ |
||
393 | protected function get_requirejs_paths_find_package_npm ($dir) { |
||
394 | $package = @file_get_json("$dir/package.json"); |
||
395 | // If we have browser-specific declaration - use it |
||
396 | $main = @$package['browser'] ?: (@$package['jspm']['main'] ?: @$package['main']); |
||
397 | if (preg_match('/\.js$/', $main)) { |
||
398 | $main = substr($main, 0, -3); |
||
399 | } |
||
400 | if ($main) { |
||
401 | // There is a chance that minified file is present |
||
402 | return file_exists_with_extension("$dir/$main", ['min.js', 'js']) ?: file_exists_with_extension("$dir/dist/$main", ['min.js', 'js']); |
||
403 | } |
||
404 | } |
||
405 | /** |
||
406 | * Since modules, plugins and storage directories can be (at least theoretically) moved from default location - let's do proper path conversion |
||
407 | * |
||
408 | * @param string|string[] $path |
||
409 | * |
||
410 | * @return string|string[] |
||
411 | */ |
||
412 | protected function absolute_path_to_relative ($path) { |
||
413 | if (is_array($path)) { |
||
414 | foreach ($path as &$p) { |
||
415 | $p = $this->absolute_path_to_relative($p); |
||
416 | } |
||
417 | return $path; |
||
418 | } |
||
419 | if (strpos($path, MODULES) === 0) { |
||
420 | return 'components/modules'.substr($path, strlen(MODULES)); |
||
421 | } |
||
422 | if (strpos($path, PLUGINS) === 0) { |
||
423 | return 'components/plugins'.substr($path, strlen(PLUGINS)); |
||
424 | } |
||
425 | if (strpos($path, STORAGE) === 0) { |
||
426 | return 'storage'.substr($path, strlen(STORAGE)); |
||
427 | } |
||
428 | return substr($path, strlen(DIR) + 1); |
||
429 | } |
||
430 | /** |
||
431 | * Add JS polyfills for IE/Edge |
||
432 | */ |
||
433 | protected function ie_edge () { |
||
434 | if (preg_match('/Trident|Edge/', Request::instance()->header('user-agent'))) { |
||
435 | $this->js_internal( |
||
436 | get_files_list(DIR."/includes/js/microsoft_sh*t", "/.*\\.js$/i", 'f', "includes/js/microsoft_sh*t", true), |
||
437 | 'file', |
||
438 | true |
||
439 | ); |
||
440 | } |
||
441 | } |
||
442 | /** |
||
443 | * Hack: Add WebComponents Polyfill for browsers without native Shadow DOM support |
||
444 | * |
||
445 | * TODO: Probably, some effective User Agent-based check might be used here |
||
446 | * |
||
447 | * @param Request $Request |
||
448 | * @param bool $with_compression |
||
449 | */ |
||
450 | protected function webcomponents_polyfill ($Request, $with_compression) { |
||
467 | protected function add_system_configs () { |
||
468 | $Config = Config::instance(); |
||
469 | $Request = Request::instance(); |
||
470 | $User = User::instance(); |
||
471 | $current_module = $Request->current_module; |
||
472 | $this->config_internal( |
||
473 | [ |
||
474 | 'base_url' => $Config->base_url(), |
||
475 | 'current_base_url' => $Config->base_url().'/'.($Request->admin_path ? 'admin/' : '').$current_module, |
||
476 | 'public_key' => Core::instance()->public_key, |
||
477 | 'module' => $current_module, |
||
478 | 'in_admin' => (int)$Request->admin_path, |
||
479 | 'is_admin' => (int)$User->admin(), |
||
480 | 'is_user' => (int)$User->user(), |
||
481 | 'is_guest' => (int)$User->guest(), |
||
482 | 'password_min_length' => (int)$Config->core['password_min_length'], |
||
483 | 'password_min_strength' => (int)$Config->core['password_min_strength'], |
||
484 | 'debug' => (int)DEBUG, |
||
485 | 'route' => $Request->route, |
||
486 | 'route_path' => $Request->route_path, |
||
487 | 'route_ids' => $Request->route_ids |
||
488 | ], |
||
489 | 'cs', |
||
490 | true |
||
491 | ); |
||
492 | if ($User->admin()) { |
||
493 | $this->config_internal((int)$Config->core['simple_admin_mode'], 'cs.simple_admin_mode', true); |
||
494 | } |
||
495 | } |
||
496 | /** |
||
497 | * @return array[] |
||
498 | */ |
||
499 | protected function get_includes_for_page_with_compression () { |
||
530 | /** |
||
531 | * @param Config $Config |
||
532 | * |
||
533 | * @return array[] |
||
534 | */ |
||
535 | protected function get_includes_for_page_without_compression ($Config) { |
||
536 | // To determine all dependencies and stuff we need `$Config` object to be already created |
||
537 | if ($Config) { |
||
538 | list($dependencies, $includes_map) = $this->includes_dependencies_and_map(); |
||
539 | $system_includes = $includes_map['']; |
||
540 | list($includes, $dependencies_includes, $dependencies, $current_url) = $this->get_includes_prepare($dependencies, '/'); |
||
541 | foreach ($includes_map as $url => $local_includes) { |
||
542 | if (!$url) { |
||
543 | continue; |
||
544 | } |
||
545 | $is_dependency = $this->get_includes_is_dependency($dependencies, $url, '/'); |
||
546 | if ($is_dependency) { |
||
547 | $dependencies_includes = array_merge_recursive($dependencies_includes, $local_includes); |
||
548 | } elseif (mb_strpos($current_url, $url) === 0) { |
||
549 | $includes = array_merge_recursive($includes, $local_includes); |
||
550 | } |
||
551 | } |
||
552 | $includes = array_merge_recursive($system_includes, $dependencies_includes, $includes); |
||
553 | $includes = $this->absolute_path_to_relative($includes); |
||
554 | } else { |
||
555 | $includes = $this->get_includes_list(); |
||
556 | } |
||
557 | return $this->add_versions_hash($includes); |
||
558 | } |
||
559 | /** |
||
560 | * @param array $dependencies |
||
561 | * @param string $separator `+` or `/` |
||
562 | * |
||
563 | * @return array |
||
564 | */ |
||
565 | protected function get_includes_prepare ($dependencies, $separator) { |
||
566 | $Request = Request::instance(); |
||
567 | $includes = [ |
||
568 | 'css' => [], |
||
569 | 'js' => [], |
||
570 | 'html' => [] |
||
571 | ]; |
||
572 | $dependencies_includes = $includes; |
||
573 | $current_module = $Request->current_module; |
||
574 | /** |
||
575 | * Current URL based on controller path (it better represents how page was rendered) |
||
576 | */ |
||
577 | $current_url = array_slice(App::instance()->controller_path, 1); |
||
578 | $current_url = ($Request->admin_path ? "admin$separator" : '')."$current_module$separator".implode($separator, $current_url); |
||
579 | /** |
||
580 | * Narrow the dependencies to current module only |
||
581 | */ |
||
582 | $dependencies = array_merge( |
||
583 | isset($dependencies[$current_module]) ? $dependencies[$current_module] : [], |
||
584 | $dependencies['System'] |
||
585 | ); |
||
586 | return [$includes, $dependencies_includes, $dependencies, $current_url]; |
||
587 | } |
||
588 | /** |
||
589 | * @param array $dependencies |
||
590 | * @param string $url |
||
591 | * @param string $separator `+` or `/` |
||
592 | * |
||
593 | * @return bool |
||
594 | */ |
||
595 | protected function get_includes_is_dependency ($dependencies, $url, $separator) { |
||
628 | /** |
||
629 | * @param Config $Config |
||
630 | */ |
||
631 | protected function add_includes_on_page_manually_added ($Config) { |
||
653 | /** |
||
654 | * Getting of HTML, JS and CSS files list to be included |
||
655 | * |
||
656 | * @param bool $absolute If <i>true</i> - absolute paths to files will be returned |
||
657 | * |
||
658 | * @return string[][] |
||
1 ignored issue
–
show
|
|||
659 | */ |
||
660 | protected function get_includes_list ($absolute = false) { |
||
661 | $includes = []; |
||
662 | $add_includes = function ($dir, $public_path) use (&$includes, $absolute) { |
||
663 | foreach (['html', 'js', 'css'] as $extension) { |
||
664 | $list = get_files_list("$dir/$extension", "/.*\\.$extension$/i", 'f', $absolute ? true : "$public_path/$extension", true, 'name', '!include') ?: []; |
||
690 | /** |
||
691 | * Rebuilding of HTML, JS and CSS cache |
||
692 | * |
||
693 | * @return \cs\Page |
||
694 | */ |
||
695 | protected function rebuild_cache () { |
||
712 | /** |
||
713 | * Creates cached version of given HTML, JS and CSS files. |
||
714 | * Resulting file name consists of <b>$filename_prefix</b> and <b>$this->pcache_basename</b> |
||
715 | * |
||
716 | * @param string $filename_prefix |
||
717 | * @param array $includes Array of paths to files, may have keys: <b>css</b> and/or <b>js</b> and/or <b>html</b> |
||
718 | * |
||
719 | * @return array |
||
720 | */ |
||
721 | protected function create_cached_includes_files ($filename_prefix, $includes) { |
||
785 | /** |
||
786 | * Get dependencies of components between each other (only that contains some HTML, JS and CSS files) and mapping HTML, JS and CSS files to URL paths |
||
787 | * |
||
788 | * @return array[] [$dependencies, $includes_map] |
||
789 | */ |
||
790 | protected function includes_dependencies_and_map () { |
||
863 | /** |
||
864 | * Process meta information and corresponding entries to dependencies and functionalities |
||
865 | * |
||
866 | * @param array $meta |
||
867 | * @param array $dependencies |
||
868 | * @param array $functionalities |
||
869 | */ |
||
870 | protected function process_meta ($meta, &$dependencies, &$functionalities) { |
||
910 | /** |
||
911 | * Process map structure, fill includes map and remove files from list of all includes (remaining files will be included on all pages) |
||
912 | * |
||
913 | * @param array $map |
||
914 | * @param string $includes_dir |
||
915 | * @param array $includes_map |
||
916 | * @param array $all_includes |
||
917 | */ |
||
918 | protected function process_map ($map, $includes_dir, &$includes_map, &$all_includes) { |
||
945 | /** |
||
946 | * Replace functionalities by real packages names, take into account recursive dependencies |
||
947 | * |
||
948 | * @param array $dependencies |
||
949 | * @param array $functionalities |
||
950 | * |
||
951 | * @return array |
||
952 | */ |
||
953 | protected function normalize_dependencies ($dependencies, $functionalities) { |
||
1002 | /** |
||
1003 | * Includes array is composed from dependencies and sometimes dependencies doesn't have any files, so we'll clean that |
||
1004 | * |
||
1005 | * @param array $dependencies |
||
1006 | * @param array $includes_map |
||
1007 | * |
||
1008 | * @return array |
||
1009 | */ |
||
1010 | protected function clean_includes_arrays_without_files ($dependencies, $includes_map) { |
||
1021 | } |
||
1022 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.