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 () { |
||
| 269 | /** |
||
| 270 | * @param Config $Config |
||
| 271 | * @param Request $Request |
||
| 272 | * |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | protected function page_compression_usage ($Config, $Request) { |
||
| 278 | /** |
||
| 279 | * Add JS polyfills for IE/Edge |
||
| 280 | */ |
||
| 281 | protected function ie_edge () { |
||
| 320 | /** |
||
| 321 | * Hack: Add WebComponents Polyfill for browsers without native Shadow DOM support |
||
| 322 | * |
||
| 323 | * @param Request $Request |
||
| 324 | * @param bool $with_compression |
||
| 325 | */ |
||
| 326 | protected function webcomponents_polyfill ($Request, $with_compression) { |
||
| 337 | /** |
||
| 338 | * @param Config $Config |
||
| 339 | * @param Request $Request |
||
| 340 | * |
||
| 341 | * @return array[] |
||
| 342 | */ |
||
| 343 | protected function get_includes_and_preload_resource_for_page_with_compression ($Config, $Request) { |
||
| 359 | /** |
||
| 360 | * @param array $dependencies |
||
| 361 | * @param string[][] $includes_map |
||
| 362 | * @param Request $Request |
||
| 363 | * |
||
| 364 | * @return string[][] |
||
| 365 | */ |
||
| 366 | protected function get_normalized_includes ($dependencies, $includes_map, $Request) { |
||
| 405 | /** |
||
| 406 | * @param array $dependencies |
||
| 407 | * @param string $url |
||
| 408 | * @param Request $Request |
||
| 409 | * |
||
| 410 | * @return false|string |
||
| 411 | */ |
||
| 412 | protected function get_dependency_component ($dependencies, $url, $Request) { |
||
| 424 | /** |
||
| 425 | * @param Config $Config |
||
| 426 | * @param Request $Request |
||
| 427 | * |
||
| 428 | * @return string[][] |
||
| 429 | */ |
||
| 430 | protected function get_includes_for_page_without_compression ($Config, $Request) { |
||
| 436 | /** |
||
| 437 | * @param string[]|string[][] $path |
||
| 438 | * |
||
| 439 | * @return string[]|string[][] |
||
| 440 | */ |
||
| 441 | protected function absolute_path_to_relative ($path) { |
||
| 444 | /** |
||
| 445 | * @param string[][] $includes |
||
| 446 | * |
||
| 447 | * @return string[][] |
||
| 448 | */ |
||
| 449 | protected function add_versions_hash ($includes) { |
||
| 465 | /** |
||
| 466 | * @param Config $Config |
||
| 467 | * @param Request $Request |
||
| 468 | * @param string[] $preload |
||
| 469 | */ |
||
| 470 | protected function add_includes_on_page_manually_added ($Config, $Request, $preload) { |
||
| 486 | /** |
||
| 487 | * @param Config $Config |
||
| 488 | * @param Request $Request |
||
| 489 | * @param string[] $preload |
||
| 490 | */ |
||
| 491 | protected function add_includes_on_page_manually_added_normal ($Config, $Request, $preload) { |
||
| 519 | /** |
||
| 520 | * Hack: jQuery is kind of special; it is only loaded directly in normal mode, during frontend load optimization it is loaded asynchronously in frontend |
||
| 521 | * TODO: In future we'll load jQuery as AMD module only and this thing will not be needed |
||
| 522 | * |
||
| 523 | * @param bool $with_compression |
||
| 524 | * |
||
| 525 | * @return string |
||
| 526 | */ |
||
| 527 | protected function jquery ($with_compression) { |
||
| 535 | /** |
||
| 536 | * @param string[] $preload |
||
| 537 | */ |
||
| 538 | protected function add_preload ($preload) { |
||
| 547 | /** |
||
| 548 | * @param Config $Config |
||
| 549 | */ |
||
| 550 | protected function add_includes_on_page_manually_added_frontend_load_optimization ($Config) { |
||
| 589 | } |
||
| 590 |