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 | 34 | protected function init_includes () { |
|
| 97 | /** |
||
| 98 | * @param string|string[] $add |
||
| 99 | * |
||
| 100 | * @return \cs\Page |
||
| 101 | */ |
||
| 102 | 4 | protected function core_html ($add) { |
|
| 105 | /** |
||
| 106 | * @param string|string[] $add |
||
| 107 | * |
||
| 108 | * @return \cs\Page |
||
| 109 | */ |
||
| 110 | 4 | protected function core_js ($add) { |
|
| 113 | /** |
||
| 114 | * @param string|string[] $add |
||
| 115 | * |
||
| 116 | * @return \cs\Page |
||
| 117 | */ |
||
| 118 | 4 | protected function core_css ($add) { |
|
| 121 | /** |
||
| 122 | * Including of Web Components |
||
| 123 | * |
||
| 124 | * @param string|string[] $add Path to including file, or code |
||
| 125 | * |
||
| 126 | * @return \cs\Page |
||
| 127 | */ |
||
| 128 | public function html ($add) { |
||
| 131 | /** |
||
| 132 | * Including of JavaScript |
||
| 133 | * |
||
| 134 | * @param string|string[] $add Path to including file, or code |
||
| 135 | * |
||
| 136 | * @return \cs\Page |
||
| 137 | */ |
||
| 138 | public function js ($add) { |
||
| 141 | /** |
||
| 142 | * Including of CSS |
||
| 143 | * |
||
| 144 | * @param string|string[] $add Path to including file, or code |
||
| 145 | * |
||
| 146 | * @return \cs\Page |
||
| 147 | */ |
||
| 148 | public function css ($add) { |
||
| 151 | /** |
||
| 152 | * @param string $what |
||
| 153 | * @param string|string[] $add |
||
| 154 | * @param bool $core |
||
| 155 | * |
||
| 156 | * @return \cs\Page |
||
| 157 | */ |
||
| 158 | 4 | protected function include_common ($what, $add, $core) { |
|
| 175 | /** |
||
| 176 | * Add config on page to make it available on frontend |
||
| 177 | * |
||
| 178 | * @param mixed $config_structure Any scalar type or array |
||
| 179 | * @param string $target Target is property of `window` object where config will be inserted as value, nested properties like `cs.sub.prop` |
||
| 180 | * are supported and all nested properties are created on demand. It is recommended to use sub-properties of `cs` |
||
| 181 | * |
||
| 182 | * @return \cs\Page |
||
| 183 | */ |
||
| 184 | 4 | public function config ($config_structure, $target) { |
|
| 187 | /** |
||
| 188 | * @param mixed $config_structure |
||
| 189 | * @param string $target |
||
| 190 | * @param bool $core |
||
| 191 | * |
||
| 192 | * @return \cs\Page |
||
| 193 | */ |
||
| 194 | 4 | protected function config_internal ($config_structure, $target, $core = false) { |
|
| 210 | /** |
||
| 211 | * Getting of HTML, JS and CSS includes |
||
| 212 | * |
||
| 213 | * @return \cs\Page |
||
| 214 | */ |
||
| 215 | 4 | protected function add_includes_on_page () { |
|
| 266 | /** |
||
| 267 | * @param Config $Config |
||
| 268 | * @param Request $Request |
||
| 269 | * |
||
| 270 | * @return bool |
||
| 271 | */ |
||
| 272 | 4 | protected function page_compression_usage ($Config, $Request) { |
|
| 275 | /** |
||
| 276 | * Add JS polyfills for IE/Edge |
||
| 277 | */ |
||
| 278 | 4 | protected function edge () { |
|
| 286 | /** |
||
| 287 | * Hack: Add WebComponents Polyfill for browsers without native Shadow DOM support |
||
| 288 | * |
||
| 289 | * @param Request $Request |
||
| 290 | * @param Config $Config |
||
| 291 | * @param bool $with_compression |
||
| 292 | */ |
||
| 293 | 4 | protected function webcomponents_polyfill ($Request, $Config, $with_compression) { |
|
| 304 | /** |
||
| 305 | * @param Config $Config |
||
| 306 | * @param string $content |
||
| 307 | */ |
||
| 308 | 4 | protected function add_script_imports_to_document ($Config, $content) { |
|
| 315 | /** |
||
| 316 | * @param Request $Request |
||
| 317 | * |
||
| 318 | * @return array[] |
||
| 319 | */ |
||
| 320 | 4 | protected function get_includes_and_preload_resource_for_page_with_compression ($Request) { |
|
| 332 | /** |
||
| 333 | * @param array $dependencies |
||
| 334 | * @param string[][] $includes_map |
||
| 335 | * @param Request $Request |
||
| 336 | * |
||
| 337 | * @return string[][] |
||
| 338 | */ |
||
| 339 | 4 | protected function get_normalized_includes ($dependencies, $includes_map, $Request) { |
|
| 379 | /** |
||
| 380 | * @param array $dependencies |
||
| 381 | * @param string $url |
||
| 382 | * @param Request $Request |
||
| 383 | * |
||
| 384 | * @return false|string |
||
| 385 | */ |
||
| 386 | 4 | protected function get_dependency_component ($dependencies, $url, $Request) { |
|
| 398 | /** |
||
| 399 | * @param Config $Config |
||
| 400 | * @param Request $Request |
||
| 401 | * |
||
| 402 | * @return string[][] |
||
| 403 | */ |
||
| 404 | 2 | protected function get_includes_for_page_without_compression ($Config, $Request) { |
|
| 410 | /** |
||
| 411 | * @param string[]|string[][] $path |
||
| 412 | * |
||
| 413 | * @return string[]|string[][] |
||
| 414 | */ |
||
| 415 | 4 | protected function absolute_path_to_relative ($path) { |
|
| 418 | /** |
||
| 419 | * @param string[][] $includes |
||
| 420 | * |
||
| 421 | * @return string[][] |
||
| 422 | */ |
||
| 423 | 2 | protected function add_versions_hash ($includes) { |
|
| 439 | /** |
||
| 440 | * @param Config $Config |
||
| 441 | * @param Request $Request |
||
| 442 | * @param string[] $preload |
||
| 443 | */ |
||
| 444 | 4 | protected function add_includes_on_page_manually_added ($Config, $Request, $preload) { |
|
| 458 | /** |
||
| 459 | * @param Config $Config |
||
| 460 | * @param Request $Request |
||
| 461 | * @param string[] $preload |
||
| 462 | */ |
||
| 463 | 2 | protected function add_includes_on_page_manually_added_normal ($Config, $Request, $preload) { |
|
| 481 | /** |
||
| 482 | * @param string[] $preload |
||
| 483 | * @param Request $Request |
||
| 484 | */ |
||
| 485 | 4 | protected function add_preload ($preload, $Request) { |
|
| 498 | /** |
||
| 499 | * @param Config $Config |
||
| 500 | * @param Request $Request |
||
| 501 | */ |
||
| 502 | 4 | protected function add_includes_on_page_manually_added_frontend_load_optimization ($Config, $Request) { |
|
| 532 | } |
||
| 533 |