Complex classes like Packages_dependencies 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 Packages_dependencies, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Packages_dependencies { |
||
| 20 | /** |
||
| 21 | * Check dependencies for new component (during installation/updating/enabling) |
||
| 22 | * |
||
| 23 | * @param array $meta `meta.json` contents of target component |
||
| 24 | * @param bool $update Is this for updating component to newer version (`$meta` will correspond to the version that components is going to be updated to) |
||
| 25 | * |
||
| 26 | * @return array |
||
| 27 | */ |
||
| 28 | 2 | public static function get_dependencies ($meta, $update = false) { |
|
| 84 | /** |
||
| 85 | * @param array $dependencies |
||
| 86 | * @param array $meta |
||
| 87 | * @param array $component_meta |
||
| 88 | * @param bool $update |
||
| 89 | */ |
||
| 90 | 2 | protected static function common_checks (&$dependencies, &$meta, $component_meta, $update) { |
|
| 151 | /** |
||
| 152 | * Check whether there is available supported DB driver |
||
| 153 | * |
||
| 154 | * @param string[] $db_support |
||
| 155 | * |
||
| 156 | * @return bool |
||
| 157 | */ |
||
| 158 | 2 | protected static function check_dependencies_db ($db_support) { |
|
| 177 | /** |
||
| 178 | * Check whether there is available supported Storage driver |
||
| 179 | * |
||
| 180 | * @param string[] $storage_support |
||
| 181 | * |
||
| 182 | * @return bool |
||
| 183 | */ |
||
| 184 | 2 | protected static function check_dependencies_storage ($storage_support) { |
|
| 203 | /** |
||
| 204 | * Check if two both components are the same |
||
| 205 | * |
||
| 206 | * @param array $new_meta `meta.json` content of new component |
||
| 207 | * @param array $existing_meta `meta.json` content of existing component |
||
| 208 | * |
||
| 209 | * @return bool |
||
| 210 | */ |
||
| 211 | 2 | protected static function check_dependencies_are_the_same ($new_meta, $existing_meta) { |
|
| 216 | /** |
||
| 217 | * Check for functionality provided by other components |
||
| 218 | * |
||
| 219 | * @param array $new_meta `meta.json` content of new component |
||
| 220 | * @param array $existing_meta `meta.json` content of existing component |
||
| 221 | * |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | 2 | protected static function also_provided_by ($new_meta, $existing_meta) { |
|
| 227 | /** |
||
| 228 | * Check whether other component is required and have satisfactory version |
||
| 229 | * |
||
| 230 | * @param array $new_meta `meta.json` content of new component |
||
| 231 | * @param array $existing_meta `meta.json` content of existing component |
||
| 232 | * |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | 2 | protected static function check_requirement ($new_meta, $existing_meta) { |
|
| 246 | /** |
||
| 247 | * Check whether other component is required and have satisfactory version |
||
| 248 | * |
||
| 249 | * @param array $requirements |
||
| 250 | * @param string $component |
||
| 251 | * @param string $version |
||
| 252 | * @param bool $should_satisfy `true` for conflicts detection and `false` for requirements to fail |
||
| 253 | * |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | 2 | protected static function check_conflicts_or_requirements ($requirements, $component, $version, $should_satisfy) { |
|
| 274 | /** |
||
| 275 | * Check for if component conflicts other components |
||
| 276 | * |
||
| 277 | * @param array $new_meta `meta.json` content of new component |
||
| 278 | * @param array $existing_meta `meta.json` content of existing component |
||
| 279 | * |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | 2 | protected static function check_conflicts ($new_meta, $existing_meta) { |
|
| 293 | /** |
||
| 294 | * @param array $meta_from |
||
| 295 | * @param array $meta_to |
||
| 296 | * |
||
| 297 | * @return array |
||
| 298 | */ |
||
| 299 | 2 | protected static function conflicts_one_step ($meta_from, $meta_to) { |
|
| 313 | /** |
||
| 314 | * Check whether package is currently used by any other package (during uninstalling/disabling) |
||
| 315 | * |
||
| 316 | * @param array $meta `meta.json` contents of target component |
||
| 317 | * |
||
| 318 | * @return string[][] Empty array if dependencies are fine or array with optional key `modules` that contain array of dependent packages |
||
| 319 | */ |
||
| 320 | 2 | public static function get_dependent_packages ($meta) { |
|
| 339 | /** |
||
| 340 | * @param array $meta |
||
| 341 | * @param string $module |
||
| 342 | * @param int $active |
||
| 343 | * |
||
| 344 | * @return bool |
||
| 345 | */ |
||
| 346 | 2 | protected static function is_used_by ($meta, $module, $active) { |
|
| 366 | /** |
||
| 367 | * Normalize structure of `meta.json` |
||
| 368 | * |
||
| 369 | * Addition necessary items if they are not present and casting some string values to arrays in order to decrease number of checks in further code |
||
| 370 | * |
||
| 371 | * @param array $meta |
||
| 372 | * |
||
| 373 | * @return array mixed |
||
| 374 | */ |
||
| 375 | 2 | protected static function normalize_meta ($meta) { |
|
| 384 | /** |
||
| 385 | * Function for normalization of dependencies structure |
||
| 386 | * |
||
| 387 | * @param array|string $dependence_structure |
||
| 388 | * |
||
| 389 | * @return array |
||
| 390 | */ |
||
| 391 | 2 | protected static function dep_normal ($dependence_structure) { |
|
| 403 | } |
||
| 404 |