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 | /** |
||
| 82 | * Get list of additional packages to include if precessing recursively. |
||
| 83 | * |
||
| 84 | * @return array |
||
| 85 | */ |
||
| 86 | 165 | public function getIncludes() |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Get list of additional packages to require if precessing recursively. |
||
| 94 | * |
||
| 95 | * @return array |
||
| 96 | */ |
||
| 97 | 165 | public function getRequires() |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Read the contents of a composer.json style file into an array. |
||
| 105 | * |
||
| 106 | * The package contents are fixed up to be usable to create a Package |
||
| 107 | * object by providing dummy "name" and "version" values if they have not |
||
| 108 | * been provided in the file. This is consistent with the default root |
||
| 109 | * package loading behavior of Composer. |
||
| 110 | * |
||
| 111 | * @param string $path |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | 170 | protected function readPackageJson($path) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * @param array $json |
||
| 130 | * @return CompletePackage |
||
| 131 | */ |
||
| 132 | 170 | protected function loadPackage(array $json) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Merge this package into a RootPackageInterface |
||
| 149 | * |
||
| 150 | * @param RootPackageInterface $root |
||
| 151 | * @param PluginState $state |
||
| 152 | */ |
||
| 153 | 170 | public function mergeInto(RootPackageInterface $root, PluginState $state) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Merge just the dev portion into a RootPackageInterface |
||
| 180 | * |
||
| 181 | * @param RootPackageInterface $root |
||
| 182 | * @param PluginState $state |
||
| 183 | */ |
||
| 184 | 165 | public function mergeDevInto(RootPackageInterface $root, PluginState $state) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Add a collection of repositories described by the given configuration |
||
| 193 | * to the given package and the global repository manager. |
||
| 194 | * |
||
| 195 | * @param RootPackageInterface $root |
||
| 196 | */ |
||
| 197 | 170 | protected function prependRepositories(RootPackageInterface $root) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Merge require or require-dev into a RootPackageInterface |
||
| 230 | * |
||
| 231 | * @param string $type 'require' or 'require-dev' |
||
| 232 | 170 | * @param RootPackageInterface $root |
|
| 233 | * @param PluginState $state |
||
| 234 | */ |
||
| 235 | protected function mergeRequires( |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Merge two collections of package links and collect duplicates for |
||
| 267 | * subsequent processing. |
||
| 268 | * |
||
| 269 | * @param string $type 'require' or 'require-dev' |
||
| 270 | * @param array $origin Primary collection |
||
| 271 | * @param array $merge Additional collection |
||
| 272 | 95 | * @param PluginState $state |
|
| 273 | * @return array Merged collection |
||
| 274 | */ |
||
| 275 | protected function mergeOrDefer( |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Merge autoload or autoload-dev into a RootPackageInterface |
||
| 308 | * |
||
| 309 | 170 | * @param string $type 'autoload' or 'devAutoload' |
|
| 310 | * @param RootPackageInterface $root |
||
| 311 | 170 | */ |
|
| 312 | 170 | protected function mergeAutoload($type, RootPackageInterface $root) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Fix a collection of paths that are relative to this package to be |
||
| 331 | * relative to the base package. |
||
| 332 | * |
||
| 333 | 15 | * @param array $paths |
|
| 334 | * @return array |
||
| 335 | 15 | */ |
|
| 336 | 15 | protected function fixRelativePaths(array $paths) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Extract and merge stability flags from the given collection of |
||
| 352 | * requires and merge them into a RootPackageInterface |
||
| 353 | * |
||
| 354 | 95 | * @param RootPackageInterface $root |
|
| 355 | * @param array $requires |
||
| 356 | */ |
||
| 357 | protected function mergeStabilityFlags( |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Merge package links of the given type into a RootPackageInterface |
||
| 373 | * |
||
| 374 | 170 | * @param string $type 'conflict', 'replace' or 'provide' |
|
| 375 | * @param RootPackageInterface $root |
||
| 376 | 170 | */ |
|
| 377 | 170 | protected function mergePackageLinks($type, RootPackageInterface $root) |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Merge suggested packages into a RootPackageInterface |
||
| 403 | 170 | * |
|
| 404 | * @param RootPackageInterface $root |
||
| 405 | 170 | */ |
|
| 406 | 170 | protected function mergeSuggests(RootPackageInterface $root) |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Merge extra config into a RootPackageInterface |
||
| 420 | * |
||
| 421 | 170 | * @param RootPackageInterface $root |
|
| 422 | * @param PluginState $state |
||
| 423 | 170 | */ |
|
| 424 | 170 | public function mergeExtra(RootPackageInterface $root, PluginState $state) |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Merge scripts config into a RootPackageInterface |
||
| 459 | * |
||
| 460 | 170 | * @param RootPackageInterface $root |
|
| 461 | * @param PluginState $state |
||
| 462 | 170 | */ |
|
| 463 | 170 | public function mergeScripts(RootPackageInterface $root, PluginState $state) |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Merges two arrays either via arrayMergeDeep or via array_merge. |
||
| 486 | * |
||
| 487 | * @param bool $mergeDeep |
||
| 488 | * @param array $array1 |
||
| 489 | 30 | * @param array $array2 |
|
| 490 | * @return array |
||
| 491 | 30 | */ |
|
| 492 | 10 | public static function mergeExtraArray($mergeDeep, $array1, $array2) |
|
| 500 | |||
| 501 | /** |
||
| 502 | * Update Links with a 'self.version' constraint with the root package's |
||
| 503 | * version. |
||
| 504 | * |
||
| 505 | * @param string $type Link type |
||
| 506 | * @param array $links |
||
| 507 | 110 | * @param RootPackageInterface $root |
|
| 508 | * @return array |
||
| 509 | */ |
||
| 510 | protected function replaceSelfVersionDependencies( |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Get a full featured Package from a RootPackageInterface. |
||
| 554 | * |
||
| 555 | * In Composer versions before 599ad77 the RootPackageInterface only |
||
| 556 | * defines a sub-set of operations needed by composer-merge-plugin and |
||
| 557 | * RootAliasPackage only implemented those methods defined by the |
||
| 558 | * interface. Most of the unimplemented methods in RootAliasPackage can be |
||
| 559 | * worked around because the getter methods that are implemented proxy to |
||
| 560 | * the aliased package which we can modify by unwrapping. The exception |
||
| 561 | * being modifying the 'conflicts', 'provides' and 'replaces' collections. |
||
| 562 | * We have no way to actually modify those collections unfortunately in |
||
| 563 | * older versions of Composer. |
||
| 564 | * |
||
| 565 | * @param RootPackageInterface $root |
||
| 566 | 170 | * @param string $method Method needed |
|
| 567 | * @return RootPackageInterface|RootPackage |
||
| 568 | */ |
||
| 569 | public static function unwrapIfNeeded( |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Update the root packages reference information. |
||
| 586 | 170 | * |
|
| 587 | * @param RootPackageInterface $root |
||
| 588 | */ |
||
| 589 | protected function mergeReferences(RootPackageInterface $root) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Extract vcs revision from version constraint (dev-master#abc123. |
||
| 609 | * |
||
| 610 | * @param array $requires |
||
| 611 | * @param array $references |
||
| 612 | 170 | * @return array |
|
| 613 | * @see RootPackageLoader::extractReferences() |
||
| 614 | 170 | */ |
|
| 615 | 65 | protected function extractReferences(array $requires, array $references) |
|
| 631 | } |
||
| 632 | // vim:sw=4:ts=4:sts=4:et: |
||
| 633 |