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 | * 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 | * @param bool $prepend |
||
| 129 | */ |
||
| 130 | 95 | private function addRepositories(RootPackageInterface $root, $prepend) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Add a repository to collection of repositories. |
||
| 151 | * |
||
| 152 | * @param \Composer\Repository\RepositoryManager $repoManager |
||
| 153 | * @param array $repositories |
||
| 154 | * @param array $repoJson |
||
| 155 | * @param bool $prepend |
||
| 156 | */ |
||
| 157 | 15 | private function addRepository( |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Merge require into a RootPackage. |
||
| 180 | * |
||
| 181 | * @param \Composer\Package\RootPackageInterface $root |
||
| 182 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
| 183 | */ |
||
| 184 | 95 | private function mergeRequires(RootPackageInterface $root, PluginState $state) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Merge require-dev into RootPackage. |
||
| 209 | * |
||
| 210 | * @param \Composer\Package\RootPackageInterface $root |
||
| 211 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
| 212 | */ |
||
| 213 | 95 | private function mergeDevRequires(RootPackageInterface $root, PluginState $state) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Merge two collections of package links and collect duplicates for subsequent processing. |
||
| 238 | * |
||
| 239 | * @param \Composer\Package\Link[] $origin Primary collection |
||
| 240 | * @param array $merge Additional collection |
||
| 241 | * @param bool $replace Replace existing links ? |
||
| 242 | * @param array $duplicateLinks Duplicate storage |
||
| 243 | * |
||
| 244 | * @return \Composer\Package\Link[] Merged collection |
||
| 245 | */ |
||
| 246 | 60 | private function mergeLinks(array $origin, array $merge, $replace, array &$duplicateLinks) |
|
| 262 | |||
| 263 | /** |
||
| 264 | * Merge autoload into a RootPackage. |
||
| 265 | * |
||
| 266 | * @param \Composer\Package\RootPackageInterface $root |
||
| 267 | */ |
||
| 268 | 95 | private function mergeAutoload(RootPackageInterface $root) |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Merge autoload-dev into a RootPackage. |
||
| 282 | * |
||
| 283 | * @param \Composer\Package\RootPackageInterface $root |
||
| 284 | */ |
||
| 285 | 95 | private function mergeDevAutoload(RootPackageInterface $root) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Extract and merge stability flags from the given collection of |
||
| 299 | * requires and merge them into a RootPackage. |
||
| 300 | * |
||
| 301 | * @param \Composer\Package\RootPackageInterface $root |
||
| 302 | * @param \Composer\Package\Link[] $requires |
||
| 303 | */ |
||
| 304 | 60 | private function mergeStabilityFlags(RootPackageInterface $root, array $requires) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Merge package links of the given type into a RootPackageInterface |
||
| 318 | * |
||
| 319 | * @param string $type 'conflict', 'replace' or 'provide' |
||
| 320 | * @param \Composer\Package\RootPackageInterface $root |
||
| 321 | */ |
||
| 322 | 95 | protected function mergePackageLinks($type, RootPackageInterface $root) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Merge suggested packages into a RootPackage. |
||
| 351 | * |
||
| 352 | * @param \Composer\Package\RootPackageInterface $root |
||
| 353 | */ |
||
| 354 | 95 | private function mergeSuggests(RootPackageInterface $root) |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Merge extra config into a RootPackage. |
||
| 366 | * |
||
| 367 | * @param \Composer\Package\RootPackageInterface $root |
||
| 368 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
| 369 | */ |
||
| 370 | 95 | private function mergeExtra(RootPackageInterface $root, PluginState $state) |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Get extra config. |
||
| 388 | * |
||
| 389 | * @param \Composer\Package\RootPackageInterface $root |
||
| 390 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
| 391 | * @param array $extra |
||
| 392 | * |
||
| 393 | * @return array |
||
| 394 | */ |
||
| 395 | 15 | private function getExtra( |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Update the root packages reference information. |
||
| 416 | * |
||
| 417 | * @param \Composer\Package\RootPackageInterface $root |
||
| 418 | */ |
||
| 419 | 95 | private function mergeReferences(RootPackageInterface $root) |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Extract vcs revision from version constraint (dev-master#abc123). |
||
| 444 | * @see \Composer\Package\Loader\RootPackageLoader::extractReferences() |
||
| 445 | * |
||
| 446 | * @param array $requires |
||
| 447 | * @param array $references |
||
| 448 | * |
||
| 449 | * @return array |
||
| 450 | */ |
||
| 451 | 95 | protected function extractReferences(array $requires, array $references) |
|
| 465 | |||
| 466 | /** |
||
| 467 | * Update Links with a 'self.version' constraint with the root package's version. |
||
| 468 | * |
||
| 469 | * @param string $type |
||
| 470 | * @param array $links |
||
| 471 | * @param \Composer\Package\RootPackageInterface $root |
||
| 472 | * |
||
| 473 | * @return array |
||
| 474 | */ |
||
| 475 | 70 | protected function replaceSelfVersionDependencies( |
|
| 505 | |||
| 506 | /** |
||
| 507 | * Get a full featured Package from a RootPackageInterface. |
||
| 508 | * |
||
| 509 | * @param \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage $root |
||
| 510 | * @param string $method |
||
| 511 | * |
||
| 512 | * @return \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage |
||
| 513 | */ |
||
| 514 | 95 | private static function unwrapIfNeeded( |
|
| 521 | } |
||
| 522 |
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