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; |
||
| 20 | class Package |
||
| 21 | { |
||
| 22 | /* ------------------------------------------------------------------------------------------------ |
||
| 23 | | Properties |
||
| 24 | | ------------------------------------------------------------------------------------------------ |
||
| 25 | */ |
||
| 26 | /** @var \Composer\Composer $composer */ |
||
| 27 | protected $composer; |
||
| 28 | |||
| 29 | /** @var \Arcanedev\Composer\Utilities\Logger $logger */ |
||
| 30 | protected $logger; |
||
| 31 | |||
| 32 | /** @var string $path */ |
||
| 33 | protected $path; |
||
| 34 | |||
| 35 | /** @var array $json */ |
||
| 36 | protected $json; |
||
| 37 | |||
| 38 | /** @var \Composer\Package\CompletePackage $package */ |
||
| 39 | protected $package; |
||
| 40 | |||
| 41 | /** @var \Composer\Package\Version\VersionParser $versionParser */ |
||
| 42 | protected $versionParser; |
||
| 43 | |||
| 44 | /* ------------------------------------------------------------------------------------------------ |
||
| 45 | | Constructor |
||
| 46 | | ------------------------------------------------------------------------------------------------ |
||
| 47 | */ |
||
| 48 | /** |
||
| 49 | * Make a Package instance. |
||
| 50 | * |
||
| 51 | * @param string $path |
||
| 52 | * @param \Composer\Composer $composer |
||
| 53 | * @param \Arcanedev\Composer\Utilities\Logger $logger |
||
| 54 | */ |
||
| 55 | 135 | public function __construct($path, Composer $composer, Logger $logger) |
|
| 64 | |||
| 65 | /* ------------------------------------------------------------------------------------------------ |
||
| 66 | | Getters & Setters |
||
| 67 | | ------------------------------------------------------------------------------------------------ |
||
| 68 | */ |
||
| 69 | /** |
||
| 70 | * Get list of additional packages to require if precessing recursively. |
||
| 71 | * |
||
| 72 | * @return array |
||
| 73 | */ |
||
| 74 | 130 | public function getRequires() |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Get list of additional packages to include if precessing recursively. |
||
| 83 | * |
||
| 84 | * @return array |
||
| 85 | */ |
||
| 86 | 130 | public function getIncludes() |
|
| 92 | |||
| 93 | /* ------------------------------------------------------------------------------------------------ |
||
| 94 | | Main Functions |
||
| 95 | | ------------------------------------------------------------------------------------------------ |
||
| 96 | */ |
||
| 97 | /** |
||
| 98 | * Merge this package into a RootPackage. |
||
| 99 | * |
||
| 100 | * @param \Composer\Package\RootPackageInterface $root |
||
| 101 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
| 102 | */ |
||
| 103 | 135 | public function mergeInto(RootPackageInterface $root, PluginState $state) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Merge just the dev portion into a RootPackageInterface. |
||
| 123 | * |
||
| 124 | * @param \Composer\Package\RootPackageInterface $root |
||
| 125 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
| 126 | */ |
||
| 127 | 135 | public function mergeDevInto(RootPackageInterface $root, PluginState $state) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Add a collection of repositories described by the given configuration |
||
| 136 | * to the given package and the global repository manager. |
||
| 137 | * |
||
| 138 | * @param \Composer\Package\RootPackageInterface $root |
||
| 139 | * @param bool $prepend |
||
| 140 | */ |
||
| 141 | 135 | private function addRepositories(RootPackageInterface $root, $prepend) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Add a repository to collection of repositories. |
||
| 162 | * |
||
| 163 | * @param \Composer\Repository\RepositoryManager $repoManager |
||
| 164 | * @param array $repositories |
||
| 165 | * @param array $repoJson |
||
| 166 | * @param bool $prepend |
||
| 167 | */ |
||
| 168 | 20 | private function addRepository( |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Merge require into a RootPackage. |
||
| 191 | * |
||
| 192 | * @param \Composer\Package\RootPackageInterface $root |
||
| 193 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
| 194 | */ |
||
| 195 | 135 | private function mergeRequires(RootPackageInterface $root, PluginState $state) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Merge require-dev into RootPackage. |
||
| 220 | * |
||
| 221 | * @param \Composer\Package\RootPackageInterface $root |
||
| 222 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
| 223 | */ |
||
| 224 | 135 | private function mergeDevRequires(RootPackageInterface $root, PluginState $state) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Merge two collections of package links and collect duplicates for subsequent processing. |
||
| 249 | * |
||
| 250 | * @param \Composer\Package\Link[] $origin Primary collection |
||
| 251 | * @param array $merge Additional collection |
||
| 252 | * @param bool $replace Replace existing links ? |
||
| 253 | * @param array $duplicateLinks Duplicate storage |
||
| 254 | * |
||
| 255 | * @return \Composer\Package\Link[] Merged collection |
||
| 256 | */ |
||
| 257 | 80 | private function mergeLinks(array $origin, array $merge, $replace, array &$duplicateLinks) |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Merge autoload into a RootPackage. |
||
| 276 | * |
||
| 277 | * @param \Composer\Package\RootPackageInterface $root |
||
| 278 | */ |
||
| 279 | 135 | private function mergeAutoload(RootPackageInterface $root) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Merge autoload-dev into a RootPackage. |
||
| 293 | * |
||
| 294 | * @param \Composer\Package\RootPackageInterface $root |
||
| 295 | */ |
||
| 296 | 135 | private function mergeDevAutoload(RootPackageInterface $root) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Extract and merge stability flags from the given collection of |
||
| 310 | * requires and merge them into a RootPackage. |
||
| 311 | * |
||
| 312 | * @param \Composer\Package\RootPackageInterface $root |
||
| 313 | * @param \Composer\Package\Link[] $requires |
||
| 314 | */ |
||
| 315 | 80 | private function mergeStabilityFlags(RootPackageInterface $root, array $requires) |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Merge package links of the given type into a RootPackageInterface |
||
| 329 | * |
||
| 330 | * @param string $type 'conflict', 'replace' or 'provide' |
||
| 331 | * @param \Composer\Package\RootPackageInterface $root |
||
| 332 | */ |
||
| 333 | 135 | protected function mergePackageLinks($type, RootPackageInterface $root) |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Merge suggested packages into a RootPackage. |
||
| 362 | * |
||
| 363 | * @param \Composer\Package\RootPackageInterface $root |
||
| 364 | */ |
||
| 365 | 135 | private function mergeSuggests(RootPackageInterface $root) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Merge extra config into a RootPackage. |
||
| 377 | * |
||
| 378 | * @param \Composer\Package\RootPackageInterface $root |
||
| 379 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
| 380 | */ |
||
| 381 | 135 | private function mergeExtra(RootPackageInterface $root, PluginState $state) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Get extra config. |
||
| 399 | * |
||
| 400 | * @param \Composer\Package\RootPackageInterface $root |
||
| 401 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
| 402 | * @param array $extra |
||
| 403 | * |
||
| 404 | * @return array |
||
| 405 | */ |
||
| 406 | 30 | private function getExtra( |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Merges two arrays either via arrayMergeDeep or via array_merge. |
||
| 429 | * |
||
| 430 | * @param bool $mergeDeep |
||
| 431 | * @param array $array1 |
||
| 432 | * @param array $array2 |
||
| 433 | * |
||
| 434 | * @return array |
||
| 435 | */ |
||
| 436 | 30 | public static function mergeExtraArray($mergeDeep, $array1, $array2) |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Update the root packages reference information. |
||
| 445 | * |
||
| 446 | * @param \Composer\Package\RootPackageInterface $root |
||
| 447 | */ |
||
| 448 | 135 | private function mergeReferences(RootPackageInterface $root) |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Extract vcs revision from version constraint (dev-master#abc123). |
||
| 473 | * @see \Composer\Package\Loader\RootPackageLoader::extractReferences() |
||
| 474 | * |
||
| 475 | * @param array $requires |
||
| 476 | * @param array $references |
||
| 477 | * |
||
| 478 | * @return array |
||
| 479 | */ |
||
| 480 | 135 | protected function extractReferences(array $requires, array $references) |
|
| 494 | |||
| 495 | /** |
||
| 496 | * Update Links with a 'self.version' constraint with the root package's version. |
||
| 497 | * |
||
| 498 | * @param string $type |
||
| 499 | * @param array $links |
||
| 500 | * @param \Composer\Package\RootPackageInterface $root |
||
| 501 | * |
||
| 502 | * @return array |
||
| 503 | */ |
||
| 504 | 95 | protected function replaceSelfVersionDependencies( |
|
| 534 | |||
| 535 | /** |
||
| 536 | * Get a full featured Package from a RootPackageInterface. |
||
| 537 | * |
||
| 538 | * @param \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage $root |
||
| 539 | * @param string $method |
||
| 540 | * |
||
| 541 | * @return \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage |
||
| 542 | */ |
||
| 543 | 135 | private static function unwrapIfNeeded( |
|
| 550 | } |
||
| 551 |
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