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 |
||
| 32 | trait Includes { |
||
| 33 | use |
||
| 34 | Cache, |
||
| 35 | Collecting, |
||
| 36 | RequireJS; |
||
| 37 | protected $extension_to_as = [ |
||
| 38 | 'jpeg' => 'image', |
||
| 39 | 'jpe' => 'image', |
||
| 40 | 'jpg' => 'image', |
||
| 41 | 'gif' => 'image', |
||
| 42 | 'png' => 'image', |
||
| 43 | 'svg' => 'image', |
||
| 44 | 'svgz' => 'image', |
||
| 45 | 'woff' => 'font', |
||
| 46 | //'woff2' => 'font', |
||
| 47 | 'css' => 'style', |
||
| 48 | 'js' => 'script', |
||
| 49 | 'html' => 'document' |
||
| 50 | ]; |
||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $core_html; |
||
| 55 | /** |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | protected $core_js; |
||
| 59 | /** |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $core_css; |
||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | protected $core_config; |
||
| 67 | /** |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $html; |
||
| 71 | /** |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $js; |
||
| 75 | /** |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $css; |
||
| 79 | /** |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | protected $config; |
||
| 83 | /** |
||
| 84 | * Base name is used as prefix when creating CSS/JS/HTML cache files in order to avoid collisions when having several themes and languages |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | protected $pcache_basename_path; |
||
| 88 | protected function init_includes () { |
||
| 99 | /** |
||
| 100 | * Including of Web Components |
||
| 101 | * |
||
| 102 | * @param string|string[] $add Path to including file, or code |
||
| 103 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
| 104 | * |
||
| 105 | * @return \cs\Page |
||
| 106 | */ |
||
| 107 | function html ($add, $mode = 'file') { |
||
| 110 | /** |
||
| 111 | * @param string|string[] $add |
||
| 112 | * @param string $mode |
||
| 113 | * @param bool $core |
||
| 114 | * |
||
| 115 | * @return \cs\Page |
||
| 116 | */ |
||
| 117 | protected function html_internal ($add, $mode = 'file', $core = false) { |
||
| 120 | /** |
||
| 121 | * Including of JavaScript |
||
| 122 | * |
||
| 123 | * @param string|string[] $add Path to including file, or code |
||
| 124 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
| 125 | * |
||
| 126 | * @return \cs\Page |
||
| 127 | */ |
||
| 128 | function js ($add, $mode = 'file') { |
||
| 131 | /** |
||
| 132 | * @param string|string[] $add |
||
| 133 | * @param string $mode |
||
| 134 | * @param bool $core |
||
| 135 | * |
||
| 136 | * @return \cs\Page |
||
| 137 | */ |
||
| 138 | protected function js_internal ($add, $mode = 'file', $core = false) { |
||
| 141 | /** |
||
| 142 | * Including of CSS |
||
| 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 css ($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 css_internal ($add, $mode = 'file', $core = false) { |
||
| 162 | /** |
||
| 163 | * @param string $what |
||
| 164 | * @param string|string[] $add |
||
| 165 | * @param string $mode |
||
| 166 | * @param bool $core |
||
| 167 | * |
||
| 168 | * @return \cs\Page |
||
| 169 | */ |
||
| 170 | protected function include_common ($what, $add, $mode, $core) { |
||
| 191 | /** |
||
| 192 | * Add config on page to make it available on frontend |
||
| 193 | * |
||
| 194 | * @param mixed $config_structure Any scalar type or array |
||
| 195 | * @param string $target Target is property of `window` object where config will be inserted as value, nested properties like `cs.sub.prop` |
||
| 196 | * are supported and all nested properties are created on demand. It is recommended to use sub-properties of `cs` |
||
| 197 | * |
||
| 198 | * @return \cs\Page |
||
| 199 | */ |
||
| 200 | function config ($config_structure, $target) { |
||
| 203 | /** |
||
| 204 | * @param mixed $config_structure |
||
| 205 | * @param string $target |
||
| 206 | * @param bool $core |
||
| 207 | * |
||
| 208 | * @return \cs\Page |
||
| 209 | */ |
||
| 210 | protected function config_internal ($config_structure, $target, $core = false) { |
||
| 226 | /** |
||
| 227 | * Getting of HTML, JS and CSS includes |
||
| 228 | * |
||
| 229 | * @return \cs\Page |
||
| 230 | */ |
||
| 231 | protected function add_includes_on_page () { |
||
| 273 | /** |
||
| 274 | * @param Config $Config |
||
| 275 | * @param Request $Request |
||
| 276 | * |
||
| 277 | * @return bool |
||
| 278 | */ |
||
| 279 | protected function page_compression_usage ($Config, $Request) { |
||
| 282 | /** |
||
| 283 | * Add JS polyfills for IE/Edge |
||
| 284 | */ |
||
| 285 | protected function ie_edge () { |
||
| 317 | /** |
||
| 318 | * Hack: Add WebComponents Polyfill for browsers without native Shadow DOM support |
||
| 319 | * |
||
| 320 | * @param Request $Request |
||
| 321 | * @param bool $with_compression |
||
| 322 | */ |
||
| 323 | protected function webcomponents_polyfill ($Request, $with_compression) { |
||
| 334 | /** |
||
| 335 | * @param Request $Request |
||
| 336 | * |
||
| 337 | * @return array[] |
||
| 338 | */ |
||
| 339 | protected function get_includes_and_preload_resource_for_page_with_compression ($Request) { |
||
| 351 | /** |
||
| 352 | * @param array $dependencies |
||
| 353 | * @param string[][] $includes_map |
||
| 354 | * @param Request $Request |
||
| 355 | * |
||
| 356 | * @return string[][] |
||
| 357 | */ |
||
| 358 | protected function get_normalized_includes ($dependencies, $includes_map, $Request) { |
||
| 398 | /** |
||
| 399 | * @param array $dependencies |
||
| 400 | * @param string $url |
||
| 401 | * @param Request $Request |
||
| 402 | * |
||
| 403 | * @return false|string |
||
| 404 | */ |
||
| 405 | protected function get_dependency_component ($dependencies, $url, $Request) { |
||
| 417 | /** |
||
| 418 | * @param Config $Config |
||
| 419 | * @param Request $Request |
||
| 420 | * |
||
| 421 | * @return string[][] |
||
| 422 | */ |
||
| 423 | protected function get_includes_for_page_without_compression ($Config, $Request) { |
||
| 429 | /** |
||
| 430 | * @param string[]|string[][] $path |
||
| 431 | * |
||
| 432 | * @return string[]|string[][] |
||
| 433 | */ |
||
| 434 | protected function absolute_path_to_relative ($path) { |
||
| 437 | /** |
||
| 438 | * @param string[][] $includes |
||
| 439 | * |
||
| 440 | * @return string[][] |
||
| 441 | */ |
||
| 442 | protected function add_versions_hash ($includes) { |
||
| 458 | /** |
||
| 459 | * @param Config $Config |
||
| 460 | * @param Request $Request |
||
| 461 | * @param string[] $preload |
||
| 462 | */ |
||
| 463 | protected function add_includes_on_page_manually_added ($Config, $Request, $preload) { |
||
| 479 | /** |
||
| 480 | * @param Config $Config |
||
| 481 | * @param Request $Request |
||
| 482 | * @param string[] $preload |
||
| 483 | */ |
||
| 484 | protected function add_includes_on_page_manually_added_normal ($Config, $Request, $preload) { |
||
| 512 | /** |
||
| 513 | * Hack: jQuery is kind of special; it is only loaded directly in normal mode, during frontend load optimization it is loaded asynchronously in frontend |
||
| 514 | * TODO: In future we'll load jQuery as AMD module only and this thing will not be needed |
||
| 515 | * |
||
| 516 | * @param bool $with_compression |
||
| 517 | * |
||
| 518 | * @return string |
||
| 519 | */ |
||
| 520 | protected function jquery ($with_compression) { |
||
| 528 | /** |
||
| 529 | * @param string[] $preload |
||
| 530 | */ |
||
| 531 | protected function add_preload ($preload) { |
||
| 540 | /** |
||
| 541 | * @param Config $Config |
||
| 542 | */ |
||
| 543 | protected function add_includes_on_page_manually_added_frontend_load_optimization ($Config) { |
||
| 582 | } |
||
| 583 |