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 | /** |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | protected $core_html; |
||
| 41 | /** |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $core_js; |
||
| 45 | /** |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected $core_css; |
||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | protected $core_config; |
||
| 53 | /** |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $html; |
||
| 57 | /** |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected $js; |
||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $css; |
||
| 65 | /** |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | protected $config; |
||
| 69 | /** |
||
| 70 | * Base name is used as prefix when creating CSS/JS/HTML cache files in order to avoid collisions when having several themes and languages |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | protected $pcache_basename_path; |
||
| 74 | protected function init_includes () { |
||
| 75 | $this->core_html = ['path' => [], 'plain' => '']; |
||
| 76 | $this->core_js = ['path' => [], 'plain' => '']; |
||
| 77 | $this->core_css = ['path' => [], 'plain' => '']; |
||
| 78 | $this->core_config = ''; |
||
| 79 | $this->html = ['path' => [], 'plain' => '']; |
||
| 80 | $this->js = ['path' => [], 'plain' => '']; |
||
| 81 | $this->css = ['path' => [], 'plain' => '']; |
||
| 82 | $this->config = ''; |
||
| 83 | $this->pcache_basename_path = ''; |
||
| 84 | } |
||
| 85 | /** |
||
| 86 | * Including of Web Components |
||
| 87 | * |
||
| 88 | * @param string|string[] $add Path to including file, or code |
||
| 89 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
| 90 | * |
||
| 91 | * @return \cs\Page |
||
| 92 | */ |
||
| 93 | function html ($add, $mode = 'file') { |
||
| 94 | return $this->html_internal($add, $mode); |
||
| 95 | } |
||
| 96 | /** |
||
| 97 | * @param string|string[] $add |
||
| 98 | * @param string $mode |
||
| 99 | * @param bool $core |
||
| 100 | * |
||
| 101 | * @return \cs\Page |
||
| 102 | */ |
||
| 103 | protected function html_internal ($add, $mode = 'file', $core = false) { |
||
| 104 | return $this->include_common('html', $add, $mode, $core); |
||
| 105 | } |
||
| 106 | /** |
||
| 107 | * Including of JavaScript |
||
| 108 | * |
||
| 109 | * @param string|string[] $add Path to including file, or code |
||
| 110 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
| 111 | * |
||
| 112 | * @return \cs\Page |
||
| 113 | */ |
||
| 114 | function js ($add, $mode = 'file') { |
||
| 117 | /** |
||
| 118 | * @param string|string[] $add |
||
| 119 | * @param string $mode |
||
| 120 | * @param bool $core |
||
| 121 | * |
||
| 122 | * @return \cs\Page |
||
| 123 | */ |
||
| 124 | protected function js_internal ($add, $mode = 'file', $core = false) { |
||
| 127 | /** |
||
| 128 | * Including of CSS |
||
| 129 | * |
||
| 130 | * @param string|string[] $add Path to including file, or code |
||
| 131 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
| 132 | * |
||
| 133 | * @return \cs\Page |
||
| 134 | */ |
||
| 135 | function css ($add, $mode = 'file') { |
||
| 138 | /** |
||
| 139 | * @param string|string[] $add |
||
| 140 | * @param string $mode |
||
| 141 | * @param bool $core |
||
| 142 | * |
||
| 143 | * @return \cs\Page |
||
| 144 | */ |
||
| 145 | protected function css_internal ($add, $mode = 'file', $core = false) { |
||
| 148 | /** |
||
| 149 | * @param string $what |
||
| 150 | * @param string|string[] $add |
||
| 151 | * @param string $mode |
||
| 152 | * @param bool $core |
||
| 153 | * |
||
| 154 | * @return \cs\Page |
||
| 155 | */ |
||
| 156 | protected function include_common ($what, $add, $mode, $core) { |
||
| 177 | /** |
||
| 178 | * Add config on page to make it available on frontend |
||
| 179 | * |
||
| 180 | * @param mixed $config_structure Any scalar type or array |
||
| 181 | * @param string $target Target is property of `window` object where config will be inserted as value, nested properties like `cs.sub.prop` |
||
| 182 | * are supported and all nested properties are created on demand. It is recommended to use sub-properties of `cs` |
||
| 183 | * |
||
| 184 | * @return \cs\Page |
||
| 185 | */ |
||
| 186 | function config ($config_structure, $target) { |
||
| 189 | /** |
||
| 190 | * @param mixed $config_structure |
||
| 191 | * @param string $target |
||
| 192 | * @param bool $core |
||
| 193 | * |
||
| 194 | * @return \cs\Page |
||
| 195 | */ |
||
| 196 | protected function config_internal ($config_structure, $target, $core = false) { |
||
| 212 | /** |
||
| 213 | * Getting of HTML, JS and CSS includes |
||
| 214 | * |
||
| 215 | * @return \cs\Page |
||
| 216 | */ |
||
| 217 | protected function add_includes_on_page () { |
||
| 254 | /** |
||
| 255 | * @param string[]|string[][] $path |
||
| 256 | * |
||
| 257 | * @return string[]|string[][] |
||
| 258 | */ |
||
| 259 | protected function absolute_path_to_relative ($path) { |
||
| 262 | /** |
||
| 263 | * Add JS polyfills for IE/Edge |
||
| 264 | */ |
||
| 265 | protected function ie_edge () { |
||
| 275 | /** |
||
| 276 | * Hack: Add WebComponents Polyfill for browsers without native Shadow DOM support |
||
| 277 | * |
||
| 278 | * @param Request $Request |
||
| 279 | * @param bool $with_compression |
||
| 280 | */ |
||
| 281 | protected function webcomponents_polyfill ($Request, $with_compression) { |
||
| 328 | /** |
||
| 329 | * @param Config $Config |
||
| 330 | * |
||
| 331 | * @return string[][] |
||
| 332 | */ |
||
| 333 | protected function get_includes_for_page_with_compression ($Config) { |
||
| 356 | /** |
||
| 357 | * @param array $dependencies |
||
| 358 | * @param string[][] $includes_map |
||
| 359 | * @param string $separator `+` or `/` |
||
| 360 | * |
||
| 361 | * @return array |
||
| 362 | */ |
||
| 363 | protected function get_normalized_includes ($dependencies, $includes_map, $separator) { |
||
| 392 | /** |
||
| 393 | * @param array $dependencies |
||
| 394 | * @param string $url |
||
| 395 | * @param string $separator `+` or `/` |
||
| 396 | * |
||
| 397 | * @return bool |
||
| 398 | */ |
||
| 399 | protected function is_dependency ($dependencies, $url, $separator) { |
||
| 411 | /** |
||
| 412 | * @param Config $Config |
||
| 413 | * |
||
| 414 | * @return string[][] |
||
| 415 | */ |
||
| 416 | protected function get_includes_for_page_without_compression ($Config) { |
||
| 422 | /** |
||
| 423 | * @param string[][] $includes |
||
| 424 | * |
||
| 425 | * @return string[][] |
||
| 426 | */ |
||
| 427 | protected function add_versions_hash ($includes) { |
||
| 443 | /** |
||
| 444 | * @param Config $Config |
||
| 445 | */ |
||
| 446 | protected function add_includes_on_page_manually_added ($Config) { |
||
| 481 | } |
||
| 482 |