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 | 36 | 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 | 4 | 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 | 4 | 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 | 4 | 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 | 4 | 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 | 4 | 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 | 4 | protected function config_internal ($config_structure, $target, $core = false) { |
|
| 210 | 4 | $config = h::script( |
|
| 211 | 4 | json_encode($config_structure, JSON_UNESCAPED_UNICODE), |
|
| 212 | [ |
||
| 213 | 4 | 'target' => $target, |
|
| 214 | 4 | 'class' => 'cs-config', |
|
| 215 | 4 | 'type' => 'application/json' |
|
| 216 | ] |
||
| 217 | ); |
||
| 218 | 4 | if ($core) { |
|
| 219 | 2 | $this->core_config .= $config; |
|
| 220 | } else { |
||
| 221 | 4 | $this->config .= $config; |
|
| 222 | } |
||
| 223 | 4 | return $this; |
|
| 224 | } |
||
| 225 | /** |
||
| 226 | * Getting of HTML, JS and CSS includes |
||
| 227 | * |
||
| 228 | * @return \cs\Page |
||
| 229 | */ |
||
| 230 | 4 | protected function add_includes_on_page () { |
|
| 231 | 4 | $Config = Config::instance(true); |
|
| 232 | 4 | if (!$Config) { |
|
| 233 | return $this; |
||
| 234 | } |
||
| 235 | /** |
||
| 236 | * Base name for cache files |
||
| 237 | */ |
||
| 238 | 4 | $this->pcache_basename_path = PUBLIC_CACHE.'/'.$this->theme.'_'.Language::instance()->clang; |
|
| 239 | // TODO: I hope some day we'll get rid of this sh*t :( |
||
| 240 | 4 | $this->ie_edge(); |
|
| 241 | 4 | $Request = Request::instance(); |
|
| 242 | /** |
||
| 243 | * If CSS and JavaScript compression enabled |
||
| 244 | */ |
||
| 245 | 4 | if ($this->page_compression_usage($Config, $Request)) { |
|
| 246 | /** |
||
| 247 | * Rebuilding HTML, JS and CSS cache if necessary |
||
| 248 | */ |
||
| 249 | 4 | $this->rebuild_cache($Config); |
|
| 250 | 4 | $this->webcomponents_polyfill($Request, $Config, true); |
|
| 251 | 4 | list($includes, $preload) = $this->get_includes_and_preload_resource_for_page_with_compression($Request); |
|
| 252 | } else { |
||
| 253 | 2 | $this->webcomponents_polyfill($Request, $Config, false); |
|
| 254 | /** |
||
| 255 | * Language translation is added explicitly only when compression is disabled, otherwise it will be in compressed JS file |
||
| 256 | */ |
||
| 257 | 2 | $this->config_internal(Language::instance(), 'cs.Language', true); |
|
| 258 | 2 | $this->config_internal($this->get_requirejs_paths(), 'requirejs.paths', true); |
|
| 259 | 2 | $includes = $this->get_includes_for_page_without_compression($Config, $Request); |
|
| 260 | 2 | $preload = []; |
|
| 261 | } |
||
| 262 | 4 | $this->css_internal($includes['css'], 'file', true); |
|
| 263 | 4 | $this->js_internal($includes['js'], 'file', true); |
|
| 264 | 4 | $this->html_internal($includes['html'], 'file', true); |
|
| 265 | 4 | $this->add_includes_on_page_manually_added($Config, $Request, $preload); |
|
| 266 | 4 | return $this; |
|
| 267 | } |
||
| 268 | /** |
||
| 269 | * @param Config $Config |
||
| 270 | * @param Request $Request |
||
| 271 | * |
||
| 272 | * @return bool |
||
| 273 | */ |
||
| 274 | 4 | protected function page_compression_usage ($Config, $Request) { |
|
| 277 | /** |
||
| 278 | * Add JS polyfills for IE/Edge |
||
| 279 | */ |
||
| 280 | 4 | protected function ie_edge () { |
|
| 290 | /** |
||
| 291 | * Hack: Add WebComponents Polyfill for browsers without native Shadow DOM support |
||
| 292 | * |
||
| 293 | * @param Request $Request |
||
| 294 | * @param Config $Config |
||
| 295 | * @param bool $with_compression |
||
| 296 | */ |
||
| 297 | 4 | protected function webcomponents_polyfill ($Request, $Config, $with_compression) { |
|
| 298 | 4 | if ($Request->cookie('shadow_dom') == 1) { |
|
| 299 | return; |
||
| 300 | } |
||
| 301 | 4 | if ($with_compression) { |
|
| 302 | 4 | $hash = file_get_contents(PUBLIC_CACHE.'/webcomponents.js.hash'); |
|
| 303 | 4 | $this->add_script_imports_to_document($Config, "<script src=\"/storage/pcache/webcomponents.js?$hash\"></script>\n"); |
|
| 304 | } else { |
||
| 305 | 2 | $this->add_script_imports_to_document($Config, "<script src=\"/includes/js/WebComponents-polyfill/webcomponents-custom.min.js\"></script>\n"); |
|
| 306 | } |
||
| 307 | 4 | } |
|
| 308 | /** |
||
| 309 | * @param Config $Config |
||
| 310 | * @param string $content |
||
| 311 | */ |
||
| 312 | 4 | protected function add_script_imports_to_document ($Config, $content) { |
|
| 313 | 4 | if ($Config->core['put_js_after_body']) { |
|
| 314 | 4 | $this->post_Body .= $content; |
|
| 315 | } else { |
||
| 316 | 2 | $this->Head .= $content; |
|
| 317 | } |
||
| 318 | 4 | } |
|
| 319 | /** |
||
| 320 | * @param Request $Request |
||
| 321 | * |
||
| 322 | * @return array[] |
||
| 323 | */ |
||
| 324 | 4 | protected function get_includes_and_preload_resource_for_page_with_compression ($Request) { |
|
| 336 | /** |
||
| 337 | * @param array $dependencies |
||
| 338 | * @param string[][] $includes_map |
||
| 339 | * @param Request $Request |
||
| 340 | * |
||
| 341 | * @return string[][] |
||
| 342 | */ |
||
| 343 | 4 | protected function get_normalized_includes ($dependencies, $includes_map, $Request) { |
|
| 383 | /** |
||
| 384 | * @param array $dependencies |
||
| 385 | * @param string $url |
||
| 386 | * @param Request $Request |
||
| 387 | * |
||
| 388 | * @return false|string |
||
| 389 | */ |
||
| 390 | 4 | protected function get_dependency_component ($dependencies, $url, $Request) { |
|
| 402 | /** |
||
| 403 | * @param Config $Config |
||
| 404 | * @param Request $Request |
||
| 405 | * |
||
| 406 | * @return string[][] |
||
| 407 | */ |
||
| 408 | 2 | protected function get_includes_for_page_without_compression ($Config, $Request) { |
|
| 409 | // To determine all dependencies and stuff we need `$Config` object to be already created |
||
| 410 | 2 | list($dependencies, $includes_map) = $this->get_includes_dependencies_and_map($Config); |
|
| 411 | 2 | $includes = $this->get_normalized_includes($dependencies, $includes_map, $Request); |
|
| 412 | 2 | return $this->add_versions_hash($this->absolute_path_to_relative($includes)); |
|
| 413 | } |
||
| 414 | /** |
||
| 415 | * @param string[]|string[][] $path |
||
| 416 | * |
||
| 417 | * @return string[]|string[][] |
||
| 418 | */ |
||
| 419 | 4 | protected function absolute_path_to_relative ($path) { |
|
| 422 | /** |
||
| 423 | * @param string[][] $includes |
||
| 424 | * |
||
| 425 | * @return string[][] |
||
| 426 | */ |
||
| 427 | 2 | protected function add_versions_hash ($includes) { |
|
| 428 | 2 | $content = array_reduce( |
|
| 429 | 2 | get_files_list(DIR.'/components', '/^meta\.json$/', 'f', true, true), |
|
| 430 | function ($content, $file) { |
||
| 431 | return $content.file_get_contents($file); |
||
| 432 | 2 | } |
|
| 433 | ); |
||
| 434 | 2 | $content_md5 = substr(md5($content), 0, 5); |
|
| 435 | 2 | foreach ($includes as &$files) { |
|
| 436 | 2 | foreach ($files as &$file) { |
|
| 437 | 2 | $file .= "?$content_md5"; |
|
| 438 | } |
||
| 439 | 2 | unset($file); |
|
| 440 | } |
||
| 441 | 2 | return $includes; |
|
| 442 | } |
||
| 443 | /** |
||
| 444 | * @param Config $Config |
||
| 445 | * @param Request $Request |
||
| 446 | * @param string[] $preload |
||
| 447 | */ |
||
| 448 | 4 | protected function add_includes_on_page_manually_added ($Config, $Request, $preload) { |
|
| 449 | /** @noinspection NestedTernaryOperatorInspection */ |
||
| 450 | 4 | $this->Head .= |
|
| 451 | 4 | array_reduce( |
|
| 452 | 4 | array_merge($this->core_css['path'], $this->css['path']), |
|
| 453 | function ($content, $href) { |
||
| 454 | return "$content<link href=\"$href\" rel=\"stylesheet\">\n"; |
||
| 455 | 4 | } |
|
| 456 | ). |
||
| 457 | 4 | h::style($this->css['plain'] ?: false); |
|
| 458 | 4 | if ($this->page_compression_usage($Config, $Request) && $Config->core['frontend_load_optimization']) { |
|
| 459 | 4 | $this->add_includes_on_page_manually_added_frontend_load_optimization($Config); |
|
| 460 | } else { |
||
| 461 | 2 | $this->add_includes_on_page_manually_added_normal($Config, $preload); |
|
| 462 | } |
||
| 463 | 4 | } |
|
| 464 | /** |
||
| 465 | * @param Config $Config |
||
| 466 | * @param string[] $preload |
||
| 467 | */ |
||
| 468 | 2 | protected function add_includes_on_page_manually_added_normal ($Config, $preload) { |
|
| 469 | 2 | $this->add_preload($preload); |
|
| 470 | 2 | $configs = $this->core_config.$this->config; |
|
| 471 | $scripts = |
||
| 472 | 2 | array_reduce( |
|
| 473 | 2 | array_merge($this->core_js['path'], $this->js['path']), |
|
| 474 | function ($content, $src) { |
||
| 475 | return "$content<script src=\"$src\"></script>\n"; |
||
| 476 | 2 | } |
|
| 477 | ). |
||
| 478 | 2 | h::script($this->js['plain'] ?: false); |
|
| 479 | $html_imports = |
||
| 480 | 2 | array_reduce( |
|
| 481 | 2 | array_merge($this->core_html['path'], $this->html['path']), |
|
| 482 | 2 | function ($content, $href) { |
|
| 483 | return "$content<link href=\"$href\" rel=\"import\">\n"; |
||
| 484 | 2 | } |
|
| 485 | ). |
||
| 486 | 2 | $this->html['plain']; |
|
| 487 | 2 | $this->Head .= $configs; |
|
| 488 | 2 | $this->add_script_imports_to_document($Config, $scripts.$html_imports); |
|
| 489 | 2 | } |
|
| 490 | /** |
||
| 491 | * @param string[] $preload |
||
| 492 | */ |
||
| 493 | 4 | protected function add_preload ($preload) { |
|
| 502 | /** |
||
| 503 | * @param Config $Config |
||
| 504 | */ |
||
| 505 | 4 | protected function add_includes_on_page_manually_added_frontend_load_optimization ($Config) { |
|
| 540 | } |
||
| 541 |