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 | 140 | public function __construct($path, Composer $composer, Logger $logger) |
|
| 72 | { |
||
| 73 | 140 | $this->path = $path; |
|
| 74 | 140 | $this->composer = $composer; |
|
| 75 | 140 | $this->logger = $logger; |
|
| 76 | 140 | $this->json = $this->readPackageJson($path); |
|
| 77 | 140 | $this->package = $this->loadPackage($this->json); |
|
| 78 | 140 | $this->versionParser = new VersionParser(); |
|
| 79 | 140 | } |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Get list of additional packages to include if precessing recursively. |
||
| 83 | * |
||
| 84 | * @return array |
||
| 85 | */ |
||
| 86 | 135 | public function getIncludes() |
|
| 87 | { |
||
| 88 | 135 | return isset($this->json['extra']['merge-plugin']['include']) ? |
|
| 89 | 135 | $this->json['extra']['merge-plugin']['include'] : array(); |
|
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Get list of additional packages to require if precessing recursively. |
||
| 94 | * |
||
| 95 | * @return array |
||
| 96 | */ |
||
| 97 | 135 | public function getRequires() |
|
| 98 | { |
||
| 99 | 135 | return isset($this->json['extra']['merge-plugin']['require']) ? |
|
| 100 | 135 | $this->json['extra']['merge-plugin']['require'] : array(); |
|
| 101 | } |
||
| 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 | 140 | protected function readPackageJson($path) |
|
| 115 | { |
||
| 116 | 140 | $file = new JsonFile($path); |
|
| 117 | 140 | $json = $file->read(); |
|
| 118 | 140 | if (!isset($json['name'])) { |
|
| 119 | 130 | $json['name'] = 'merge-plugin/' . |
|
| 120 | 130 | strtr($path, DIRECTORY_SEPARATOR, '-'); |
|
| 121 | 130 | } |
|
| 122 | 140 | if (!isset($json['version'])) { |
|
| 123 | 140 | $json['version'] = '1.0.0'; |
|
| 124 | 140 | } |
|
| 125 | 140 | return $json; |
|
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param array $json |
||
| 130 | * @return CompletePackage |
||
| 131 | */ |
||
| 132 | 140 | protected function loadPackage(array $json) |
|
| 133 | { |
||
| 134 | 140 | $loader = new ArrayLoader(); |
|
| 135 | 140 | $package = $loader->load($json); |
|
| 136 | // @codeCoverageIgnoreStart |
||
| 137 | if (!$package instanceof CompletePackage) { |
||
| 138 | throw new UnexpectedValueException( |
||
| 139 | 'Expected instance of CompletePackage, got ' . |
||
| 140 | get_class($package) |
||
| 141 | ); |
||
| 142 | } |
||
| 143 | // @codeCoverageIgnoreEnd |
||
| 144 | 140 | return $package; |
|
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Merge this package into a RootPackageInterface |
||
| 149 | * |
||
| 150 | * @param RootPackageInterface $root |
||
| 151 | * @param PluginState $state |
||
| 152 | */ |
||
| 153 | 140 | public function mergeInto(RootPackageInterface $root, PluginState $state) |
|
| 154 | { |
||
| 155 | 140 | $this->prependRepositories($root); |
|
| 156 | |||
| 157 | 140 | $this->mergeRequires('require', $root, $state); |
|
| 158 | |||
| 159 | 140 | $this->mergePackageLinks('conflict', $root); |
|
| 160 | 140 | $this->mergePackageLinks('replace', $root); |
|
| 161 | 140 | $this->mergePackageLinks('provide', $root); |
|
| 162 | |||
| 163 | 140 | $this->mergeSuggests($root); |
|
| 164 | |||
| 165 | 140 | $this->mergeAutoload('autoload', $root); |
|
| 166 | |||
| 167 | 140 | $this->mergeExtra($root, $state); |
|
| 168 | |||
| 169 | 140 | if ($state->isDevMode()) { |
|
| 170 | 75 | $this->mergeDevInto($root, $state); |
|
| 171 | 75 | } else { |
|
| 172 | 65 | $this->mergeReferences($root); |
|
| 173 | } |
||
| 174 | 140 | } |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Merge just the dev portion into a RootPackageInterface |
||
| 178 | * |
||
| 179 | * @param RootPackageInterface $root |
||
| 180 | * @param PluginState $state |
||
| 181 | */ |
||
| 182 | 135 | public function mergeDevInto(RootPackageInterface $root, PluginState $state) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Add a collection of repositories described by the given configuration |
||
| 191 | * to the given package and the global repository manager. |
||
| 192 | * |
||
| 193 | * @param RootPackageInterface $root |
||
| 194 | */ |
||
| 195 | 140 | protected function prependRepositories(RootPackageInterface $root) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Merge require or require-dev into a RootPackageInterface |
||
| 225 | * |
||
| 226 | * @param string $type 'require' or 'require-dev' |
||
| 227 | * @param RootPackageInterface $root |
||
| 228 | * @param PluginState $state |
||
| 229 | */ |
||
| 230 | 140 | protected function mergeRequires( |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Merge two collections of package links and collect duplicates for |
||
| 262 | * subsequent processing. |
||
| 263 | * |
||
| 264 | * @param string $type 'require' or 'require-dev' |
||
| 265 | * @param array $origin Primary collection |
||
| 266 | * @param array $merge Additional collection |
||
| 267 | * @param PluginState $state |
||
| 268 | * @return array Merged collection |
||
| 269 | */ |
||
| 270 | 85 | protected function mergeOrDefer( |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Merge autoload or autoload-dev into a RootPackageInterface |
||
| 295 | * |
||
| 296 | * @param string $type 'autoload' or 'devAutoload' |
||
| 297 | * @param RootPackageInterface $root |
||
| 298 | */ |
||
| 299 | 140 | protected function mergeAutoload($type, RootPackageInterface $root) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Fix a collection of paths that are relative to this package to be |
||
| 318 | * relative to the base package. |
||
| 319 | * |
||
| 320 | * @param array $paths |
||
| 321 | * @return array |
||
| 322 | */ |
||
| 323 | 15 | protected function fixRelativePaths(array $paths) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Extract and merge stability flags from the given collection of |
||
| 339 | * requires and merge them into a RootPackageInterface |
||
| 340 | * |
||
| 341 | * @param RootPackageInterface $root |
||
| 342 | * @param array $requires |
||
| 343 | */ |
||
| 344 | 85 | protected function mergeStabilityFlags( |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Merge package links of the given type into a RootPackageInterface |
||
| 360 | * |
||
| 361 | * @param string $type 'conflict', 'replace' or 'provide' |
||
| 362 | * @param RootPackageInterface $root |
||
| 363 | */ |
||
| 364 | 140 | protected function mergePackageLinks($type, RootPackageInterface $root) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Merge suggested packages into a RootPackageInterface |
||
| 390 | * |
||
| 391 | * @param RootPackageInterface $root |
||
| 392 | */ |
||
| 393 | 140 | protected function mergeSuggests(RootPackageInterface $root) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Merge extra config into a RootPackageInterface |
||
| 407 | * |
||
| 408 | * @param RootPackageInterface $root |
||
| 409 | * @param PluginState $state |
||
| 410 | */ |
||
| 411 | 140 | public function mergeExtra(RootPackageInterface $root, PluginState $state) |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Merges two arrays either via arrayMergeDeep or via array_merge. |
||
| 446 | * |
||
| 447 | * @param bool $mergeDeep |
||
| 448 | * @param array $array1 |
||
| 449 | * @param array $array2 |
||
| 450 | * @return array |
||
| 451 | */ |
||
| 452 | 30 | public static function mergeExtraArray($mergeDeep, $array1, $array2) |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Update Links with a 'self.version' constraint with the root package's |
||
| 463 | * version. |
||
| 464 | * |
||
| 465 | * @param string $type Link type |
||
| 466 | * @param array $links |
||
| 467 | * @param RootPackageInterface $root |
||
| 468 | * @return array |
||
| 469 | */ |
||
| 470 | 100 | protected function replaceSelfVersionDependencies( |
|
| 511 | |||
| 512 | /** |
||
| 513 | * Get a full featured Package from a RootPackageInterface. |
||
| 514 | * |
||
| 515 | * In Composer versions before 599ad77 the RootPackageInterface only |
||
| 516 | * defines a sub-set of operations needed by composer-merge-plugin and |
||
| 517 | * RootAliasPackage only implemented those methods defined by the |
||
| 518 | * interface. Most of the unimplemented methods in RootAliasPackage can be |
||
| 519 | * worked around because the getter methods that are implemented proxy to |
||
| 520 | * the aliased package which we can modify by unwrapping. The exception |
||
| 521 | * being modifying the 'conflicts', 'provides' and 'replaces' collections. |
||
| 522 | * We have no way to actually modify those collections unfortunately in |
||
| 523 | * older versions of Composer. |
||
| 524 | * |
||
| 525 | * @param RootPackageInterface $root |
||
| 526 | * @param string $method Method needed |
||
| 527 | * @return RootPackageInterface|RootPackage |
||
| 528 | */ |
||
| 529 | 140 | public static function unwrapIfNeeded( |
|
| 543 | |||
| 544 | /** |
||
| 545 | * Update the root packages reference information. |
||
| 546 | * |
||
| 547 | * @param RootPackageInterface $root |
||
| 548 | */ |
||
| 549 | 140 | protected function mergeReferences(RootPackageInterface $root) |
|
| 566 | |||
| 567 | /** |
||
| 568 | * Extract vcs revision from version constraint (dev-master#abc123. |
||
| 569 | * |
||
| 570 | * @param array $requires |
||
| 571 | * @param array $references |
||
| 572 | * @return array |
||
| 573 | * @see RootPackageLoader::extractReferences() |
||
| 574 | */ |
||
| 575 | 140 | protected function extractReferences(array $requires, array $references) |
|
| 591 | } |
||
| 592 | // vim:sw=4:ts=4:sts=4:et: |
||
| 593 |