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 | 34 | protected function init_includes () { |
|
| 97 | /** |
||
| 98 | * @param string|string[] $add |
||
| 99 | * |
||
| 100 | * @return \cs\Page |
||
| 101 | */ |
||
| 102 | 4 | protected function core_html ($add) { |
|
| 105 | /** |
||
| 106 | * @param string|string[] $add |
||
| 107 | * |
||
| 108 | * @return \cs\Page |
||
| 109 | */ |
||
| 110 | 4 | protected function core_js ($add) { |
|
| 113 | /** |
||
| 114 | * @param string|string[] $add |
||
| 115 | * |
||
| 116 | * @return \cs\Page |
||
| 117 | */ |
||
| 118 | 4 | protected function core_css ($add) { |
|
| 121 | /** |
||
| 122 | * Including of Web Components |
||
| 123 | * |
||
| 124 | * @param string|string[] $add Path to including file, or code |
||
| 125 | * |
||
| 126 | * @return \cs\Page |
||
| 127 | */ |
||
| 128 | 2 | public function html ($add) { |
|
| 129 | 2 | return $this->include_common('html', $add, false); |
|
| 130 | } |
||
| 131 | /** |
||
| 132 | * Including of JavaScript |
||
| 133 | * |
||
| 134 | * @param string|string[] $add Path to including file, or code |
||
| 135 | * |
||
| 136 | * @return \cs\Page |
||
| 137 | */ |
||
| 138 | 2 | public function js ($add) { |
|
| 139 | 2 | return $this->include_common('js', $add, false); |
|
| 140 | } |
||
| 141 | /** |
||
| 142 | * Including of CSS |
||
| 143 | * |
||
| 144 | * @param string|string[] $add Path to including file, or code |
||
| 145 | * |
||
| 146 | * @return \cs\Page |
||
| 147 | */ |
||
| 148 | 2 | public function css ($add) { |
|
| 149 | 2 | return $this->include_common('css', $add, false); |
|
| 150 | } |
||
| 151 | /** |
||
| 152 | * @param string $what |
||
| 153 | * @param string|string[] $add |
||
| 154 | * @param bool $core |
||
| 155 | * |
||
| 156 | * @return \cs\Page |
||
| 157 | */ |
||
| 158 | 4 | protected function include_common ($what, $add, $core) { |
|
| 159 | 4 | if (!$add) { |
|
| 160 | 2 | return $this; |
|
| 161 | } |
||
| 162 | 4 | if (is_array($add)) { |
|
| 163 | 4 | foreach (array_filter($add) as $a) { |
|
| 164 | 4 | $this->include_common($what, $a, $core); |
|
| 165 | } |
||
| 166 | } else { |
||
| 167 | 4 | if ($core) { |
|
| 168 | 4 | $what = "core_$what"; |
|
| 169 | } |
||
| 170 | 4 | $target = &$this->$what; |
|
| 171 | 4 | $target[] = $add; |
|
| 172 | } |
||
| 173 | 4 | return $this; |
|
| 174 | } |
||
| 175 | /** |
||
| 176 | * Add config on page to make it available on frontend |
||
| 177 | * |
||
| 178 | * @param mixed $config_structure Any scalar type or array |
||
| 179 | * @param string $target Target is property of `window` object where config will be inserted as value, nested properties like `cs.sub.prop` |
||
| 180 | * are supported and all nested properties are created on demand. It is recommended to use sub-properties of `cs` |
||
| 181 | * |
||
| 182 | * @return \cs\Page |
||
| 183 | */ |
||
| 184 | 4 | public function config ($config_structure, $target) { |
|
| 187 | /** |
||
| 188 | * @param mixed $config_structure |
||
| 189 | * @param string $target |
||
| 190 | * @param bool $core |
||
| 191 | * |
||
| 192 | * @return \cs\Page |
||
| 193 | */ |
||
| 194 | 4 | protected function config_internal ($config_structure, $target, $core = false) { |
|
| 210 | /** |
||
| 211 | * Getting of HTML, JS and CSS includes |
||
| 212 | * |
||
| 213 | * @return \cs\Page |
||
| 214 | */ |
||
| 215 | 4 | protected function add_includes_on_page () { |
|
| 266 | /** |
||
| 267 | * @param Config $Config |
||
| 268 | * @param Request $Request |
||
| 269 | * |
||
| 270 | * @return bool |
||
| 271 | */ |
||
| 272 | 4 | protected function page_compression_usage ($Config, $Request) { |
|
| 275 | /** |
||
| 276 | * Add JS polyfills for IE/Edge |
||
| 277 | */ |
||
| 278 | 4 | protected function edge () { |
|
| 279 | 4 | if (strpos(Request::instance()->header('user-agent'), 'Edge') === false) { |
|
| 280 | 4 | return; |
|
| 281 | } |
||
| 282 | 2 | $this->core_js( |
|
| 283 | 2 | get_files_list(DIR.'/includes/js/microsoft_sh*t', '/.*\.js$/i', 'f', 'includes/js/microsoft_sh*t', true) |
|
| 284 | ); |
||
| 285 | 2 | } |
|
| 286 | /** |
||
| 287 | * Hack: Add WebComponents Polyfill for browsers without native Shadow DOM support |
||
| 288 | * |
||
| 289 | * @param Request $Request |
||
| 290 | * @param Config $Config |
||
| 291 | * @param bool $with_compression |
||
| 292 | */ |
||
| 293 | 4 | protected function webcomponents_polyfill ($Request, $Config, $with_compression) { |
|
| 294 | 4 | if (($this->theme != Config::SYSTEM_THEME && $Config->core['disable_webcomponents']) || $Request->cookie('shadow_dom') == 1) { |
|
| 295 | 2 | return; |
|
| 296 | } |
||
| 297 | 4 | if ($with_compression) { |
|
| 298 | 4 | $hash = file_get_contents(PUBLIC_CACHE.'/webcomponents.js.hash'); |
|
| 299 | 4 | $this->add_script_imports_to_document($Config, "<script src=\"/storage/pcache/webcomponents.js?$hash\"></script>\n"); |
|
| 300 | } else { |
||
| 301 | 2 | $this->add_script_imports_to_document($Config, "<script src=\"/includes/js/WebComponents-polyfill/webcomponents-custom.min.js\"></script>\n"); |
|
| 302 | } |
||
| 303 | 4 | } |
|
| 304 | /** |
||
| 305 | * @param Config $Config |
||
| 306 | * @param string $content |
||
| 307 | */ |
||
| 308 | 4 | protected function add_script_imports_to_document ($Config, $content) { |
|
| 315 | /** |
||
| 316 | * @param Request $Request |
||
| 317 | * |
||
| 318 | * @return array[] |
||
| 319 | */ |
||
| 320 | 4 | protected function get_includes_and_preload_resource_for_page_with_compression ($Request) { |
|
| 332 | /** |
||
| 333 | * @param array $dependencies |
||
| 334 | * @param string[][] $includes_map |
||
| 335 | * @param Request $Request |
||
| 336 | * |
||
| 337 | * @return string[][] |
||
| 338 | */ |
||
| 339 | 4 | protected function get_normalized_includes ($dependencies, $includes_map, $Request) { |
|
| 340 | 4 | $current_module = $Request->current_module; |
|
| 341 | /** |
||
| 342 | * Current URL based on controller path (it better represents how page was rendered) |
||
| 343 | */ |
||
| 344 | 4 | $current_url = array_slice(App::instance()->controller_path, 1); |
|
| 345 | 4 | $current_url = ($Request->admin_path ? 'admin/' : '')."$current_module/".implode('/', $current_url); |
|
| 346 | /** |
||
| 347 | * Narrow the dependencies to current module only |
||
| 348 | */ |
||
| 349 | 4 | $dependencies = array_unique( |
|
| 350 | array_merge( |
||
| 351 | 4 | ['System'], |
|
| 352 | 4 | $dependencies['System'], |
|
| 353 | 4 | isset($dependencies[$current_module]) ? $dependencies[$current_module] : [] |
|
| 354 | ) |
||
| 355 | ); |
||
| 356 | 4 | $system_includes = []; |
|
| 357 | // Array with empty array in order to avoid `array_merge()` failure later |
||
| 358 | 4 | $dependencies_includes = array_fill_keys($dependencies, [[]]); |
|
| 359 | 4 | $includes = []; |
|
| 360 | 4 | foreach ($includes_map as $path => $local_includes) { |
|
| 361 | 4 | if ($path == 'System') { |
|
| 362 | 4 | $system_includes = $local_includes; |
|
| 363 | 4 | } elseif ($component = $this->get_dependency_component($dependencies, $path, $Request)) { |
|
| 364 | /** |
||
| 365 | * @var string $component |
||
| 366 | */ |
||
| 367 | 2 | $dependencies_includes[$component][] = $local_includes; |
|
| 368 | 4 | } elseif (mb_strpos($current_url, $path) === 0) { |
|
| 369 | 4 | $includes[] = $local_includes; |
|
| 370 | } |
||
| 371 | } |
||
| 372 | // Convert to indexed array first |
||
| 373 | 4 | $dependencies_includes = array_values($dependencies_includes); |
|
| 374 | // Flatten array on higher level |
||
| 375 | 4 | $dependencies_includes = array_merge(...$dependencies_includes); |
|
| 376 | // Hack: 2 array_merge_recursive() just to be compatible with HHVM, simplify when https://github.com/facebook/hhvm/issues/7087 is resolved |
||
| 377 | 4 | return _array(array_merge_recursive(array_merge_recursive($system_includes, ...$dependencies_includes), ...$includes)); |
|
| 378 | } |
||
| 379 | /** |
||
| 380 | * @param array $dependencies |
||
| 381 | * @param string $url |
||
| 382 | * @param Request $Request |
||
| 383 | * |
||
| 384 | * @return false|string |
||
| 385 | */ |
||
| 386 | 4 | protected function get_dependency_component ($dependencies, $url, $Request) { |
|
| 398 | /** |
||
| 399 | * @param Config $Config |
||
| 400 | * @param Request $Request |
||
| 401 | * |
||
| 402 | * @return string[][] |
||
| 403 | */ |
||
| 404 | 2 | protected function get_includes_for_page_without_compression ($Config, $Request) { |
|
| 410 | /** |
||
| 411 | * @param string[]|string[][] $path |
||
| 412 | * |
||
| 413 | * @return string[]|string[][] |
||
| 414 | */ |
||
| 415 | 6 | protected function absolute_path_to_relative ($path) { |
|
| 418 | /** |
||
| 419 | * @param string[][] $includes |
||
| 420 | * |
||
| 421 | * @return string[][] |
||
| 422 | */ |
||
| 423 | 2 | protected function add_versions_hash ($includes) { |
|
| 439 | /** |
||
| 440 | * @param Config $Config |
||
| 441 | * @param Request $Request |
||
| 442 | * @param string[] $preload |
||
| 443 | */ |
||
| 444 | 4 | protected function add_includes_on_page_manually_added ($Config, $Request, $preload) { |
|
| 458 | /** |
||
| 459 | * @param Config $Config |
||
| 460 | * @param Request $Request |
||
| 461 | * @param string[] $preload |
||
| 462 | */ |
||
| 463 | 2 | protected function add_includes_on_page_manually_added_normal ($Config, $Request, $preload) { |
|
| 481 | /** |
||
| 482 | * @param string[] $preload |
||
| 483 | * @param Request $Request |
||
| 484 | */ |
||
| 485 | 4 | protected function add_preload ($preload, $Request) { |
|
| 486 | 4 | if ($Request->cookie('pushed')) { |
|
| 487 | 2 | return; |
|
| 488 | } |
||
| 498 | /** |
||
| 499 | * @param Config $Config |
||
| 500 | * @param Request $Request |
||
| 501 | */ |
||
| 502 | 4 | protected function add_includes_on_page_manually_added_frontend_load_optimization ($Config, $Request) { |
|
| 532 | } |
||
| 533 |