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 |
||
| 41 | trait Includes { |
||
| 42 | protected $core_html = [0 => [], 1 => []]; |
||
| 43 | protected $core_js = [0 => [], 1 => []]; |
||
| 44 | protected $core_css = [0 => [], 1 => []]; |
||
| 45 | protected $core_config = ''; |
||
| 46 | protected $html = [0 => [], 1 => []]; |
||
| 47 | protected $js = [0 => [], 1 => []]; |
||
| 48 | protected $css = [0 => [], 1 => []]; |
||
| 49 | protected $config = ''; |
||
| 50 | /** |
||
| 51 | * Base name is used as prefix when creating CSS/JS/HTML cache files in order to avoid collisions when having several themes and languages |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $pcache_basename; |
||
| 55 | /** |
||
| 56 | * Including of Web Components |
||
| 57 | * |
||
| 58 | * @param string|string[] $add Path to including file, or code |
||
| 59 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
| 60 | * |
||
| 61 | * @return \cs\Page |
||
| 62 | */ |
||
| 63 | function html ($add, $mode = 'file') { |
||
| 66 | /** |
||
| 67 | * @param string|string[] $add |
||
| 68 | * @param string $mode |
||
| 69 | * @param bool $core |
||
| 70 | * |
||
| 71 | * @return \cs\Page |
||
| 72 | */ |
||
| 73 | protected function html_internal ($add, $mode = 'file', $core = false) { |
||
| 100 | /** |
||
| 101 | * Including of JavaScript |
||
| 102 | * |
||
| 103 | * @param string|string[] $add Path to including file, or code |
||
| 104 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
| 105 | * |
||
| 106 | * @return \cs\Page |
||
| 107 | */ |
||
| 108 | function js ($add, $mode = 'file') { |
||
| 111 | /** |
||
| 112 | * @param string|string[] $add |
||
| 113 | * @param string $mode |
||
| 114 | * @param bool $core |
||
| 115 | * |
||
| 116 | * @return \cs\Page |
||
| 117 | */ |
||
| 118 | protected function js_internal ($add, $mode = 'file', $core = false) { |
||
| 119 | if (!$add) { |
||
| 120 | return $this; |
||
| 121 | } |
||
| 122 | if (is_array($add)) { |
||
| 123 | foreach (array_filter($add) as $script) { |
||
| 124 | $this->js_internal($script, $mode, $core); |
||
| 125 | } |
||
| 126 | } else { |
||
| 127 | if ($core) { |
||
| 128 | $js = &$this->core_js; |
||
| 129 | } else { |
||
| 130 | $js = &$this->js; |
||
| 131 | } |
||
| 132 | if ($mode == 'file') { |
||
| 133 | $js[0][] = h::script( |
||
| 134 | [ |
||
| 135 | 'src' => $add |
||
| 136 | ] |
||
| 137 | ); |
||
| 138 | } elseif ($mode == 'code') { |
||
| 139 | $js[1][] = "$add\n"; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | return $this; |
||
| 143 | } |
||
| 144 | /** |
||
| 145 | * Including of CSS |
||
| 146 | * |
||
| 147 | * @param string|string[] $add Path to including file, or code |
||
| 148 | * @param string $mode Can be <b>file</b> or <b>code</b> |
||
| 149 | * |
||
| 150 | * @return \cs\Page |
||
| 151 | */ |
||
| 152 | function css ($add, $mode = 'file') { |
||
| 155 | /** |
||
| 156 | * @param string|string[] $add |
||
| 157 | * @param string $mode |
||
| 158 | * @param bool $core |
||
| 159 | * |
||
| 160 | * @return \cs\Page |
||
| 161 | */ |
||
| 162 | protected function css_internal ($add, $mode = 'file', $core = false) { |
||
| 190 | /** |
||
| 191 | * Add config on page to make it available on frontend |
||
| 192 | * |
||
| 193 | * @param mixed $config_structure Any scalar type or array |
||
| 194 | * @param string $target Target is property of `window` object where config will be inserted as value, nested properties like `cs.sub.prop` |
||
| 195 | * are supported and all nested properties are created on demand. It is recommended to use sub-properties of `cs` |
||
| 196 | * |
||
| 197 | * @return \cs\Page |
||
| 198 | */ |
||
| 199 | function config ($config_structure, $target) { |
||
| 202 | /** |
||
| 203 | * @param mixed $config_structure |
||
| 204 | * @param string $target |
||
| 205 | * @param bool $core |
||
| 206 | * |
||
| 207 | * @return \cs\Page |
||
| 208 | */ |
||
| 209 | protected function config_internal ($config_structure, $target, $core = false) { |
||
| 210 | $config = h::script( |
||
| 211 | json_encode($config_structure, JSON_UNESCAPED_UNICODE), |
||
| 212 | [ |
||
| 213 | 'target' => $target, |
||
| 214 | 'class' => 'cs-config', |
||
| 215 | 'type' => 'application/json' |
||
| 216 | ] |
||
| 217 | ); |
||
| 218 | if ($core) { |
||
| 219 | $this->core_config .= $config; |
||
| 220 | } else { |
||
| 221 | $this->config .= $config; |
||
| 222 | } |
||
| 223 | return $this; |
||
| 224 | } |
||
| 225 | /** |
||
| 226 | * Getting of HTML, JS and CSS includes |
||
| 227 | * |
||
| 228 | * @return \cs\Page |
||
| 229 | */ |
||
| 230 | protected function add_includes_on_page () { |
||
| 231 | $Config = Config::instance(true); |
||
| 232 | if (!$Config) { |
||
| 233 | return $this; |
||
| 234 | } |
||
| 235 | /** |
||
| 236 | * Base name for cache files |
||
| 237 | */ |
||
| 238 | $this->pcache_basename = "_{$this->theme}_".Language::instance()->clang; |
||
| 239 | /** |
||
| 240 | * Some JS configs required by system |
||
| 241 | */ |
||
| 242 | $this->add_system_configs(); |
||
| 243 | /** |
||
| 244 | * If CSS and JavaScript compression enabled |
||
| 245 | */ |
||
| 246 | if ($Config->core['cache_compress_js_css'] && !(admin_path() && current_module() == Config::SYSTEM_MODULE)) { |
||
| 247 | $includes = $this->get_includes_for_page_with_compression(); |
||
| 248 | } else { |
||
| 249 | /** |
||
| 250 | * Language translation is added explicitly only when compression is disabled, otherwise it will be in compressed JS file |
||
| 251 | */ |
||
| 252 | /** |
||
| 253 | * @var \cs\Page $this |
||
| 254 | */ |
||
| 255 | $this->config_internal(Language::instance(), 'cs.Language', true); |
||
| 256 | $includes = $this->get_includes_for_page_without_compression($Config); |
||
| 257 | } |
||
| 258 | $this->css_internal($includes['css'], 'file', true); |
||
| 259 | $this->js_internal($includes['js'], 'file', true); |
||
| 260 | $this->html_internal($includes['html'], 'file', true); |
||
| 261 | $this->add_includes_on_page_manually_added($Config); |
||
| 262 | return $this; |
||
| 263 | } |
||
| 264 | protected function add_system_configs () { |
||
| 265 | $Config = Config::instance(); |
||
| 266 | $Index = Index::instance(); |
||
| 267 | $Route = Route::instance(); |
||
| 268 | $User = User::instance(); |
||
| 269 | $current_module = current_module(); |
||
| 270 | /** |
||
| 271 | * @var \cs\_SERVER $_SERVER |
||
| 272 | */ |
||
| 273 | $this->config_internal( |
||
| 274 | [ |
||
| 275 | 'base_url' => $Config->base_url(), |
||
| 276 | 'current_base_url' => $Config->base_url().'/'.($Index->in_admin() ? 'admin/' : '').$current_module, |
||
| 277 | 'public_key' => Core::instance()->public_key, |
||
| 278 | 'module' => $current_module, |
||
| 279 | 'in_admin' => (int)$Index->in_admin(), |
||
| 280 | 'is_admin' => (int)$User->admin(), |
||
| 281 | 'is_user' => (int)$User->user(), |
||
| 282 | 'is_guest' => (int)$User->guest(), |
||
| 283 | 'password_min_length' => (int)$Config->core['password_min_length'], |
||
| 284 | 'password_min_strength' => (int)$Config->core['password_min_strength'], |
||
| 285 | 'debug' => (int)DEBUG, |
||
| 286 | 'cookie_prefix' => $Config->core['cookie_prefix'], |
||
| 287 | 'cookie_domain' => $Config->core['cookie_domain'][$Route->mirror_index], |
||
| 288 | 'protocol' => $_SERVER->protocol, |
||
| 289 | 'route' => $Route->route, |
||
| 290 | 'route_path' => $Route->path, |
||
| 291 | 'route_ids' => $Route->ids |
||
| 292 | ], |
||
| 293 | 'cs', |
||
| 294 | true |
||
| 295 | ); |
||
| 296 | if ($User->guest()) { |
||
| 297 | $this->config_internal(get_core_ml_text('rules'), 'cs.rules_text', true); |
||
| 298 | } |
||
| 299 | if ($User->admin()) { |
||
| 300 | $this->config_internal((int)$Config->core['simple_admin_mode'], 'cs.simple_admin_mode', true); |
||
| 301 | } |
||
| 302 | } |
||
| 303 | /** |
||
| 304 | * @return array[] |
||
|
1 ignored issue
–
show
|
|||
| 305 | */ |
||
| 306 | protected function get_includes_for_page_with_compression () { |
||
| 337 | /** |
||
| 338 | * @param Config $Config |
||
| 339 | * |
||
| 340 | * @return array[] |
||
| 341 | */ |
||
| 342 | protected function get_includes_for_page_without_compression ($Config) { |
||
| 366 | /** |
||
| 367 | * @param array $dependencies |
||
| 368 | * @param string $separator `+` or `/` |
||
| 369 | * |
||
| 370 | * @return array |
||
| 371 | */ |
||
| 372 | protected function get_includes_prepare ($dependencies, $separator) { |
||
| 391 | /** |
||
| 392 | * @param array $dependencies |
||
| 393 | * @param string $url |
||
| 394 | * @param string $separator `+` or `/` |
||
| 395 | * |
||
| 396 | * @return bool |
||
| 397 | */ |
||
| 398 | protected function get_includes_is_dependency ($dependencies, $url, $separator) { |
||
| 430 | /** |
||
| 431 | * @param Config $Config |
||
| 432 | */ |
||
| 433 | protected function add_includes_on_page_manually_added ($Config) { |
||
| 455 | /** |
||
| 456 | * Getting of HTML, JS and CSS files list to be included |
||
| 457 | * |
||
| 458 | * @param bool $absolute If <i>true</i> - absolute paths to files will be returned |
||
| 459 | * @param bool $with_disabled |
||
| 460 | * |
||
| 461 | * @return array |
||
| 462 | */ |
||
| 463 | protected function get_includes_list ($absolute = false, $with_disabled = false) { |
||
| 513 | /** |
||
| 514 | * Rebuilding of HTML, JS and CSS cache |
||
| 515 | * |
||
| 516 | * @return \cs\Page |
||
| 517 | */ |
||
| 518 | protected function rebuild_cache () { |
||
| 535 | /** |
||
| 536 | * Creates cached version of given HTML, JS and CSS files. |
||
| 537 | * Resulting file name consists of <b>$filename_prefix</b> and <b>$this->pcache_basename</b> |
||
| 538 | * |
||
| 539 | * @param string $filename_prefix |
||
| 540 | * @param array $includes Array of paths to files, may have keys: <b>css</b> and/or <b>js</b> and/or <b>html</b> |
||
| 541 | * |
||
| 542 | * @return array |
||
| 543 | */ |
||
| 544 | protected function create_cached_includes_files ($filename_prefix, $includes) { |
||
| 607 | /** |
||
| 608 | * Get dependencies of components between each other (only that contains some HTML, JS and CSS files) and mapping HTML, JS and CSS files to URL paths |
||
| 609 | * |
||
| 610 | * @param bool $with_disabled |
||
| 611 | * |
||
| 612 | * @return array[] [$dependencies, $includes_map] |
||
| 613 | */ |
||
| 614 | protected function includes_dependencies_and_map ($with_disabled = false) { |
||
| 693 | /** |
||
| 694 | * Process meta information and corresponding entries to dependencies and functionalities |
||
| 695 | * |
||
| 696 | * @param array $meta |
||
| 697 | * @param array $dependencies |
||
| 698 | * @param array $functionalities |
||
| 699 | */ |
||
| 700 | protected function process_meta ($meta, &$dependencies, &$functionalities) { |
||
| 740 | /** |
||
| 741 | * Process map structure, fill includes map and remove files from list of all includes (remaining files will be included on all pages) |
||
| 742 | * |
||
| 743 | * @param array $map |
||
| 744 | * @param string $includes_dir |
||
| 745 | * @param array $includes_map |
||
| 746 | * @param array $all_includes |
||
| 747 | */ |
||
| 748 | protected function process_map ($map, $includes_dir, &$includes_map, &$all_includes) { |
||
| 779 | /** |
||
| 780 | * Replace functionalities by real packages names, take into account recursive dependencies |
||
| 781 | * |
||
| 782 | * @param array $dependencies |
||
| 783 | * @param array $functionalities |
||
| 784 | * |
||
| 785 | * @return array |
||
| 786 | */ |
||
| 787 | protected function normalize_dependencies ($dependencies, $functionalities) { |
||
| 836 | /** |
||
| 837 | * Includes array is composed from dependencies and sometimes dependencies doesn't have any files, so we'll clean that |
||
| 838 | * |
||
| 839 | * @param array $dependencies |
||
| 840 | * @param array $includes_map |
||
| 841 | * |
||
| 842 | * @return array |
||
| 843 | */ |
||
| 844 | protected function clean_includes_arrays_without_files ($dependencies, $includes_map) { |
||
| 855 | } |
||
| 856 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.