Complex classes like Package 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 Package, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Arcanedev\Composer\Entities; |
||
| 19 | class Package |
||
| 20 | { |
||
| 21 | /* ------------------------------------------------------------------------------------------------ |
||
| 22 | | Properties |
||
| 23 | | ------------------------------------------------------------------------------------------------ |
||
| 24 | */ |
||
| 25 | /** @var \Composer\Composer $composer */ |
||
| 26 | protected $composer; |
||
| 27 | |||
| 28 | /** @var \Arcanedev\Composer\Utilities\Logger $logger */ |
||
| 29 | protected $logger; |
||
| 30 | |||
| 31 | /** @var string $path */ |
||
| 32 | protected $path; |
||
| 33 | |||
| 34 | /** @var array $json */ |
||
| 35 | protected $json; |
||
| 36 | |||
| 37 | /** @var \Composer\Package\CompletePackage $package */ |
||
| 38 | protected $package; |
||
| 39 | |||
| 40 | /* ------------------------------------------------------------------------------------------------ |
||
| 41 | | Constructor |
||
| 42 | | ------------------------------------------------------------------------------------------------ |
||
| 43 | */ |
||
| 44 | /** |
||
| 45 | * Make a Package instance. |
||
| 46 | * |
||
| 47 | * @param string $path |
||
| 48 | * @param \Composer\Composer $composer |
||
| 49 | * @param \Arcanedev\Composer\Utilities\Logger $logger |
||
| 50 | */ |
||
| 51 | 80 | public function __construct($path, Composer $composer, Logger $logger) |
|
| 59 | |||
| 60 | /* ------------------------------------------------------------------------------------------------ |
||
| 61 | | Getters & Setters |
||
| 62 | | ------------------------------------------------------------------------------------------------ |
||
| 63 | */ |
||
| 64 | /** |
||
| 65 | * Get list of additional packages to require if precessing recursively. |
||
| 66 | * |
||
| 67 | * @return array |
||
| 68 | */ |
||
| 69 | 75 | public function getRequires() |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Get list of additional packages to include if precessing recursively. |
||
| 78 | * |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | 75 | public function getIncludes() |
|
| 89 | |||
| 90 | /* ------------------------------------------------------------------------------------------------ |
||
| 91 | | Main Functions |
||
| 92 | | ------------------------------------------------------------------------------------------------ |
||
| 93 | */ |
||
| 94 | /** |
||
| 95 | * Merge this package into a RootPackage. |
||
| 96 | * |
||
| 97 | * @param \Composer\Package\RootPackageInterface $root |
||
| 98 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
| 99 | */ |
||
| 100 | 80 | public function mergeInto(RootPackageInterface $root, PluginState $state) |
|
| 101 | { |
||
| 102 | 80 | $this->addRepositories($root); |
|
| 103 | |||
| 104 | 80 | $this->mergeRequires($root, $state); |
|
| 105 | 80 | $this->mergeAutoload($root); |
|
| 106 | |||
| 107 | 80 | if ($state->isDevMode()) { |
|
| 108 | 80 | $this->mergeDevRequires($root, $state); |
|
| 109 | 80 | $this->mergeDevAutoload($root); |
|
| 110 | 64 | } |
|
| 111 | |||
| 112 | 80 | $this->mergePackageLinks('conflict', $root); |
|
| 113 | 80 | $this->mergePackageLinks('replace', $root); |
|
| 114 | 80 | $this->mergePackageLinks('provide', $root); |
|
| 115 | |||
| 116 | 80 | $this->mergeSuggests($root); |
|
| 117 | 80 | $this->mergeExtra($root, $state); |
|
| 118 | 80 | } |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Add a collection of repositories described by the given configuration |
||
| 122 | * to the given package and the global repository manager. |
||
| 123 | * |
||
| 124 | * @param \Composer\Package\RootPackageInterface $root |
||
| 125 | */ |
||
| 126 | 80 | private function addRepositories(RootPackageInterface $root) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Add a repository to collection of repositories. |
||
| 143 | * |
||
| 144 | * @param \Composer\Repository\RepositoryManager $repoManager |
||
| 145 | * @param array $repositories |
||
| 146 | * @param array $repoJson |
||
| 147 | */ |
||
| 148 | 10 | private function addRepository( |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Merge require into a RootPackage. |
||
| 167 | * |
||
| 168 | * @param \Composer\Package\RootPackageInterface $root |
||
| 169 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
| 170 | */ |
||
| 171 | 80 | private function mergeRequires(RootPackageInterface $root, PluginState $state) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Merge require-dev into RootPackage. |
||
| 196 | * |
||
| 197 | * @param \Composer\Package\RootPackageInterface $root |
||
| 198 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
| 199 | */ |
||
| 200 | 80 | private function mergeDevRequires(RootPackageInterface $root, PluginState $state) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Update Links with a 'self.version' constraint with the root package's version. |
||
| 225 | * |
||
| 226 | * @param string $type |
||
| 227 | * @param array $links |
||
| 228 | * @param \Composer\Package\RootPackageInterface $root |
||
| 229 | * |
||
| 230 | * @return array |
||
| 231 | */ |
||
| 232 | 60 | protected function replaceSelfVersionDependencies( |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Merge two collections of package links and collect duplicates for subsequent processing. |
||
| 257 | * |
||
| 258 | * @param array $origin Primary collection |
||
| 259 | * @param array $merge Additional collection |
||
| 260 | * @param bool $replace Replace existing links ? |
||
| 261 | * @param array $duplicateLinks Duplicate storage |
||
| 262 | * |
||
| 263 | * @return array Merged collection |
||
| 264 | */ |
||
| 265 | 55 | private function mergeLinks(array $origin, array $merge, $replace, array &$duplicateLinks) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Merge autoload into a RootPackage. |
||
| 284 | * |
||
| 285 | * @param \Composer\Package\RootPackageInterface $root |
||
| 286 | */ |
||
| 287 | 80 | private function mergeAutoload(RootPackageInterface $root) |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Merge autoload-dev into a RootPackage. |
||
| 301 | * |
||
| 302 | * @param \Composer\Package\RootPackageInterface $root |
||
| 303 | */ |
||
| 304 | 80 | private function mergeDevAutoload(RootPackageInterface $root) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Extract and merge stability flags from the given collection of |
||
| 318 | * requires and merge them into a RootPackage. |
||
| 319 | * |
||
| 320 | * @param \Composer\Package\RootPackageInterface $root |
||
| 321 | * @param array $requires |
||
| 322 | */ |
||
| 323 | 55 | private function mergeStabilityFlags(RootPackageInterface $root, array $requires) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Merge package links of the given type into a RootPackageInterface |
||
| 337 | * |
||
| 338 | * @param string $type 'conflict', 'replace' or 'provide' |
||
| 339 | * @param \Composer\Package\RootPackageInterface $root |
||
| 340 | */ |
||
| 341 | 80 | protected function mergePackageLinks($type, RootPackageInterface $root) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Merge suggested packages into a RootPackage. |
||
| 370 | * |
||
| 371 | * @param \Composer\Package\RootPackageInterface $root |
||
| 372 | */ |
||
| 373 | 80 | private function mergeSuggests(RootPackageInterface $root) |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Merge extra config into a RootPackage. |
||
| 385 | * |
||
| 386 | * @param \Composer\Package\RootPackageInterface $root |
||
| 387 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
| 388 | */ |
||
| 389 | 80 | private function mergeExtra(RootPackageInterface $root, PluginState $state) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Get extra config. |
||
| 407 | * |
||
| 408 | * @param \Composer\Package\RootPackageInterface $root |
||
| 409 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
| 410 | * @param array $extra |
||
| 411 | * |
||
| 412 | * @return array |
||
| 413 | */ |
||
| 414 | 15 | private function getExtra( |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Get a full featured Package from a RootPackageInterface. |
||
| 435 | * |
||
| 436 | * @param \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage $root |
||
| 437 | * @param string $method |
||
| 438 | * |
||
| 439 | * @return \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage |
||
| 440 | */ |
||
| 441 | 80 | private static function unwrapIfNeeded( |
|
| 448 | } |
||
| 449 |