Complex classes like ExtraPackage 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 ExtraPackage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class ExtraPackage |
||
| 34 | { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var Composer $composer |
||
| 38 | */ |
||
| 39 | protected $composer; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var Logger $logger |
||
| 43 | */ |
||
| 44 | protected $logger; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string $path |
||
| 48 | */ |
||
| 49 | protected $path; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array $json |
||
| 53 | */ |
||
| 54 | protected $json; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var CompletePackage $package |
||
| 58 | */ |
||
| 59 | protected $package; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var VersionParser $versionParser |
||
| 63 | */ |
||
| 64 | protected $versionParser; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param string $path Path to composer.json file |
||
| 68 | * @param Composer $composer |
||
| 69 | * @param Logger $logger |
||
| 70 | */ |
||
| 71 | 170 | public function __construct($path, Composer $composer, Logger $logger) |
|
| 80 | |||
| 81 | public function getName() |
||
| 85 | |||
| 86 | 165 | /** |
|
| 87 | * Get list of additional packages to include if precessing recursively. |
||
| 88 | 165 | * |
|
| 89 | 165 | * @return array |
|
| 90 | */ |
||
| 91 | public function getIncludes() |
||
| 96 | |||
| 97 | 165 | /** |
|
| 98 | * Get list of additional packages to require if precessing recursively. |
||
| 99 | 165 | * |
|
| 100 | 165 | * @return array |
|
| 101 | */ |
||
| 102 | public function getRequires() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Read the contents of a composer.json style file into an array. |
||
| 110 | * |
||
| 111 | * The package contents are fixed up to be usable to create a Package |
||
| 112 | * object by providing dummy "name" and "version" values if they have not |
||
| 113 | * been provided in the file. This is consistent with the default root |
||
| 114 | 170 | * package loading behavior of Composer. |
|
| 115 | * |
||
| 116 | 170 | * @param string $path |
|
| 117 | 170 | * @return array |
|
| 118 | 170 | */ |
|
| 119 | 160 | protected function readPackageJson($path) |
|
| 132 | 170 | ||
| 133 | /** |
||
| 134 | 170 | * @param array $json |
|
| 135 | 170 | * @return CompletePackage |
|
| 136 | */ |
||
| 137 | protected function loadPackage(array $json) |
||
| 151 | |||
| 152 | /** |
||
| 153 | 170 | * Merge this package into a RootPackageInterface |
|
| 154 | * |
||
| 155 | 170 | * @param RootPackageInterface $root |
|
| 156 | * @param PluginState $state |
||
| 157 | 170 | */ |
|
| 158 | public function mergeInto(RootPackageInterface $root, PluginState $state) |
||
| 182 | |||
| 183 | /** |
||
| 184 | 165 | * Merge just the dev portion into a RootPackageInterface |
|
| 185 | * |
||
| 186 | 165 | * @param RootPackageInterface $root |
|
| 187 | 165 | * @param PluginState $state |
|
| 188 | 165 | */ |
|
| 189 | 165 | public function mergeDevInto(RootPackageInterface $root, PluginState $state) |
|
| 195 | |||
| 196 | /** |
||
| 197 | 170 | * Add a collection of repositories described by the given configuration |
|
| 198 | * to the given package and the global repository manager. |
||
| 199 | 170 | * |
|
| 200 | 150 | * @param RootPackageInterface $root |
|
| 201 | */ |
||
| 202 | 20 | protected function prependRepositories(RootPackageInterface $root) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Merge require or require-dev into a RootPackageInterface |
||
| 232 | 170 | * |
|
| 233 | * @param string $type 'require' or 'require-dev' |
||
| 234 | * @param RootPackageInterface $root |
||
| 235 | * @param PluginState $state |
||
| 236 | */ |
||
| 237 | 170 | protected function mergeRequires( |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Merge two collections of package links and collect duplicates for |
||
| 270 | * subsequent processing. |
||
| 271 | * |
||
| 272 | 95 | * @param string $type 'require' or 'require-dev' |
|
| 273 | * @param array $origin Primary collection |
||
| 274 | * @param array $merge Additional collection |
||
| 275 | * @param PluginState $state |
||
| 276 | * @return array Merged collection |
||
| 277 | */ |
||
| 278 | 95 | protected function mergeOrDefer( |
|
| 309 | 170 | ||
| 310 | /** |
||
| 311 | 170 | * Merge autoload or autoload-dev into a RootPackageInterface |
|
| 312 | 170 | * |
|
| 313 | * @param string $type 'autoload' or 'devAutoload' |
||
| 314 | 170 | * @param RootPackageInterface $root |
|
| 315 | 170 | */ |
|
| 316 | 160 | protected function mergeAutoload($type, RootPackageInterface $root) |
|
| 332 | |||
| 333 | 15 | /** |
|
| 334 | * Fix a collection of paths that are relative to this package to be |
||
| 335 | 15 | * relative to the base package. |
|
| 336 | 15 | * |
|
| 337 | * @param array $paths |
||
| 338 | 15 | * @return array |
|
| 339 | 15 | */ |
|
| 340 | protected function fixRelativePaths(array $paths) |
||
| 353 | |||
| 354 | 95 | /** |
|
| 355 | * Extract and merge stability flags from the given collection of |
||
| 356 | * requires and merge them into a RootPackageInterface |
||
| 357 | * |
||
| 358 | 95 | * @param RootPackageInterface $root |
|
| 359 | 95 | * @param array $requires |
|
| 360 | */ |
||
| 361 | 95 | protected function mergeStabilityFlags( |
|
| 375 | |||
| 376 | 170 | /** |
|
| 377 | 170 | * Merge package links of the given type into a RootPackageInterface |
|
| 378 | 170 | * |
|
| 379 | * @param string $type 'conflict', 'replace' or 'provide' |
||
| 380 | 170 | * @param RootPackageInterface $root |
|
| 381 | 170 | */ |
|
| 382 | 30 | protected function mergePackageLinks($type, RootPackageInterface $root) |
|
| 405 | 170 | ||
| 406 | 170 | /** |
|
| 407 | 15 | * Merge suggested packages into a RootPackageInterface |
|
| 408 | 15 | * |
|
| 409 | 15 | * @param RootPackageInterface $root |
|
| 410 | */ |
||
| 411 | 15 | protected function mergeSuggests(RootPackageInterface $root) |
|
| 422 | |||
| 423 | 170 | /** |
|
| 424 | 170 | * Merge extra config into a RootPackageInterface |
|
| 425 | 170 | * |
|
| 426 | 140 | * @param RootPackageInterface $root |
|
| 427 | * @param PluginState $state |
||
| 428 | */ |
||
| 429 | 30 | public function mergeExtra(RootPackageInterface $root, PluginState $state) |
|
| 461 | |||
| 462 | 170 | /** |
|
| 463 | 170 | * Merge scripts config into a RootPackageInterface |
|
| 464 | 150 | * |
|
| 465 | * @param RootPackageInterface $root |
||
| 466 | * @param PluginState $state |
||
| 467 | 20 | */ |
|
| 468 | 20 | public function mergeScripts(RootPackageInterface $root, PluginState $state) |
|
| 488 | |||
| 489 | 30 | /** |
|
| 490 | * Merges two arrays either via arrayMergeDeep or via array_merge. |
||
| 491 | 30 | * |
|
| 492 | 10 | * @param bool $mergeDeep |
|
| 493 | * @param array $array1 |
||
| 494 | * @param array $array2 |
||
| 495 | 20 | * @return array |
|
| 496 | */ |
||
| 497 | public static function mergeExtraArray($mergeDeep, $array1, $array2) |
||
| 505 | |||
| 506 | /** |
||
| 507 | 110 | * Update Links with a 'self.version' constraint with the root package's |
|
| 508 | * version. |
||
| 509 | * |
||
| 510 | * @param string $type Link type |
||
| 511 | * @param array $links |
||
| 512 | 110 | * @param RootPackageInterface $root |
|
| 513 | 110 | * @return array |
|
| 514 | 110 | */ |
|
| 515 | 110 | protected function replaceSelfVersionDependencies( |
|
| 557 | |||
| 558 | /** |
||
| 559 | * Get a full featured Package from a RootPackageInterface. |
||
| 560 | * |
||
| 561 | * In Composer versions before 599ad77 the RootPackageInterface only |
||
| 562 | * defines a sub-set of operations needed by composer-merge-plugin and |
||
| 563 | * RootAliasPackage only implemented those methods defined by the |
||
| 564 | * interface. Most of the unimplemented methods in RootAliasPackage can be |
||
| 565 | * worked around because the getter methods that are implemented proxy to |
||
| 566 | 170 | * the aliased package which we can modify by unwrapping. The exception |
|
| 567 | * being modifying the 'conflicts', 'provides' and 'replaces' collections. |
||
| 568 | * We have no way to actually modify those collections unfortunately in |
||
| 569 | * older versions of Composer. |
||
| 570 | * |
||
| 571 | * @param RootPackageInterface $root |
||
| 572 | * @param string $method Method needed |
||
| 573 | * @return RootPackageInterface|RootPackage |
||
| 574 | */ |
||
| 575 | public static function unwrapIfNeeded( |
||
| 590 | 170 | ||
| 591 | 170 | /** |
|
| 592 | 170 | * Update the root packages reference information. |
|
| 593 | 170 | * |
|
| 594 | 170 | * @param RootPackageInterface $root |
|
| 595 | 170 | */ |
|
| 596 | 170 | protected function mergeReferences(RootPackageInterface $root) |
|
| 613 | |||
| 614 | 170 | /** |
|
| 615 | 65 | * Extract vcs revision from version constraint (dev-master#abc123. |
|
| 616 | 65 | * |
|
| 617 | * @param array $requires |
||
| 618 | 65 | * @param array $references |
|
| 619 | * @return array |
||
| 620 | 65 | * @see RootPackageLoader::extractReferences() |
|
| 621 | 15 | */ |
|
| 622 | 15 | protected function extractReferences(array $requires, array $references) |
|
| 638 | } |
||
| 639 | // vim:sw=4:ts=4:sts=4:et: |
||
| 640 |