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 | 95 | public function __construct($path, Composer $composer, Logger $logger) |
|
| 72 | { |
||
| 73 | 95 | $this->path = $path; |
|
| 74 | 95 | $this->composer = $composer; |
|
| 75 | 95 | $this->logger = $logger; |
|
| 76 | 95 | $this->json = $this->readPackageJson($path); |
|
| 77 | 95 | $this->package = $this->loadPackage($this->json); |
|
|
|
|||
| 78 | 95 | $this->versionParser = new VersionParser(); |
|
| 79 | 95 | } |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Get list of additional packages to include if precessing recursively. |
||
| 83 | * |
||
| 84 | * @return array |
||
| 85 | */ |
||
| 86 | 90 | public function getIncludes() |
|
| 87 | { |
||
| 88 | 90 | return isset($this->json['extra']['merge-plugin']['include']) ? |
|
| 89 | 90 | $this->json['extra']['merge-plugin']['include'] : array(); |
|
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Get list of additional packages to require if precessing recursively. |
||
| 94 | * |
||
| 95 | * @return array |
||
| 96 | */ |
||
| 97 | 90 | public function getRequires() |
|
| 98 | { |
||
| 99 | 90 | return isset($this->json['extra']['merge-plugin']['require']) ? |
|
| 100 | 90 | $this->json['extra']['merge-plugin']['require'] : array(); |
|
| 101 | } |
||
| 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 | 95 | protected function readPackageJson($path) |
|
| 115 | { |
||
| 116 | 95 | $file = new JsonFile($path); |
|
| 117 | 95 | $json = $file->read(); |
|
| 118 | 95 | if (!isset($json['name'])) { |
|
| 119 | 90 | $json['name'] = 'merge-plugin/' . |
|
| 120 | 90 | strtr($path, DIRECTORY_SEPARATOR, '-'); |
|
| 121 | 90 | } |
|
| 122 | 95 | if (!isset($json['version'])) { |
|
| 123 | 95 | $json['version'] = '1.0.0'; |
|
| 124 | 95 | } |
|
| 125 | 95 | return $json; |
|
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param string $json |
||
| 130 | * @return CompletePackage |
||
| 131 | */ |
||
| 132 | 95 | protected function loadPackage($json) |
|
| 133 | { |
||
| 134 | 95 | $loader = new ArrayLoader(); |
|
| 135 | 95 | $package = $loader->load($json); |
|
| 136 | // @codeCoverageIgnoreStart |
||
| 137 | if (!$package instanceof CompletePackage) { |
||
| 138 | throw new UnexpectedValueException( |
||
| 139 | 'Expected instance of CompletePackage, got ' . |
||
| 140 | get_class($package) |
||
| 141 | ); |
||
| 142 | } |
||
| 143 | // @codeCoverageIgnoreEnd |
||
| 144 | 95 | return $package; |
|
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Merge this package into a RootPackageInterface |
||
| 149 | * |
||
| 150 | * @param RootPackageInterface $root |
||
| 151 | * @param PluginState $state |
||
| 152 | */ |
||
| 153 | 95 | public function mergeInto(RootPackageInterface $root, PluginState $state) |
|
| 154 | { |
||
| 155 | 95 | $this->addRepositories($root); |
|
| 156 | |||
| 157 | 95 | $this->mergeRequires('require', $root, $state); |
|
| 158 | 95 | if ($state->isDevMode()) { |
|
| 159 | 90 | $this->mergeRequires('require-dev', $root, $state); |
|
| 160 | 90 | } |
|
| 161 | |||
| 162 | 95 | $this->mergePackageLinks('conflict', $root); |
|
| 163 | 95 | $this->mergePackageLinks('replace', $root); |
|
| 164 | 95 | $this->mergePackageLinks('provide', $root); |
|
| 165 | |||
| 166 | 95 | $this->mergeSuggests($root); |
|
| 167 | |||
| 168 | 95 | $this->mergeAutoload('autoload', $root); |
|
| 169 | 95 | if ($state->isDevMode()) { |
|
| 170 | 90 | $this->mergeAutoload('devAutoload', $root); |
|
| 171 | 90 | } |
|
| 172 | |||
| 173 | 95 | $this->mergeExtra($root, $state); |
|
| 174 | 95 | $this->mergeReferences($root); |
|
| 175 | 95 | } |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Merge just the dev portion into a RootPackageInterface |
||
| 179 | * |
||
| 180 | * @param RootPackageInterface $root |
||
| 181 | * @param PluginState $state |
||
| 182 | */ |
||
| 183 | 95 | public function mergeDev(RootPackageInterface $root, PluginState $state) |
|
| 184 | { |
||
| 185 | 95 | $this->mergeRequires('require-dev', $root, $state); |
|
| 186 | 85 | $this->mergeAutoload('devAutoload', $root); |
|
| 187 | $this->mergeReferences($root); |
||
| 188 | 10 | } |
|
| 189 | 10 | ||
| 190 | /** |
||
| 191 | 10 | * Add a collection of repositories described by the given configuration |
|
| 192 | 10 | * to the given package and the global repository manager. |
|
| 193 | 10 | * |
|
| 194 | * @param RootPackageInterface $root |
||
| 195 | 10 | */ |
|
| 196 | 10 | protected function addRepositories(RootPackageInterface $root) |
|
| 223 | 95 | ||
| 224 | 95 | /** |
|
| 225 | 95 | * Merge require or require-dev into a RootPackageInterface |
|
| 226 | * |
||
| 227 | 95 | * @param string $type 'require' or 'require-dev' |
|
| 228 | 95 | * @param RootPackageInterface $root |
|
| 229 | 75 | * @param PluginState $state |
|
| 230 | */ |
||
| 231 | protected function mergeRequires( |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Merge two collections of package links and collect duplicates for |
||
| 263 | * subsequent processing. |
||
| 264 | 65 | * |
|
| 265 | 65 | * @param string $type 'require' or 'require-dev' |
|
| 266 | 65 | * @param array $origin Primary collection |
|
| 267 | 65 | * @param array $merge Additional collection |
|
| 268 | 65 | * @param PluginState $state |
|
| 269 | 65 | * @return array Merged collection |
|
| 270 | */ |
||
| 271 | 15 | protected function mergeOrDefer( |
|
| 293 | 95 | ||
| 294 | 90 | /** |
|
| 295 | * Merge autoload or autoload-dev into a RootPackageInterface |
||
| 296 | * |
||
| 297 | 10 | * @param string $type 'autoload' or 'devAutoload' |
|
| 298 | 10 | * @param RootPackageInterface $root |
|
| 299 | 10 | */ |
|
| 300 | 10 | protected function mergeAutoload($type, RootPackageInterface $root) |
|
| 316 | 10 | ||
| 317 | 10 | /** |
|
| 318 | * Fix a collection of paths that are relative to this package to be |
||
| 319 | 10 | * relative to the base package. |
|
| 320 | 10 | * |
|
| 321 | 10 | * @param array $paths |
|
| 322 | 10 | * @return array |
|
| 323 | */ |
||
| 324 | protected function fixRelativePaths(array $paths) |
||
| 337 | 65 | ||
| 338 | /** |
||
| 339 | 65 | * Extract and merge stability flags from the given collection of |
|
| 340 | 65 | * requires and merge them into a RootPackageInterface |
|
| 341 | 65 | * |
|
| 342 | 65 | * @param RootPackageInterface $root |
|
| 343 | 65 | * @param array $requires |
|
| 344 | 65 | */ |
|
| 345 | protected function mergeStabilityFlags( |
||
| 358 | 95 | ||
| 359 | 95 | /** |
|
| 360 | 20 | * Merge package links of the given type into a RootPackageInterface |
|
| 361 | * |
||
| 362 | * @param string $type 'conflict', 'replace' or 'provide' |
||
| 363 | * @param RootPackageInterface $root |
||
| 364 | */ |
||
| 365 | protected function mergePackageLinks($type, RootPackageInterface $root) |
||
| 388 | |||
| 389 | 10 | /** |
|
| 390 | 10 | * Merge suggested packages into a RootPackageInterface |
|
| 391 | 95 | * |
|
| 392 | * @param RootPackageInterface $root |
||
| 393 | */ |
||
| 394 | protected function mergeSuggests(RootPackageInterface $root) |
||
| 405 | |||
| 406 | /** |
||
| 407 | 15 | * Merge extra config into a RootPackageInterface |
|
| 408 | 15 | * |
|
| 409 | * @param RootPackageInterface $root |
||
| 410 | 15 | * @param PluginState $state |
|
| 411 | 5 | */ |
|
| 412 | 5 | public function mergeExtra(RootPackageInterface $root, PluginState $state) |
|
| 443 | |||
| 444 | /** |
||
| 445 | 75 | * Update Links with a 'self.version' constraint with the root package's |
|
| 446 | 75 | * version. |
|
| 447 | 75 | * |
|
| 448 | 75 | * @param string $type Link type |
|
| 449 | * @param array $links |
||
| 450 | 75 | * @param RootPackageInterface $root |
|
| 451 | 75 | * @return array |
|
| 452 | */ |
||
| 453 | 75 | protected function replaceSelfVersionDependencies( |
|
| 494 | |||
| 495 | /** |
||
| 496 | * Get a full featured Package from a RootPackageInterface. |
||
| 497 | * |
||
| 498 | * In Composer versions before 599ad77 the RootPackageInterface only |
||
| 499 | 95 | * defines a sub-set of operations needed by composer-merge-plugin and |
|
| 500 | * RootAliasPackage only implemented those methods defined by the |
||
| 501 | * interface. Most of the unimplemented methods in RootAliasPackage can be |
||
| 502 | * worked around because the getter methods that are implemented proxy to |
||
| 503 | * the aliased package which we can modify by unwrapping. The exception |
||
| 504 | * being modifying the 'conflicts', 'provides' and 'replaces' collections. |
||
| 505 | * We have no way to actually modify those collections unfortunately in |
||
| 506 | * older versions of Composer. |
||
| 507 | * |
||
| 508 | * @param RootPackageInterface $root |
||
| 509 | * @param string $method Method needed |
||
| 510 | * @return RootPackageInterface|RootPackage |
||
| 511 | 95 | */ |
|
| 512 | public static function unwrapIfNeeded( |
||
| 526 | 95 | ||
| 527 | 95 | /** |
|
| 528 | 95 | * Update the root packages reference information. |
|
| 529 | 95 | * |
|
| 530 | 35 | * @param RootPackageInterface $root |
|
| 531 | 95 | */ |
|
| 532 | 95 | protected function mergeReferences(RootPackageInterface $root) |
|
| 549 | 35 | ||
| 550 | /** |
||
| 551 | 35 | * Extract vcs revision from version constraint (dev-master#abc123. |
|
| 552 | * |
||
| 553 | 35 | * @param array $requires |
|
| 554 | 5 | * @param array $references |
|
| 555 | 5 | * @return array |
|
| 556 | 5 | * @see RootPackageLoader::extractReferences() |
|
| 557 | 95 | */ |
|
| 558 | protected function extractReferences(array $requires, array $references) |
||
| 574 | } |
||
| 575 | // vim:sw=4:ts=4:sts=4:et: |
||
| 576 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: