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 | 52 | protected function init_includes () { |
|
| 97 | /** |
||
| 98 | * Including of Web Components |
||
| 99 | * |
||
| 100 | * @param string|string[] $add Path to including file, or code |
||
| 101 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
| 102 | * |
||
| 103 | * @return \cs\Page |
||
| 104 | */ |
||
| 105 | public function html ($add, $mode = 'file') { |
||
| 108 | /** |
||
| 109 | * @param string|string[] $add |
||
| 110 | * @param string $mode |
||
| 111 | * @param bool $core |
||
| 112 | * |
||
| 113 | * @return \cs\Page |
||
| 114 | */ |
||
| 115 | 4 | protected function html_internal ($add, $mode = 'file', $core = false) { |
|
| 118 | /** |
||
| 119 | * Including of JavaScript |
||
| 120 | * |
||
| 121 | * @param string|string[] $add Path to including file, or code |
||
| 122 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
| 123 | * |
||
| 124 | * @return \cs\Page |
||
| 125 | */ |
||
| 126 | public function js ($add, $mode = 'file') { |
||
| 129 | /** |
||
| 130 | * @param string|string[] $add |
||
| 131 | * @param string $mode |
||
| 132 | * @param bool $core |
||
| 133 | * |
||
| 134 | * @return \cs\Page |
||
| 135 | */ |
||
| 136 | 4 | protected function js_internal ($add, $mode = 'file', $core = false) { |
|
| 139 | /** |
||
| 140 | * Including of CSS |
||
| 141 | * |
||
| 142 | * @param string|string[] $add Path to including file, or code |
||
| 143 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
| 144 | * |
||
| 145 | * @return \cs\Page |
||
| 146 | */ |
||
| 147 | public function css ($add, $mode = 'file') { |
||
| 150 | /** |
||
| 151 | * @param string|string[] $add |
||
| 152 | * @param string $mode |
||
| 153 | * @param bool $core |
||
| 154 | * |
||
| 155 | * @return \cs\Page |
||
| 156 | */ |
||
| 157 | 4 | protected function css_internal ($add, $mode = 'file', $core = false) { |
|
| 160 | /** |
||
| 161 | * @param string $what |
||
| 162 | * @param string|string[] $add |
||
| 163 | * @param string $mode |
||
| 164 | * @param bool $core |
||
| 165 | * |
||
| 166 | * @return \cs\Page |
||
| 167 | */ |
||
| 168 | 4 | protected function include_common ($what, $add, $mode, $core) { |
|
| 189 | /** |
||
| 190 | * Add config on page to make it available on frontend |
||
| 191 | * |
||
| 192 | * @param mixed $config_structure Any scalar type or array |
||
| 193 | * @param string $target Target is property of `window` object where config will be inserted as value, nested properties like `cs.sub.prop` |
||
| 194 | * are supported and all nested properties are created on demand. It is recommended to use sub-properties of `cs` |
||
| 195 | * |
||
| 196 | * @return \cs\Page |
||
| 197 | */ |
||
| 198 | 4 | public function config ($config_structure, $target) { |
|
| 201 | /** |
||
| 202 | * @param mixed $config_structure |
||
| 203 | * @param string $target |
||
| 204 | * @param bool $core |
||
| 205 | * |
||
| 206 | * @return \cs\Page |
||
| 207 | */ |
||
| 208 | 4 | protected function config_internal ($config_structure, $target, $core = false) { |
|
| 224 | /** |
||
| 225 | * Getting of HTML, JS and CSS includes |
||
| 226 | * |
||
| 227 | * @return \cs\Page |
||
| 228 | */ |
||
| 229 | 4 | protected function add_includes_on_page () { |
|
| 280 | /** |
||
| 281 | * @param Config $Config |
||
| 282 | * @param Request $Request |
||
| 283 | * |
||
| 284 | * @return bool |
||
| 285 | */ |
||
| 286 | 4 | protected function page_compression_usage ($Config, $Request) { |
|
| 289 | /** |
||
| 290 | * Add JS polyfills for IE/Edge |
||
| 291 | */ |
||
| 292 | 4 | protected function edge () { |
|
| 302 | /** |
||
| 303 | * Hack: Add WebComponents Polyfill for browsers without native Shadow DOM support |
||
| 304 | * |
||
| 305 | * @param Request $Request |
||
| 306 | * @param Config $Config |
||
| 307 | * @param bool $with_compression |
||
| 308 | */ |
||
| 309 | 4 | protected function webcomponents_polyfill ($Request, $Config, $with_compression) { |
|
| 320 | /** |
||
| 321 | * @param Config $Config |
||
| 322 | * @param string $content |
||
| 323 | */ |
||
| 324 | 4 | protected function add_script_imports_to_document ($Config, $content) { |
|
| 331 | /** |
||
| 332 | * @param Request $Request |
||
| 333 | * |
||
| 334 | * @return array[] |
||
| 335 | */ |
||
| 336 | 4 | protected function get_includes_and_preload_resource_for_page_with_compression ($Request) { |
|
| 348 | /** |
||
| 349 | * @param array $dependencies |
||
| 350 | * @param string[][] $includes_map |
||
| 351 | * @param Request $Request |
||
| 352 | * |
||
| 353 | * @return string[][] |
||
| 354 | */ |
||
| 355 | 4 | protected function get_normalized_includes ($dependencies, $includes_map, $Request) { |
|
| 395 | /** |
||
| 396 | * @param array $dependencies |
||
| 397 | * @param string $url |
||
| 398 | * @param Request $Request |
||
| 399 | * |
||
| 400 | * @return false|string |
||
| 401 | */ |
||
| 402 | 4 | protected function get_dependency_component ($dependencies, $url, $Request) { |
|
| 414 | /** |
||
| 415 | * @param Config $Config |
||
| 416 | * @param Request $Request |
||
| 417 | * |
||
| 418 | * @return string[][] |
||
| 419 | */ |
||
| 420 | 2 | protected function get_includes_for_page_without_compression ($Config, $Request) { |
|
| 426 | /** |
||
| 427 | * @param string[]|string[][] $path |
||
| 428 | * |
||
| 429 | * @return string[]|string[][] |
||
| 430 | */ |
||
| 431 | 4 | protected function absolute_path_to_relative ($path) { |
|
| 434 | /** |
||
| 435 | * @param string[][] $includes |
||
| 436 | * |
||
| 437 | * @return string[][] |
||
| 438 | */ |
||
| 439 | 2 | protected function add_versions_hash ($includes) { |
|
| 455 | /** |
||
| 456 | * @param Config $Config |
||
| 457 | * @param Request $Request |
||
| 458 | * @param string[] $preload |
||
| 459 | */ |
||
| 460 | 4 | protected function add_includes_on_page_manually_added ($Config, $Request, $preload) { |
|
| 476 | /** |
||
| 477 | * @param Config $Config |
||
| 478 | * @param Request $Request |
||
| 479 | * @param string[] $preload |
||
| 480 | */ |
||
| 481 | 2 | protected function add_includes_on_page_manually_added_normal ($Config, $Request, $preload) { |
|
| 503 | /** |
||
| 504 | * @param string[] $preload |
||
| 505 | * @param Request $Request |
||
| 506 | */ |
||
| 507 | 4 | protected function add_preload ($preload, $Request) { |
|
| 520 | /** |
||
| 521 | * @param Config $Config |
||
| 522 | * @param Request $Request |
||
| 523 | */ |
||
| 524 | 4 | protected function add_includes_on_page_manually_added_frontend_load_optimization ($Config, $Request) { |
|
| 560 | } |
||
| 561 |