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 | * @param bool $prepend Should repositories be prepended to repository manager. |
||
| 195 | */ |
||
| 196 | 100 | protected function addRepositories(RootPackageInterface $root, $prepend) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Merge require or require-dev into a RootPackageInterface |
||
| 241 | * |
||
| 242 | * @param string $type 'require' or 'require-dev' |
||
| 243 | * @param RootPackageInterface $root |
||
| 244 | * @param PluginState $state |
||
| 245 | */ |
||
| 246 | 100 | protected function mergeRequires( |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Merge two collections of package links and collect duplicates for |
||
| 278 | * subsequent processing. |
||
| 279 | * |
||
| 280 | * @param string $type 'require' or 'require-dev' |
||
| 281 | * @param array $origin Primary collection |
||
| 282 | * @param array $merge Additional collection |
||
| 283 | * @param PluginState $state |
||
| 284 | * @return array Merged collection |
||
| 285 | */ |
||
| 286 | 65 | protected function mergeOrDefer( |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Merge autoload or autoload-dev into a RootPackageInterface |
||
| 311 | * |
||
| 312 | * @param string $type 'autoload' or 'devAutoload' |
||
| 313 | * @param RootPackageInterface $root |
||
| 314 | */ |
||
| 315 | 100 | protected function mergeAutoload($type, RootPackageInterface $root) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Fix a collection of paths that are relative to this package to be |
||
| 334 | * relative to the base package. |
||
| 335 | * |
||
| 336 | * @param array $paths |
||
| 337 | * @return array |
||
| 338 | */ |
||
| 339 | 10 | protected function fixRelativePaths(array $paths) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Extract and merge stability flags from the given collection of |
||
| 355 | * requires and merge them into a RootPackageInterface |
||
| 356 | * |
||
| 357 | * @param RootPackageInterface $root |
||
| 358 | * @param array $requires |
||
| 359 | */ |
||
| 360 | 65 | protected function mergeStabilityFlags( |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Merge package links of the given type into a RootPackageInterface |
||
| 376 | * |
||
| 377 | * @param string $type 'conflict', 'replace' or 'provide' |
||
| 378 | * @param RootPackageInterface $root |
||
| 379 | */ |
||
| 380 | 100 | protected function mergePackageLinks($type, RootPackageInterface $root) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Merge suggested packages into a RootPackageInterface |
||
| 406 | * |
||
| 407 | * @param RootPackageInterface $root |
||
| 408 | */ |
||
| 409 | 100 | protected function mergeSuggests(RootPackageInterface $root) |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Merge extra config into a RootPackageInterface |
||
| 423 | * |
||
| 424 | * @param RootPackageInterface $root |
||
| 425 | * @param PluginState $state |
||
| 426 | */ |
||
| 427 | 100 | public function mergeExtra(RootPackageInterface $root, PluginState $state) |
|
| 459 | |||
| 460 | /** |
||
| 461 | * Merges two arrays either via arrayMergeDeep or via array_merge. |
||
| 462 | * |
||
| 463 | * @param bool $mergeDeep |
||
| 464 | * @param array $array1 |
||
| 465 | * @param array $array2 |
||
| 466 | * @return array |
||
| 467 | */ |
||
| 468 | 15 | public static function mergeExtraArray($mergeDeep, $array1, $array2) |
|
| 476 | |||
| 477 | /** |
||
| 478 | * Update Links with a 'self.version' constraint with the root package's |
||
| 479 | * version. |
||
| 480 | * |
||
| 481 | * @param string $type Link type |
||
| 482 | * @param array $links |
||
| 483 | * @param RootPackageInterface $root |
||
| 484 | * @return array |
||
| 485 | */ |
||
| 486 | 75 | protected function replaceSelfVersionDependencies( |
|
| 527 | |||
| 528 | /** |
||
| 529 | * Get a full featured Package from a RootPackageInterface. |
||
| 530 | * |
||
| 531 | * In Composer versions before 599ad77 the RootPackageInterface only |
||
| 532 | * defines a sub-set of operations needed by composer-merge-plugin and |
||
| 533 | * RootAliasPackage only implemented those methods defined by the |
||
| 534 | * interface. Most of the unimplemented methods in RootAliasPackage can be |
||
| 535 | * worked around because the getter methods that are implemented proxy to |
||
| 536 | * the aliased package which we can modify by unwrapping. The exception |
||
| 537 | * being modifying the 'conflicts', 'provides' and 'replaces' collections. |
||
| 538 | * We have no way to actually modify those collections unfortunately in |
||
| 539 | * older versions of Composer. |
||
| 540 | * |
||
| 541 | * @param RootPackageInterface $root |
||
| 542 | * @param string $method Method needed |
||
| 543 | * @return RootPackageInterface|RootPackage |
||
| 544 | */ |
||
| 545 | 100 | public static function unwrapIfNeeded( |
|
| 559 | |||
| 560 | /** |
||
| 561 | * Update the root packages reference information. |
||
| 562 | * |
||
| 563 | * @param RootPackageInterface $root |
||
| 564 | */ |
||
| 565 | 100 | protected function mergeReferences(RootPackageInterface $root) |
|
| 582 | |||
| 583 | /** |
||
| 584 | * Extract vcs revision from version constraint (dev-master#abc123. |
||
| 585 | * |
||
| 586 | * @param array $requires |
||
| 587 | * @param array $references |
||
| 588 | * @return array |
||
| 589 | * @see RootPackageLoader::extractReferences() |
||
| 590 | */ |
||
| 591 | 100 | protected function extractReferences(array $requires, array $references) |
|
| 607 | } |
||
| 608 | // vim:sw=4:ts=4:sts=4:et: |
||
| 609 |
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: