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 Config $Config |
||
| 295 | * @param bool $with_compression |
||
| 296 | */ |
||
| 297 | protected function webcomponents_polyfill ($Request, $Config, $with_compression) { |
||
| 308 | /** |
||
| 309 | * @param Config $Config |
||
| 310 | * @param string $content |
||
| 311 | */ |
||
| 312 | protected function add_script_imports_to_document ($Config, $content) { |
||
| 319 | /** |
||
| 320 | * @param Request $Request |
||
| 321 | * |
||
| 322 | * @return array[] |
||
| 323 | */ |
||
| 324 | 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 | 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 | protected function get_dependency_component ($dependencies, $url, $Request) { |
||
| 402 | /** |
||
| 403 | * @param Config $Config |
||
| 404 | * @param Request $Request |
||
| 405 | * |
||
| 406 | * @return string[][] |
||
| 407 | */ |
||
| 408 | protected function get_includes_for_page_without_compression ($Config, $Request) { |
||
| 414 | /** |
||
| 415 | * @param string[]|string[][] $path |
||
| 416 | * |
||
| 417 | * @return string[]|string[][] |
||
| 418 | */ |
||
| 419 | protected function absolute_path_to_relative ($path) { |
||
| 422 | /** |
||
| 423 | * @param string[][] $includes |
||
| 424 | * |
||
| 425 | * @return string[][] |
||
| 426 | */ |
||
| 427 | protected function add_versions_hash ($includes) { |
||
| 443 | /** |
||
| 444 | * @param Config $Config |
||
| 445 | * @param Request $Request |
||
| 446 | * @param string[] $preload |
||
| 447 | */ |
||
| 448 | protected function add_includes_on_page_manually_added ($Config, $Request, $preload) { |
||
| 464 | /** |
||
| 465 | * @param Config $Config |
||
| 466 | * @param Request $Request |
||
| 467 | * @param string[] $preload |
||
| 468 | */ |
||
| 469 | protected function add_includes_on_page_manually_added_normal ($Config, $Request, $preload) { |
||
| 493 | /** |
||
| 494 | * Hack: jQuery is kind of special; it is only loaded directly in normal mode, during frontend load optimization it is loaded asynchronously in frontend |
||
| 495 | * TODO: In future we'll load jQuery as AMD module only and this thing will not be needed |
||
| 496 | * |
||
| 497 | * @param bool $with_compression |
||
| 498 | * |
||
| 499 | * @return string |
||
| 500 | */ |
||
| 501 | protected function jquery ($with_compression) { |
||
| 509 | /** |
||
| 510 | * @param string[] $preload |
||
| 511 | */ |
||
| 512 | protected function add_preload ($preload) { |
||
| 521 | /** |
||
| 522 | * @param Config $Config |
||
| 523 | */ |
||
| 524 | protected function add_includes_on_page_manually_added_frontend_load_optimization ($Config) { |
||
| 559 | } |
||
| 560 |