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 | 100 | public function __construct($path, Composer $composer, Logger $logger) |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Get list of additional packages to include if precessing recursively. |
||
| 83 | * |
||
| 84 | * @return array |
||
| 85 | */ |
||
| 86 | 95 | public function getIncludes() |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Get list of additional packages to require if precessing recursively. |
||
| 94 | * |
||
| 95 | * @return array |
||
| 96 | */ |
||
| 97 | 95 | public function getRequires() |
|
| 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 | 100 | protected function readPackageJson($path) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * @param string $json |
||
| 130 | * @return CompletePackage |
||
| 131 | */ |
||
| 132 | 100 | protected function loadPackage($json) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Merge this package into a RootPackageInterface |
||
| 149 | * |
||
| 150 | * @param RootPackageInterface $root |
||
| 151 | * @param PluginState $state |
||
| 152 | */ |
||
| 153 | 100 | public function mergeInto(RootPackageInterface $root, PluginState $state) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Merge just the dev portion into a RootPackageInterface |
||
| 178 | * |
||
| 179 | * @param RootPackageInterface $root |
||
| 180 | * @param PluginState $state |
||
| 181 | */ |
||
| 182 | 95 | public function mergeDevInto(RootPackageInterface $root, PluginState $state) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Add a collection of repositories described by the given configuration |
||
| 191 | * to the given package and the global repository manager. |
||
| 192 | * |
||
| 193 | * @param RootPackageInterface $root |
||
| 194 | */ |
||
| 195 | 100 | protected function prependRepositories(RootPackageInterface $root) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Merge require or require-dev into a RootPackageInterface |
||
| 225 | * |
||
| 226 | * @param string $type 'require' or 'require-dev' |
||
| 227 | * @param RootPackageInterface $root |
||
| 228 | * @param PluginState $state |
||
| 229 | */ |
||
| 230 | 100 | protected function mergeRequires( |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Merge two collections of package links and collect duplicates for |
||
| 262 | * subsequent processing. |
||
| 263 | * |
||
| 264 | * @param string $type 'require' or 'require-dev' |
||
| 265 | * @param array $origin Primary collection |
||
| 266 | * @param array $merge Additional collection |
||
| 267 | * @param PluginState $state |
||
| 268 | * @return array Merged collection |
||
| 269 | */ |
||
| 270 | 65 | protected function mergeOrDefer( |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Merge autoload or autoload-dev into a RootPackageInterface |
||
| 295 | * |
||
| 296 | * @param string $type 'autoload' or 'devAutoload' |
||
| 297 | * @param RootPackageInterface $root |
||
| 298 | */ |
||
| 299 | 100 | protected function mergeAutoload($type, RootPackageInterface $root) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Fix a collection of paths that are relative to this package to be |
||
| 318 | * relative to the base package. |
||
| 319 | * |
||
| 320 | * @param array $paths |
||
| 321 | * @return array |
||
| 322 | */ |
||
| 323 | 10 | protected function fixRelativePaths(array $paths) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Extract and merge stability flags from the given collection of |
||
| 339 | * requires and merge them into a RootPackageInterface |
||
| 340 | * |
||
| 341 | * @param RootPackageInterface $root |
||
| 342 | * @param array $requires |
||
| 343 | */ |
||
| 344 | 65 | protected function mergeStabilityFlags( |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Merge package links of the given type into a RootPackageInterface |
||
| 360 | * |
||
| 361 | * @param string $type 'conflict', 'replace' or 'provide' |
||
| 362 | * @param RootPackageInterface $root |
||
| 363 | */ |
||
| 364 | 100 | protected function mergePackageLinks($type, RootPackageInterface $root) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Merge suggested packages into a RootPackageInterface |
||
| 390 | * |
||
| 391 | * @param RootPackageInterface $root |
||
| 392 | */ |
||
| 393 | 100 | protected function mergeSuggests(RootPackageInterface $root) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Merge extra config into a RootPackageInterface |
||
| 407 | * |
||
| 408 | * @param RootPackageInterface $root |
||
| 409 | * @param PluginState $state |
||
| 410 | */ |
||
| 411 | 100 | public function mergeExtra(RootPackageInterface $root, PluginState $state) |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Merges two arrays either via arrayMergeDeep or via array_merge. |
||
| 446 | * |
||
| 447 | * @param bool $mergeDeep |
||
| 448 | * @param array $array1 |
||
| 449 | * @param array $array2 |
||
| 450 | * @return array |
||
| 451 | */ |
||
| 452 | 15 | public static function mergeExtraArray($mergeDeep, $array1, $array2) |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Update Links with a 'self.version' constraint with the root package's |
||
| 463 | * version. |
||
| 464 | * |
||
| 465 | * @param string $type Link type |
||
| 466 | * @param array $links |
||
| 467 | * @param RootPackageInterface $root |
||
| 468 | * @return array |
||
| 469 | */ |
||
| 470 | 75 | protected function replaceSelfVersionDependencies( |
|
| 511 | |||
| 512 | /** |
||
| 513 | * Get a full featured Package from a RootPackageInterface. |
||
| 514 | * |
||
| 515 | * In Composer versions before 599ad77 the RootPackageInterface only |
||
| 516 | * defines a sub-set of operations needed by composer-merge-plugin and |
||
| 517 | * RootAliasPackage only implemented those methods defined by the |
||
| 518 | * interface. Most of the unimplemented methods in RootAliasPackage can be |
||
| 519 | * worked around because the getter methods that are implemented proxy to |
||
| 520 | * the aliased package which we can modify by unwrapping. The exception |
||
| 521 | * being modifying the 'conflicts', 'provides' and 'replaces' collections. |
||
| 522 | * We have no way to actually modify those collections unfortunately in |
||
| 523 | * older versions of Composer. |
||
| 524 | * |
||
| 525 | * @param RootPackageInterface $root |
||
| 526 | * @param string $method Method needed |
||
| 527 | * @return RootPackageInterface|RootPackage |
||
| 528 | */ |
||
| 529 | 100 | public static function unwrapIfNeeded( |
|
| 543 | |||
| 544 | /** |
||
| 545 | * Update the root packages reference information. |
||
| 546 | * |
||
| 547 | * @param RootPackageInterface $root |
||
| 548 | */ |
||
| 549 | 100 | protected function mergeReferences(RootPackageInterface $root) |
|
| 566 | |||
| 567 | /** |
||
| 568 | * Extract vcs revision from version constraint (dev-master#abc123. |
||
| 569 | * |
||
| 570 | * @param array $requires |
||
| 571 | * @param array $references |
||
| 572 | * @return array |
||
| 573 | * @see RootPackageLoader::extractReferences() |
||
| 574 | */ |
||
| 575 | 100 | protected function extractReferences(array $requires, array $references) |
|
| 591 | } |
||
| 592 | // vim:sw=4:ts=4:sts=4:et: |
||
| 593 |
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: