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 | 85 | 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 | 80 | public function getRequires() |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Get list of additional packages to include if precessing recursively. |
||
| 82 | * |
||
| 83 | * @return array |
||
| 84 | */ |
||
| 85 | 80 | 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 | 85 | public function mergeInto(RootPackageInterface $root, PluginState $state) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Add a collection of repositories described by the given configuration |
||
| 125 | * to the given package and the global repository manager. |
||
| 126 | * |
||
| 127 | * @param \Composer\Package\RootPackageInterface $root |
||
| 128 | */ |
||
| 129 | 85 | private function addRepositories(RootPackageInterface $root) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Add a repository to collection of repositories. |
||
| 146 | * |
||
| 147 | * @param \Composer\Repository\RepositoryManager $repoManager |
||
| 148 | * @param array $repositories |
||
| 149 | * @param array $repoJson |
||
| 150 | */ |
||
| 151 | 10 | private function addRepository( |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Merge require into a RootPackage. |
||
| 170 | * |
||
| 171 | * @param \Composer\Package\RootPackageInterface $root |
||
| 172 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
| 173 | */ |
||
| 174 | 85 | private function mergeRequires(RootPackageInterface $root, PluginState $state) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Merge require-dev into RootPackage. |
||
| 199 | * |
||
| 200 | * @param \Composer\Package\RootPackageInterface $root |
||
| 201 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
| 202 | */ |
||
| 203 | 85 | private function mergeDevRequires(RootPackageInterface $root, PluginState $state) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Merge two collections of package links and collect duplicates for subsequent processing. |
||
| 228 | * |
||
| 229 | * @param \Composer\Package\Link[] $origin Primary collection |
||
| 230 | * @param array $merge Additional collection |
||
| 231 | * @param bool $replace Replace existing links ? |
||
| 232 | * @param array $duplicateLinks Duplicate storage |
||
| 233 | * |
||
| 234 | * @return \Composer\Package\Link[] Merged collection |
||
| 235 | */ |
||
| 236 | 55 | private function mergeLinks(array $origin, array $merge, $replace, array &$duplicateLinks) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Merge autoload into a RootPackage. |
||
| 255 | * |
||
| 256 | * @param \Composer\Package\RootPackageInterface $root |
||
| 257 | */ |
||
| 258 | 85 | private function mergeAutoload(RootPackageInterface $root) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Merge autoload-dev into a RootPackage. |
||
| 272 | * |
||
| 273 | * @param \Composer\Package\RootPackageInterface $root |
||
| 274 | */ |
||
| 275 | 85 | private function mergeDevAutoload(RootPackageInterface $root) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Extract and merge stability flags from the given collection of |
||
| 289 | * requires and merge them into a RootPackage. |
||
| 290 | * |
||
| 291 | * @param \Composer\Package\RootPackageInterface $root |
||
| 292 | * @param \Composer\Package\Link[] $requires |
||
| 293 | */ |
||
| 294 | 55 | private function mergeStabilityFlags(RootPackageInterface $root, array $requires) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Merge package links of the given type into a RootPackageInterface |
||
| 308 | * |
||
| 309 | * @param string $type 'conflict', 'replace' or 'provide' |
||
| 310 | * @param \Composer\Package\RootPackageInterface $root |
||
| 311 | */ |
||
| 312 | 85 | protected function mergePackageLinks($type, RootPackageInterface $root) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Merge suggested packages into a RootPackage. |
||
| 341 | * |
||
| 342 | * @param \Composer\Package\RootPackageInterface $root |
||
| 343 | */ |
||
| 344 | 85 | private function mergeSuggests(RootPackageInterface $root) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Merge extra config into a RootPackage. |
||
| 356 | * |
||
| 357 | * @param \Composer\Package\RootPackageInterface $root |
||
| 358 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
| 359 | */ |
||
| 360 | 85 | private function mergeExtra(RootPackageInterface $root, PluginState $state) |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Get extra config. |
||
| 378 | * |
||
| 379 | * @param \Composer\Package\RootPackageInterface $root |
||
| 380 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
| 381 | * @param array $extra |
||
| 382 | * |
||
| 383 | * @return array |
||
| 384 | */ |
||
| 385 | 15 | private function getExtra( |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Update the root packages reference information. |
||
| 406 | * |
||
| 407 | * @param \Composer\Package\RootPackageInterface $root |
||
| 408 | */ |
||
| 409 | 85 | private function mergeReferences(RootPackageInterface $root) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Extract vcs revision from version constraint (dev-master#abc123). |
||
| 434 | * @see \Composer\Package\Loader\RootPackageLoader::extractReferences() |
||
| 435 | * |
||
| 436 | * @param array $requires |
||
| 437 | * @param array $references |
||
| 438 | * |
||
| 439 | * @return array |
||
| 440 | */ |
||
| 441 | 85 | protected function extractReferences(array $requires, array $references) |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Update Links with a 'self.version' constraint with the root package's version. |
||
| 458 | * |
||
| 459 | * @param string $type |
||
| 460 | * @param array $links |
||
| 461 | * @param \Composer\Package\RootPackageInterface $root |
||
| 462 | * |
||
| 463 | * @return array |
||
| 464 | */ |
||
| 465 | 65 | protected function replaceSelfVersionDependencies( |
|
| 495 | |||
| 496 | /** |
||
| 497 | * Get a full featured Package from a RootPackageInterface. |
||
| 498 | * |
||
| 499 | * @param \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage $root |
||
| 500 | * @param string $method |
||
| 501 | * |
||
| 502 | * @return \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage |
||
| 503 | */ |
||
| 504 | 85 | private static function unwrapIfNeeded( |
|
| 511 | } |
||
| 512 |
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