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 |
||
| 33 | trait Includes { |
||
| 34 | use |
||
| 35 | Cache, |
||
| 36 | Collecting, |
||
| 37 | RequireJS; |
||
| 38 | protected $extension_to_as = [ |
||
| 39 | 'jpeg' => 'image', |
||
| 40 | 'jpe' => 'image', |
||
| 41 | 'jpg' => 'image', |
||
| 42 | 'gif' => 'image', |
||
| 43 | 'png' => 'image', |
||
| 44 | 'svg' => 'image', |
||
| 45 | 'svgz' => 'image', |
||
| 46 | 'ttf' => 'font', |
||
| 47 | 'ttc' => 'font', |
||
| 48 | 'otf' => 'font', |
||
| 49 | 'woff' => 'font', |
||
| 50 | 'woff2' => 'font', |
||
| 51 | 'eot' => 'font', |
||
| 52 | 'css' => 'style', |
||
| 53 | 'js' => 'script', |
||
| 54 | 'html' => 'document' |
||
| 55 | ]; |
||
| 56 | /** |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $core_html; |
||
| 60 | /** |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $core_js; |
||
| 64 | /** |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | protected $core_css; |
||
| 68 | /** |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | protected $core_config; |
||
| 72 | /** |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | protected $html; |
||
| 76 | /** |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | protected $js; |
||
| 80 | /** |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | protected $css; |
||
| 84 | /** |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | protected $config; |
||
| 88 | /** |
||
| 89 | * Base name is used as prefix when creating CSS/JS/HTML cache files in order to avoid collisions when having several themes and languages |
||
| 90 | * @var string |
||
| 91 | */ |
||
| 92 | protected $pcache_basename_path; |
||
| 93 | protected function init_includes () { |
||
| 94 | $this->core_html = ['path' => [], 'plain' => '']; |
||
| 95 | $this->core_js = ['path' => [], 'plain' => '']; |
||
| 96 | $this->core_css = ['path' => [], 'plain' => '']; |
||
| 97 | $this->core_config = ''; |
||
| 98 | $this->html = ['path' => [], 'plain' => '']; |
||
| 99 | $this->js = ['path' => [], 'plain' => '']; |
||
| 100 | $this->css = ['path' => [], 'plain' => '']; |
||
| 101 | $this->config = ''; |
||
| 102 | $this->pcache_basename_path = ''; |
||
| 103 | } |
||
| 104 | /** |
||
| 105 | * Including of Web Components |
||
| 106 | * |
||
| 107 | * @param string|string[] $add Path to including file, or code |
||
| 108 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
| 109 | * |
||
| 110 | * @return \cs\Page |
||
| 111 | */ |
||
| 112 | function html ($add, $mode = 'file') { |
||
| 113 | return $this->html_internal($add, $mode); |
||
| 114 | } |
||
| 115 | /** |
||
| 116 | * @param string|string[] $add |
||
| 117 | * @param string $mode |
||
| 118 | * @param bool $core |
||
| 119 | * |
||
| 120 | * @return \cs\Page |
||
| 121 | */ |
||
| 122 | protected function html_internal ($add, $mode = 'file', $core = false) { |
||
| 123 | return $this->include_common('html', $add, $mode, $core); |
||
| 124 | } |
||
| 125 | /** |
||
| 126 | * Including of JavaScript |
||
| 127 | * |
||
| 128 | * @param string|string[] $add Path to including file, or code |
||
| 129 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
| 130 | * |
||
| 131 | * @return \cs\Page |
||
| 132 | */ |
||
| 133 | function js ($add, $mode = 'file') { |
||
| 134 | return $this->js_internal($add, $mode); |
||
| 135 | } |
||
| 136 | /** |
||
| 137 | * @param string|string[] $add |
||
| 138 | * @param string $mode |
||
| 139 | * @param bool $core |
||
| 140 | * |
||
| 141 | * @return \cs\Page |
||
| 142 | */ |
||
| 143 | protected function js_internal ($add, $mode = 'file', $core = false) { |
||
| 144 | return $this->include_common('js', $add, $mode, $core); |
||
| 145 | } |
||
| 146 | /** |
||
| 147 | * Including of CSS |
||
| 148 | * |
||
| 149 | * @param string|string[] $add Path to including file, or code |
||
| 150 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
| 151 | * |
||
| 152 | * @return \cs\Page |
||
| 153 | */ |
||
| 154 | function css ($add, $mode = 'file') { |
||
| 155 | return $this->css_internal($add, $mode); |
||
| 156 | } |
||
| 157 | /** |
||
| 158 | * @param string|string[] $add |
||
| 159 | * @param string $mode |
||
| 160 | * @param bool $core |
||
| 161 | * |
||
| 162 | * @return \cs\Page |
||
| 163 | */ |
||
| 164 | protected function css_internal ($add, $mode = 'file', $core = false) { |
||
| 165 | return $this->include_common('css', $add, $mode, $core); |
||
| 166 | } |
||
| 167 | /** |
||
| 168 | * @param string $what |
||
| 169 | * @param string|string[] $add |
||
| 170 | * @param string $mode |
||
| 171 | * @param bool $core |
||
| 172 | * |
||
| 173 | * @return \cs\Page |
||
| 174 | */ |
||
| 175 | protected function include_common ($what, $add, $mode, $core) { |
||
| 176 | if (!$add) { |
||
| 177 | return $this; |
||
| 178 | } |
||
| 179 | if (is_array($add)) { |
||
| 180 | foreach (array_filter($add) as $style) { |
||
| 181 | $this->include_common($what, $style, $mode, $core); |
||
| 182 | } |
||
| 183 | } else { |
||
| 184 | if ($core) { |
||
| 185 | $what = "core_$what"; |
||
| 186 | } |
||
| 187 | $target = &$this->$what; |
||
| 188 | if ($mode == 'file') { |
||
| 189 | $target['path'][] = $add; |
||
| 190 | } elseif ($mode == 'code') { |
||
| 191 | $target['plain'] .= "$add\n"; |
||
| 192 | } |
||
| 193 | } |
||
| 194 | return $this; |
||
| 195 | } |
||
| 196 | /** |
||
| 197 | * Add config on page to make it available on frontend |
||
| 198 | * |
||
| 199 | * @param mixed $config_structure Any scalar type or array |
||
| 200 | * @param string $target Target is property of `window` object where config will be inserted as value, nested properties like `cs.sub.prop` |
||
| 201 | * are supported and all nested properties are created on demand. It is recommended to use sub-properties of `cs` |
||
| 202 | * |
||
| 203 | * @return \cs\Page |
||
| 204 | */ |
||
| 205 | function config ($config_structure, $target) { |
||
| 208 | /** |
||
| 209 | * @param mixed $config_structure |
||
| 210 | * @param string $target |
||
| 211 | * @param bool $core |
||
| 212 | * |
||
| 213 | * @return \cs\Page |
||
| 214 | */ |
||
| 215 | protected function config_internal ($config_structure, $target, $core = false) { |
||
| 231 | /** |
||
| 232 | * Getting of HTML, JS and CSS includes |
||
| 233 | * |
||
| 234 | * @return \cs\Page |
||
| 235 | */ |
||
| 236 | protected function add_includes_on_page () { |
||
| 237 | $Config = Config::instance(true); |
||
| 238 | if (!$Config) { |
||
| 239 | return $this; |
||
| 274 | /** |
||
| 275 | * Add JS polyfills for IE/Edge |
||
| 276 | */ |
||
| 277 | protected function ie_edge () { |
||
| 316 | /** |
||
| 317 | * Hack: Add WebComponents Polyfill for browsers without native Shadow DOM support |
||
| 318 | * |
||
| 319 | * @param Request $Request |
||
| 320 | * @param bool $with_compression |
||
| 321 | */ |
||
| 322 | protected function webcomponents_polyfill ($Request, $with_compression) { |
||
| 340 | /** |
||
| 341 | * @param string[] $preload |
||
| 342 | */ |
||
| 343 | protected function add_preload ($preload) { |
||
| 352 | /** |
||
| 353 | * @param Config $Config |
||
| 354 | * @param Request $Request |
||
| 355 | * |
||
| 356 | * @return array[] |
||
| 357 | */ |
||
| 358 | protected function get_includes_and_preload_resource_for_page_with_compression ($Config, $Request) { |
||
| 391 | /** |
||
| 392 | * @param array $dependencies |
||
| 393 | * @param string[][] $includes_map |
||
| 394 | * @param string $separator `+` or `/` |
||
| 395 | * @param Request $Request |
||
| 396 | * |
||
| 397 | * @return array[] |
||
| 398 | */ |
||
| 399 | protected function get_normalized_includes ($dependencies, $includes_map, $separator, $Request) { |
||
| 427 | /** |
||
| 428 | * @param array $dependencies |
||
| 429 | * @param string $url |
||
| 430 | * @param string $separator `+` or `/` |
||
| 431 | * @param Request $Request |
||
| 432 | * |
||
| 433 | * @return bool |
||
| 434 | */ |
||
| 435 | protected function is_dependency ($dependencies, $url, $separator, $Request) { |
||
| 446 | /** |
||
| 447 | * @param Config $Config |
||
| 448 | * @param Request $Request |
||
| 449 | * |
||
| 450 | * @return string[][] |
||
| 451 | */ |
||
| 452 | protected function get_includes_for_page_without_compression ($Config, $Request) { |
||
| 458 | /** |
||
| 459 | * @param string[]|string[][] $path |
||
| 460 | * |
||
| 461 | * @return string[]|string[][] |
||
| 462 | */ |
||
| 463 | protected function absolute_path_to_relative ($path) { |
||
| 466 | /** |
||
| 467 | * @param string[][] $includes |
||
| 468 | * |
||
| 469 | * @return string[][] |
||
| 470 | */ |
||
| 471 | protected function add_versions_hash ($includes) { |
||
| 487 | /** |
||
| 488 | * @param Config $Config |
||
| 489 | */ |
||
| 490 | protected function add_includes_on_page_manually_added ($Config) { |
||
| 525 | } |
||
| 526 |