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) | |
| 80 | |||
| 81 | /** | ||
| 82 | * Get list of additional packages to include if precessing recursively. | ||
| 83 | * | ||
| 84 | * @return array | ||
| 85 | */ | ||
| 86 | 135 | public function getIncludes() | |
| 91 | |||
| 92 | /** | ||
| 93 | * Get list of additional packages to require if precessing recursively. | ||
| 94 | * | ||
| 95 | * @return array | ||
| 96 | */ | ||
| 97 | 135 | 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 | 140 | protected function readPackageJson($path) | |
| 127 | |||
| 128 | /** | ||
| 129 | * @param array $json | ||
| 130 | * @return CompletePackage | ||
| 131 | */ | ||
| 132 | 140 | 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 | 140 | public function mergeInto(RootPackageInterface $root, PluginState $state) | |
| 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 | protected function prependRepositories(RootPackageInterface $root) | ||
| 196 | 140 |     { | |
| 197 |         if (!isset($this->json['repositories'])) { | ||
| 198 | 140 | return; | |
| 199 | 120 | } | |
| 200 | $repoManager = $this->composer->getRepositoryManager(); | ||
| 201 | 20 | $newRepos = array(); | |
| 202 | 20 | ||
| 203 |         foreach ($this->json['repositories'] as $repoJson) { | ||
| 204 | 20 |             if (!isset($repoJson['type'])) { | |
| 205 | 20 | continue; | |
| 206 | 15 | } | |
| 207 |             $this->logger->info("Prepending {$repoJson['type']} repository"); | ||
| 208 | 20 | $repo = $repoManager->createRepository( | |
| 209 | 5 | $repoJson['type'], | |
| 210 | 5 | $repoJson | |
| 211 | 15 | ); | |
| 212 | $repoManager->prependRepository($repo); | ||
| 213 | 20 | $newRepos[] = $repo; | |
| 214 | 20 | } | |
| 215 | |||
| 216 | 20 | $unwrapped = self::unwrapIfNeeded($root, 'setRepositories'); | |
| 217 | 20 | $unwrapped->setRepositories(array_merge( | |
| 218 | 5 | $newRepos, | |
| 219 | 5 | $root->getRepositories() | |
| 220 | 15 | )); | |
| 221 | } | ||
| 222 | 20 | ||
| 223 | 20 | /** | |
| 224 | * Merge require or require-dev into a RootPackageInterface | ||
| 225 | 20 | * | |
| 226 | 20 | * @param string $type 'require' or 'require-dev' | |
| 227 | 5 | * @param RootPackageInterface $root | |
| 228 | 5 | * @param PluginState $state | |
| 229 | 5 | */ | |
| 230 | 5 | protected function mergeRequires( | |
| 231 | 5 | $type, | |
| 232 | 15 | RootPackageInterface $root, | |
| 233 | 15 | PluginState $state | |
| 234 |     ) { | ||
| 235 | 15 | $linkType = BasePackage::$supportedLinkTypes[$type]; | |
| 236 | $getter = 'get' . ucfirst($linkType['method']); | ||
| 237 | 20 | $setter = 'set' . ucfirst($linkType['method']); | |
| 238 | |||
| 239 |         $requires = $this->package->{$getter}(); | ||
| 240 |         if (empty($requires)) { | ||
| 241 | return; | ||
| 242 | } | ||
| 243 | |||
| 244 | $this->mergeStabilityFlags($root, $requires); | ||
| 245 | |||
| 246 | 140 | $requires = $this->replaceSelfVersionDependencies( | |
| 247 | $type, | ||
| 248 | $requires, | ||
| 249 | $root | ||
| 250 | ); | ||
| 251 | 140 | ||
| 252 | 140 |         $root->{$setter}($this->mergeOrDefer( | |
| 253 | 140 | $type, | |
| 254 |             $root->{$getter}(), | ||
| 255 | 140 | $requires, | |
| 256 | 140 | $state | |
| 257 | 105 | )); | |
| 258 | } | ||
| 259 | |||
| 260 | 85 | /** | |
| 261 | * Merge two collections of package links and collect duplicates for | ||
| 262 | 85 | * subsequent processing. | |
| 263 | 85 | * | |
| 264 | 85 | * @param string $type 'require' or 'require-dev' | |
| 265 | * @param array $origin Primary collection | ||
| 266 | 85 | * @param array $merge Additional collection | |
| 267 | * @param PluginState $state | ||
| 268 | 85 | * @return array Merged collection | |
| 269 | 85 | */ | |
| 270 | 85 | protected function mergeOrDefer( | |
| 271 | 85 | $type, | |
| 272 | array $origin, | ||
| 273 | 85 | array $merge, | |
| 274 | 85 | $state | |
| 275 |     ) { | ||
| 276 | $dups = array(); | ||
| 277 |         foreach ($merge as $name => $link) { | ||
| 278 |             if (!isset($origin[$name]) || $state->replaceDuplicateLinks()) { | ||
| 279 |                 $this->logger->info("Merging <comment>{$name}</comment>"); | ||
| 280 | $origin[$name] = $link; | ||
| 281 |             } else { | ||
| 282 | // Defer to solver. | ||
| 283 | $this->logger->info( | ||
| 284 |                     "Deferring duplicate <comment>{$name}</comment>" | ||
| 285 | ); | ||
| 286 | 85 | $dups[] = $link; | |
| 287 | } | ||
| 288 | } | ||
| 289 | $state->addDuplicateLinks($type, $dups); | ||
| 290 | return $origin; | ||
| 291 | } | ||
| 292 | 85 | ||
| 293 | 85 | /** | |
| 294 | 85 | * Merge autoload or autoload-dev into a RootPackageInterface | |
| 295 | 85 | * | |
| 296 | 85 | * @param string $type 'autoload' or 'devAutoload' | |
| 297 | 85 | * @param RootPackageInterface $root | |
| 298 | */ | ||
| 299 | 25 | protected function mergeAutoload($type, RootPackageInterface $root) | |
| 300 | 25 |     { | |
| 301 | 25 | $getter = 'get' . ucfirst($type); | |
| 302 | 25 | $setter = 'set' . ucfirst($type); | |
| 303 | |||
| 304 | 85 |         $autoload = $this->package->{$getter}(); | |
| 305 | 85 |         if (empty($autoload)) { | |
| 306 | 85 | return; | |
| 307 | } | ||
| 308 | |||
| 309 | $unwrapped = self::unwrapIfNeeded($root, $setter); | ||
| 310 |         $unwrapped->{$setter}(array_merge_recursive( | ||
| 311 |             $root->{$getter}(), | ||
| 312 | $this->fixRelativePaths($autoload) | ||
| 313 | )); | ||
| 314 | } | ||
| 315 | 140 | ||
| 316 | /** | ||
| 317 | 140 | * Fix a collection of paths that are relative to this package to be | |
| 318 | 140 | * relative to the base package. | |
| 319 | * | ||
| 320 | 140 | * @param array $paths | |
| 321 | 140 | * @return array | |
| 322 | 130 | */ | |
| 323 | protected function fixRelativePaths(array $paths) | ||
| 324 |     { | ||
| 325 | 15 | $base = dirname($this->path); | |
| 326 | 15 |         $base = ($base === '.') ? '' : "{$base}/"; | |
| 327 | 15 | ||
| 328 | 15 | array_walk_recursive( | |
| 329 | 15 | $paths, | |
| 330 | 15 |             function (&$path) use ($base) { | |
| 331 |                 $path = "{$base}{$path}"; | ||
| 332 | } | ||
| 333 | ); | ||
| 334 | return $paths; | ||
| 335 | } | ||
| 336 | |||
| 337 | /** | ||
| 338 | * Extract and merge stability flags from the given collection of | ||
| 339 | 15 | * requires and merge them into a RootPackageInterface | |
| 340 | * | ||
| 341 | 15 | * @param RootPackageInterface $root | |
| 342 | 15 | * @param array $requires | |
| 343 | */ | ||
| 344 | 15 | protected function mergeStabilityFlags( | |
| 345 | 15 | RootPackageInterface $root, | |
| 346 | array $requires | ||
| 347 | 15 |     ) { | |
| 348 | 15 | $flags = $root->getStabilityFlags(); | |
| 349 | 15 | $sf = new StabilityFlags($flags, $root->getMinimumStability()); | |
| 350 | 15 | ||
| 351 | $unwrapped = self::unwrapIfNeeded($root, 'setStabilityFlags'); | ||
| 352 | $unwrapped->setStabilityFlags(array_merge( | ||
| 353 | $flags, | ||
| 354 | $sf->extractAll($requires) | ||
| 355 | )); | ||
| 356 | } | ||
| 357 | |||
| 358 | /** | ||
| 359 | * Merge package links of the given type into a RootPackageInterface | ||
| 360 | 85 | * | |
| 361 | * @param string $type 'conflict', 'replace' or 'provide' | ||
| 362 | * @param RootPackageInterface $root | ||
| 363 | */ | ||
| 364 | 85 | protected function mergePackageLinks($type, RootPackageInterface $root) | |
| 387 | 140 | ||
| 388 | 30 | /** | |
| 389 | * Merge suggested packages into a RootPackageInterface | ||
| 390 | * | ||
| 391 | * @param RootPackageInterface $root | ||
| 392 | */ | ||
| 393 | protected function mergeSuggests(RootPackageInterface $root) | ||
| 404 | |||
| 405 | /** | ||
| 406 | * Merge extra config into a RootPackageInterface | ||
| 407 | * | ||
| 408 | * @param RootPackageInterface $root | ||
| 409 | 140 | * @param PluginState $state | |
| 410 | */ | ||
| 411 | 140 | public function mergeExtra(RootPackageInterface $root, PluginState $state) | |
| 443 | 20 | ||
| 444 | 15 | /** | |
| 445 | 15 | * Merges two arrays either via arrayMergeDeep or via array_merge. | |
| 446 | 15 | * | |
| 447 | 15 | * @param bool $mergeDeep | |
| 448 | 5 | * @param array $array1 | |
| 449 | 5 | * @param array $array2 | |
| 450 | 5 | * @return array | |
| 451 | 5 | */ | |
| 452 | 15 | 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 | 30 | * @return array | |
| 469 | */ | ||
| 470 | 30 | protected function replaceSelfVersionDependencies( | |
| 511 | 10 | ||
| 512 | /** | ||
| 513 | * Get a full featured Package from a RootPackageInterface. | ||
| 514 | 5 | * | |
| 515 | 5 | * In Composer versions before 599ad77 the RootPackageInterface only | |
| 516 | 5 | * defines a sub-set of operations needed by composer-merge-plugin and | |
| 517 | 5 | * RootAliasPackage only implemented those methods defined by the | |
| 518 | 5 | * interface. Most of the unimplemented methods in RootAliasPackage can be | |
| 519 | * worked around because the getter methods that are implemented proxy to | ||
| 520 | 5 | * the aliased package which we can modify by unwrapping. The exception | |
| 521 | * being modifying the 'conflicts', 'provides' and 'replaces' collections. | ||
| 522 | 100 | * We have no way to actually modify those collections unfortunately in | |
| 523 | 100 | * older versions of Composer. | |
| 524 | * | ||
| 525 | 100 | * @param RootPackageInterface $root | |
| 526 | * @param string $method Method needed | ||
| 527 | * @return RootPackageInterface|RootPackage | ||
| 528 | */ | ||
| 529 | public static function unwrapIfNeeded( | ||
| 543 | |||
| 544 | /** | ||
| 545 | 140 | * Update the root packages reference information. | |
| 546 | * | ||
| 547 | * @param RootPackageInterface $root | ||
| 548 | */ | ||
| 549 | protected function mergeReferences(RootPackageInterface $root) | ||
| 566 | |||
| 567 | /** | ||
| 568 | * Extract vcs revision from version constraint (dev-master#abc123. | ||
| 569 | 140 | * | |
| 570 | 140 | * @param array $requires | |
| 571 | 140 | * @param array $references | |
| 572 | 140 | * @return array | |
| 573 | 140 | * @see RootPackageLoader::extractReferences() | |
| 574 | 140 | */ | |
| 575 | 140 | protected function extractReferences(array $requires, array $references) | |
| 591 | 140 | } | |
| 592 | // vim:sw=4:ts=4:sts=4:et: | ||
| 593 |