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 | /** @var \Composer\Package\Version\VersionParser $versionParser */ |
||
| 41 | protected $versionParser; |
||
| 42 | |||
| 43 | /* ------------------------------------------------------------------------------------------------ |
||
| 44 | | Constructor |
||
| 45 | | ------------------------------------------------------------------------------------------------ |
||
| 46 | */ |
||
| 47 | /** |
||
| 48 | * Make a Package instance. |
||
| 49 | * |
||
| 50 | * @param string $path |
||
| 51 | * @param \Composer\Composer $composer |
||
| 52 | * @param \Arcanedev\Composer\Utilities\Logger $logger |
||
| 53 | */ |
||
| 54 | 95 | public function __construct($path, Composer $composer, Logger $logger) |
|
| 63 | |||
| 64 | /* ------------------------------------------------------------------------------------------------ |
||
| 65 | | Getters & Setters |
||
| 66 | | ------------------------------------------------------------------------------------------------ |
||
| 67 | */ |
||
| 68 | /** |
||
| 69 | * Get list of additional packages to require if precessing recursively. |
||
| 70 | * |
||
| 71 | * @return array |
||
| 72 | */ |
||
| 73 | 90 | public function getRequires() |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Get list of additional packages to include if precessing recursively. |
||
| 82 | * |
||
| 83 | * @return array |
||
| 84 | */ |
||
| 85 | 90 | public function getIncludes() |
|
| 91 | |||
| 92 | /* ------------------------------------------------------------------------------------------------ |
||
| 93 | | Main Functions |
||
| 94 | | ------------------------------------------------------------------------------------------------ |
||
| 95 | */ |
||
| 96 | /** |
||
| 97 | * Merge this package into a RootPackage. |
||
| 98 | * |
||
| 99 | * @param \Composer\Package\RootPackageInterface $root |
||
| 100 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
| 101 | */ |
||
| 102 | 95 | public function mergeInto(RootPackageInterface $root, PluginState $state) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Merge just the dev portion into a RootPackageInterface. |
||
| 125 | * |
||
| 126 | * @param \Composer\Package\RootPackageInterface $root |
||
| 127 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
| 128 | */ |
||
| 129 | 25 | public function mergeDev(RootPackageInterface $root, PluginState $state) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Add a collection of repositories described by the given configuration |
||
| 138 | * to the given package and the global repository manager. |
||
| 139 | * |
||
| 140 | * @param \Composer\Package\RootPackageInterface $root |
||
| 141 | * @param bool $prepend |
||
| 142 | */ |
||
| 143 | 95 | private function addRepositories(RootPackageInterface $root, $prepend) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Add a repository to collection of repositories. |
||
| 164 | * |
||
| 165 | * @param \Composer\Repository\RepositoryManager $repoManager |
||
| 166 | * @param array $repositories |
||
| 167 | * @param array $repoJson |
||
| 168 | * @param bool $prepend |
||
| 169 | */ |
||
| 170 | 15 | private function addRepository( |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Merge require into a RootPackage. |
||
| 193 | * |
||
| 194 | * @param \Composer\Package\RootPackageInterface $root |
||
| 195 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
| 196 | */ |
||
| 197 | 95 | private function mergeRequires(RootPackageInterface $root, PluginState $state) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Merge require-dev into RootPackage. |
||
| 222 | * |
||
| 223 | * @param \Composer\Package\RootPackageInterface $root |
||
| 224 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
| 225 | */ |
||
| 226 | 95 | private function mergeDevRequires(RootPackageInterface $root, PluginState $state) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Merge two collections of package links and collect duplicates for subsequent processing. |
||
| 251 | * |
||
| 252 | * @param \Composer\Package\Link[] $origin Primary collection |
||
| 253 | * @param array $merge Additional collection |
||
| 254 | * @param bool $replace Replace existing links ? |
||
| 255 | * @param array $duplicateLinks Duplicate storage |
||
| 256 | * |
||
| 257 | * @return \Composer\Package\Link[] Merged collection |
||
| 258 | */ |
||
| 259 | 60 | private function mergeLinks(array $origin, array $merge, $replace, array &$duplicateLinks) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Merge autoload into a RootPackage. |
||
| 278 | * |
||
| 279 | * @param \Composer\Package\RootPackageInterface $root |
||
| 280 | */ |
||
| 281 | 95 | private function mergeAutoload(RootPackageInterface $root) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Merge autoload-dev into a RootPackage. |
||
| 295 | * |
||
| 296 | * @param \Composer\Package\RootPackageInterface $root |
||
| 297 | */ |
||
| 298 | 95 | private function mergeDevAutoload(RootPackageInterface $root) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Extract and merge stability flags from the given collection of |
||
| 312 | * requires and merge them into a RootPackage. |
||
| 313 | * |
||
| 314 | * @param \Composer\Package\RootPackageInterface $root |
||
| 315 | * @param \Composer\Package\Link[] $requires |
||
| 316 | */ |
||
| 317 | 60 | private function mergeStabilityFlags(RootPackageInterface $root, array $requires) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Merge package links of the given type into a RootPackageInterface |
||
| 331 | * |
||
| 332 | * @param string $type 'conflict', 'replace' or 'provide' |
||
| 333 | * @param \Composer\Package\RootPackageInterface $root |
||
| 334 | */ |
||
| 335 | 95 | protected function mergePackageLinks($type, RootPackageInterface $root) |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Merge suggested packages into a RootPackage. |
||
| 364 | * |
||
| 365 | * @param \Composer\Package\RootPackageInterface $root |
||
| 366 | */ |
||
| 367 | 95 | private function mergeSuggests(RootPackageInterface $root) |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Merge extra config into a RootPackage. |
||
| 379 | * |
||
| 380 | * @param \Composer\Package\RootPackageInterface $root |
||
| 381 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
| 382 | */ |
||
| 383 | 95 | private function mergeExtra(RootPackageInterface $root, PluginState $state) |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Get extra config. |
||
| 401 | * |
||
| 402 | * @param \Composer\Package\RootPackageInterface $root |
||
| 403 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
| 404 | * @param array $extra |
||
| 405 | * |
||
| 406 | * @return array |
||
| 407 | */ |
||
| 408 | 15 | private function getExtra( |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Update the root packages reference information. |
||
| 429 | * |
||
| 430 | * @param \Composer\Package\RootPackageInterface $root |
||
| 431 | */ |
||
| 432 | 95 | private function mergeReferences(RootPackageInterface $root) |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Extract vcs revision from version constraint (dev-master#abc123). |
||
| 457 | * @see \Composer\Package\Loader\RootPackageLoader::extractReferences() |
||
| 458 | * |
||
| 459 | * @param array $requires |
||
| 460 | * @param array $references |
||
| 461 | * |
||
| 462 | * @return array |
||
| 463 | */ |
||
| 464 | 95 | protected function extractReferences(array $requires, array $references) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Update Links with a 'self.version' constraint with the root package's version. |
||
| 481 | * |
||
| 482 | * @param string $type |
||
| 483 | * @param array $links |
||
| 484 | * @param \Composer\Package\RootPackageInterface $root |
||
| 485 | * |
||
| 486 | * @return array |
||
| 487 | */ |
||
| 488 | 70 | protected function replaceSelfVersionDependencies( |
|
| 518 | |||
| 519 | /** |
||
| 520 | * Get a full featured Package from a RootPackageInterface. |
||
| 521 | * |
||
| 522 | * @param \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage $root |
||
| 523 | * @param string $method |
||
| 524 | * |
||
| 525 | * @return \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage |
||
| 526 | */ |
||
| 527 | 95 | private static function unwrapIfNeeded( |
|
| 534 | } |
||
| 535 |
It seems like you are assigning to a variable which was imported through a
usestatement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope