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 | /** |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $core_html; |
||
| 42 | /** |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $core_js; |
||
| 46 | /** |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $core_css; |
||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $core_config; |
||
| 54 | /** |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $html; |
||
| 58 | /** |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | protected $js; |
||
| 62 | /** |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | protected $css; |
||
| 66 | /** |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | protected $config; |
||
| 70 | /** |
||
| 71 | * Base name is used as prefix when creating CSS/JS/HTML cache files in order to avoid collisions when having several themes and languages |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected $pcache_basename_path; |
||
| 75 | protected function init_includes () { |
||
| 76 | $this->core_html = ['path' => [], 'plain' => '']; |
||
| 77 | $this->core_js = ['path' => [], 'plain' => '']; |
||
| 78 | $this->core_css = ['path' => [], 'plain' => '']; |
||
| 79 | $this->core_config = ''; |
||
| 80 | $this->html = ['path' => [], 'plain' => '']; |
||
| 81 | $this->js = ['path' => [], 'plain' => '']; |
||
| 82 | $this->css = ['path' => [], 'plain' => '']; |
||
| 83 | $this->config = ''; |
||
| 84 | $this->pcache_basename_path = ''; |
||
| 85 | } |
||
| 86 | /** |
||
| 87 | * Including of Web Components |
||
| 88 | * |
||
| 89 | * @param string|string[] $add Path to including file, or code |
||
| 90 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
| 91 | * |
||
| 92 | * @return \cs\Page |
||
| 93 | */ |
||
| 94 | function html ($add, $mode = 'file') { |
||
| 95 | return $this->html_internal($add, $mode); |
||
| 96 | } |
||
| 97 | /** |
||
| 98 | * @param string|string[] $add |
||
| 99 | * @param string $mode |
||
| 100 | * @param bool $core |
||
| 101 | * |
||
| 102 | * @return \cs\Page |
||
| 103 | */ |
||
| 104 | protected function html_internal ($add, $mode = 'file', $core = false) { |
||
| 105 | return $this->include_common('html', $add, $mode, $core); |
||
| 106 | } |
||
| 107 | /** |
||
| 108 | * Including of JavaScript |
||
| 109 | * |
||
| 110 | * @param string|string[] $add Path to including file, or code |
||
| 111 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
| 112 | * |
||
| 113 | * @return \cs\Page |
||
| 114 | */ |
||
| 115 | function js ($add, $mode = 'file') { |
||
| 118 | /** |
||
| 119 | * @param string|string[] $add |
||
| 120 | * @param string $mode |
||
| 121 | * @param bool $core |
||
| 122 | * |
||
| 123 | * @return \cs\Page |
||
| 124 | */ |
||
| 125 | protected function js_internal ($add, $mode = 'file', $core = false) { |
||
| 128 | /** |
||
| 129 | * Including of CSS |
||
| 130 | * |
||
| 131 | * @param string|string[] $add Path to including file, or code |
||
| 132 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
| 133 | * |
||
| 134 | * @return \cs\Page |
||
| 135 | */ |
||
| 136 | function css ($add, $mode = 'file') { |
||
| 139 | /** |
||
| 140 | * @param string|string[] $add |
||
| 141 | * @param string $mode |
||
| 142 | * @param bool $core |
||
| 143 | * |
||
| 144 | * @return \cs\Page |
||
| 145 | */ |
||
| 146 | protected function css_internal ($add, $mode = 'file', $core = false) { |
||
| 149 | /** |
||
| 150 | * @param string $what |
||
| 151 | * @param string|string[] $add |
||
| 152 | * @param string $mode |
||
| 153 | * @param bool $core |
||
| 154 | * |
||
| 155 | * @return \cs\Page |
||
| 156 | */ |
||
| 157 | protected function include_common ($what, $add, $mode, $core) { |
||
| 178 | /** |
||
| 179 | * Add config on page to make it available on frontend |
||
| 180 | * |
||
| 181 | * @param mixed $config_structure Any scalar type or array |
||
| 182 | * @param string $target Target is property of `window` object where config will be inserted as value, nested properties like `cs.sub.prop` |
||
| 183 | * are supported and all nested properties are created on demand. It is recommended to use sub-properties of `cs` |
||
| 184 | * |
||
| 185 | * @return \cs\Page |
||
| 186 | */ |
||
| 187 | function config ($config_structure, $target) { |
||
| 190 | /** |
||
| 191 | * @param mixed $config_structure |
||
| 192 | * @param string $target |
||
| 193 | * @param bool $core |
||
| 194 | * |
||
| 195 | * @return \cs\Page |
||
| 196 | */ |
||
| 197 | protected function config_internal ($config_structure, $target, $core = false) { |
||
| 213 | /** |
||
| 214 | * Getting of HTML, JS and CSS includes |
||
| 215 | * |
||
| 216 | * @return \cs\Page |
||
| 217 | */ |
||
| 218 | protected function add_includes_on_page () { |
||
| 256 | /** |
||
| 257 | * Add JS polyfills for IE/Edge |
||
| 258 | */ |
||
| 259 | protected function ie_edge () { |
||
| 298 | /** |
||
| 299 | * Hack: Add WebComponents Polyfill for browsers without native Shadow DOM support |
||
| 300 | * |
||
| 301 | * @param Request $Request |
||
| 302 | * @param bool $with_compression |
||
| 303 | */ |
||
| 304 | protected function webcomponents_polyfill ($Request, $with_compression) { |
||
| 322 | /** |
||
| 323 | * @param string[] $preload |
||
| 324 | */ |
||
| 325 | protected function add_preloads ($preload) { |
||
| 363 | /** |
||
| 364 | * @param Config $Config |
||
| 365 | * @param Request $Request |
||
| 366 | * |
||
| 367 | * @return array |
||
|
1 ignored issue
–
show
|
|||
| 368 | */ |
||
| 369 | protected function get_includes_and_preload_resource_for_page_with_compression ($Config, $Request) { |
||
| 401 | /** |
||
| 402 | * @param array $dependencies |
||
| 403 | * @param string[][] $includes_map |
||
| 404 | * @param string $separator `+` or `/` |
||
| 405 | * @param Request $Request |
||
| 406 | * |
||
| 407 | * @return array |
||
| 408 | */ |
||
| 409 | protected function get_normalized_includes ($dependencies, $includes_map, $separator, $Request) { |
||
| 437 | /** |
||
| 438 | * @param array $dependencies |
||
| 439 | * @param string $url |
||
| 440 | * @param string $separator `+` or `/` |
||
| 441 | * @param Request $Request |
||
| 442 | * |
||
| 443 | * @return bool |
||
| 444 | */ |
||
| 445 | protected function is_dependency ($dependencies, $url, $separator, $Request) { |
||
| 456 | /** |
||
| 457 | * @param Config $Config |
||
| 458 | * @param Request $Request |
||
| 459 | * |
||
| 460 | * @return string[][] |
||
| 461 | */ |
||
| 462 | protected function get_includes_for_page_without_compression ($Config, $Request) { |
||
| 468 | /** |
||
| 469 | * @param string[]|string[][] $path |
||
| 470 | * |
||
| 471 | * @return string[]|string[][] |
||
| 472 | */ |
||
| 473 | protected function absolute_path_to_relative ($path) { |
||
| 476 | /** |
||
| 477 | * @param string[][] $includes |
||
| 478 | * |
||
| 479 | * @return string[][] |
||
| 480 | */ |
||
| 481 | protected function add_versions_hash ($includes) { |
||
| 497 | /** |
||
| 498 | * @param Config $Config |
||
| 499 | */ |
||
| 500 | protected function add_includes_on_page_manually_added ($Config) { |
||
| 535 | } |
||
| 536 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.