Complex classes like Assets 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 Assets, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | trait Assets { |
||
| 31 | protected $extension_to_as = [ |
||
| 32 | 'jpeg' => 'image', |
||
| 33 | 'jpe' => 'image', |
||
| 34 | 'jpg' => 'image', |
||
| 35 | 'gif' => 'image', |
||
| 36 | 'png' => 'image', |
||
| 37 | 'svg' => 'image', |
||
| 38 | 'svgz' => 'image', |
||
| 39 | 'woff' => 'font', |
||
| 40 | //'woff2' => 'font', |
||
| 41 | 'css' => 'style', |
||
| 42 | 'js' => 'script', |
||
| 43 | 'html' => 'document' |
||
| 44 | ]; |
||
| 45 | /** |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected $core_html; |
||
| 49 | /** |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | protected $core_js; |
||
| 53 | /** |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $core_css; |
||
| 57 | /** |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | protected $core_config; |
||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $html; |
||
| 65 | /** |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | protected $js; |
||
| 69 | /** |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | protected $css; |
||
| 73 | /** |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | protected $config; |
||
| 77 | 34 | protected function init_assets () { |
|
| 87 | /** |
||
| 88 | * @param string|string[] $add |
||
| 89 | * |
||
| 90 | * @return \cs\Page |
||
|
|
|||
| 91 | */ |
||
| 92 | 4 | protected function core_html ($add) { |
|
| 95 | /** |
||
| 96 | * @param string|string[] $add |
||
| 97 | * |
||
| 98 | * @return \cs\Page |
||
| 99 | */ |
||
| 100 | 4 | protected function core_js ($add) { |
|
| 103 | /** |
||
| 104 | * @param string|string[] $add |
||
| 105 | * |
||
| 106 | * @return \cs\Page |
||
| 107 | */ |
||
| 108 | 4 | protected function core_css ($add) { |
|
| 111 | /** |
||
| 112 | * Including of Web Components |
||
| 113 | * |
||
| 114 | * @param string|string[] $add Path to including file, or code |
||
| 115 | * |
||
| 116 | * @return \cs\Page |
||
| 117 | */ |
||
| 118 | 2 | public function html ($add) { |
|
| 121 | /** |
||
| 122 | * Including of JavaScript |
||
| 123 | * |
||
| 124 | * @param string|string[] $add Path to including file, or code |
||
| 125 | * |
||
| 126 | * @return \cs\Page |
||
| 127 | */ |
||
| 128 | 2 | public function js ($add) { |
|
| 131 | /** |
||
| 132 | * Including of CSS |
||
| 133 | * |
||
| 134 | * @param string|string[] $add Path to including file, or code |
||
| 135 | * |
||
| 136 | * @return \cs\Page |
||
| 137 | */ |
||
| 138 | 2 | public function css ($add) { |
|
| 141 | /** |
||
| 142 | * @param string $what |
||
| 143 | * @param string|string[] $add |
||
| 144 | * @param bool $core |
||
| 145 | * |
||
| 146 | * @return \cs\Page |
||
| 147 | */ |
||
| 148 | 4 | protected function include_common ($what, $add, $core) { |
|
| 165 | /** |
||
| 166 | * Add config on page to make it available on frontend |
||
| 167 | * |
||
| 168 | * @param mixed $config_structure Any scalar type or array |
||
| 169 | * @param string $target Target is property of `window` object where config will be inserted as value, nested properties like `cs.sub.prop` |
||
| 170 | * are supported and all nested properties are created on demand. It is recommended to use sub-properties of `cs` |
||
| 171 | * |
||
| 172 | * @return \cs\Page |
||
| 173 | */ |
||
| 174 | 4 | public function config ($config_structure, $target) { |
|
| 177 | /** |
||
| 178 | * @param mixed $config_structure |
||
| 179 | * @param string $target |
||
| 180 | * @param bool $core |
||
| 181 | * |
||
| 182 | * @return \cs\Page |
||
| 183 | */ |
||
| 184 | 4 | protected function config_internal ($config_structure, $target, $core = false) { |
|
| 200 | /** |
||
| 201 | * Getting of HTML, JS and CSS assets |
||
| 202 | * |
||
| 203 | * @return \cs\Page |
||
| 204 | */ |
||
| 205 | 4 | protected function add_assets_on_page () { |
|
| 252 | /** |
||
| 253 | * @param Config $Config |
||
| 254 | * @param Request $Request |
||
| 255 | * |
||
| 256 | * @return bool |
||
| 257 | */ |
||
| 258 | 4 | protected function page_compression_usage ($Config, $Request) { |
|
| 261 | /** |
||
| 262 | * Add JS polyfills for IE/Edge |
||
| 263 | */ |
||
| 264 | 4 | protected function edge () { |
|
| 272 | /** |
||
| 273 | * Hack: Add WebComponents Polyfill for browsers without native Shadow DOM support |
||
| 274 | * |
||
| 275 | * @param Request $Request |
||
| 276 | * @param Config $Config |
||
| 277 | * @param bool $with_compression |
||
| 278 | */ |
||
| 279 | 4 | protected function webcomponents_polyfill ($Request, $Config, $with_compression) { |
|
| 290 | /** |
||
| 291 | * @param Config $Config |
||
| 292 | * @param string $content |
||
| 293 | */ |
||
| 294 | 4 | protected function add_script_imports_to_document ($Config, $content) { |
|
| 301 | /** |
||
| 302 | * @param Request $Request |
||
| 303 | * |
||
| 304 | * @return array[] |
||
| 305 | */ |
||
| 306 | 4 | protected function get_assets_and_preload_resource_for_page_with_compression ($Request) { |
|
| 318 | /** |
||
| 319 | * @param array $dependencies |
||
| 320 | * @param string[][] $assets_map |
||
| 321 | * @param Request $Request |
||
| 322 | * |
||
| 323 | * @return string[][] |
||
| 324 | */ |
||
| 325 | 4 | protected function get_normalized_assets ($dependencies, $assets_map, $Request) { |
|
| 365 | /** |
||
| 366 | * @param array $dependencies |
||
| 367 | * @param string $url |
||
| 368 | * @param Request $Request |
||
| 369 | * |
||
| 370 | * @return false|string |
||
| 371 | */ |
||
| 372 | 4 | protected function get_dependency_component ($dependencies, $url, $Request) { |
|
| 384 | /** |
||
| 385 | * @param Config $Config |
||
| 386 | * @param Request $Request |
||
| 387 | * |
||
| 388 | * @return string[][] |
||
| 389 | */ |
||
| 390 | 2 | protected function get_assets_for_page_without_compression ($Config, $Request) { |
|
| 396 | /** |
||
| 397 | * @param string[][] $assets |
||
| 398 | * |
||
| 399 | * @return string[][] |
||
| 400 | */ |
||
| 401 | 2 | protected function add_versions_hash ($assets) { |
|
| 417 | /** |
||
| 418 | * @param Config $Config |
||
| 419 | * @param Request $Request |
||
| 420 | * @param string[] $preload |
||
| 421 | */ |
||
| 422 | 4 | protected function add_assets_on_page_manually_added ($Config, $Request, $preload) { |
|
| 436 | /** |
||
| 437 | * @param Config $Config |
||
| 438 | * @param Request $Request |
||
| 439 | * @param string[] $preload |
||
| 440 | */ |
||
| 441 | 2 | protected function add_assets_on_page_manually_added_normal ($Config, $Request, $preload) { |
|
| 459 | /** |
||
| 460 | * @param string[] $preload |
||
| 461 | * @param Request $Request |
||
| 462 | */ |
||
| 463 | 4 | protected function add_preload ($preload, $Request) { |
|
| 476 | /** |
||
| 477 | * @param Config $Config |
||
| 478 | * @param Request $Request |
||
| 479 | */ |
||
| 480 | 4 | protected function add_assets_on_page_manually_added_frontend_load_optimization ($Config, $Request) { |
|
| 509 | } |
||
| 510 |
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.