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) |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Get list of additional packages to include if precessing recursively. |
||
| 83 | * |
||
| 84 | * @return array |
||
| 85 | */ |
||
| 86 | 90 | public function getIncludes() |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Get list of additional packages to require if precessing recursively. |
||
| 94 | * |
||
| 95 | * @return array |
||
| 96 | */ |
||
| 97 | 90 | 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 | 95 | protected function readPackageJson($path) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * @param string $json |
||
| 130 | * @return CompletePackage |
||
| 131 | */ |
||
| 132 | 95 | protected function loadPackage($json) |
|
| 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) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Add a collection of repositories described by the given configuration |
||
| 179 | * to the given package and the global repository manager. |
||
| 180 | * |
||
| 181 | * @param RootPackageInterface $root |
||
| 182 | */ |
||
| 183 | 95 | protected function addRepositories(RootPackageInterface $root) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Merge require or require-dev into a RootPackageInterface |
||
| 213 | * |
||
| 214 | * @param string $type 'require' or 'require-dev' |
||
| 215 | * @param RootPackageInterface $root |
||
| 216 | * @param PluginState $state |
||
| 217 | */ |
||
| 218 | 95 | protected function mergeRequires( |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Merge two collections of package links and collect duplicates for |
||
| 250 | * subsequent processing. |
||
| 251 | * |
||
| 252 | * @param string $type 'require' or 'require-dev' |
||
| 253 | * @param array $origin Primary collection |
||
| 254 | * @param array $merge Additional collection |
||
| 255 | * @param PluginState $state |
||
| 256 | * @return array Merged collection |
||
| 257 | */ |
||
| 258 | 65 | protected function mergeOrDefer( |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Merge autoload or autoload-dev into a RootPackageInterface |
||
| 283 | * |
||
| 284 | * @param string $type 'autoload' or 'devAutoload' |
||
| 285 | * @param RootPackageInterface $root |
||
| 286 | */ |
||
| 287 | 95 | protected function mergeAutoload($type, RootPackageInterface $root) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Fix a collection of paths that are relative to this package to be |
||
| 306 | * relative to the base package. |
||
| 307 | * |
||
| 308 | * @param array $paths |
||
| 309 | * @return array |
||
| 310 | */ |
||
| 311 | 10 | protected function fixRelativePaths(array $paths) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Extract and merge stability flags from the given collection of |
||
| 327 | * requires and merge them into a RootPackageInterface |
||
| 328 | * |
||
| 329 | * @param RootPackageInterface $root |
||
| 330 | * @param array $requires |
||
| 331 | */ |
||
| 332 | 65 | protected function mergeStabilityFlags( |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Merge package links of the given type into a RootPackageInterface |
||
| 348 | * |
||
| 349 | * @param string $type 'conflict', 'replace' or 'provide' |
||
| 350 | * @param RootPackageInterface $root |
||
| 351 | */ |
||
| 352 | 95 | protected function mergePackageLinks($type, RootPackageInterface $root) |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Merge suggested packages into a RootPackageInterface |
||
| 378 | * |
||
| 379 | * @param RootPackageInterface $root |
||
| 380 | */ |
||
| 381 | 95 | protected function mergeSuggests(RootPackageInterface $root) |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Merge extra config into a RootPackageInterface |
||
| 395 | * |
||
| 396 | * @param RootPackageInterface $root |
||
| 397 | * @param PluginState $state |
||
| 398 | */ |
||
| 399 | 95 | public function mergeExtra(RootPackageInterface $root, PluginState $state) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Merges two arrays either via arrayMergeDeep or via array_merge. |
||
| 434 | * |
||
| 435 | * @param bool $mergeDeep |
||
| 436 | * @param array $array1 |
||
| 437 | * @param array $array2 |
||
| 438 | * @return array |
||
| 439 | */ |
||
| 440 | 75 | protected function arrayMerge($mergeDeep, $array1, $array2) |
|
| 448 | 75 | ||
| 449 | /** |
||
| 450 | 75 | * Merges multiple arrays, recursively, and returns the merged array. |
|
| 451 | 75 | * |
|
| 452 | * This function is similar to PHP's array_merge_recursive() function, but it |
||
| 453 | 75 | * handles non-array values differently. When merging values that are not both |
|
| 454 | 75 | * arrays, the latter value replaces the former rather than merging with it. |
|
| 455 | 75 | * |
|
| 456 | 10 | * Example: |
|
| 457 | * @code |
||
| 458 | 5 | * $link_options_1 = array('fragment' => 'x', 'attributes' => array('title' => t('X'), 'class' => array('a', 'b'))); |
|
| 459 | 5 | * $link_options_2 = array('fragment' => 'y', 'attributes' => array('title' => t('Y'), 'class' => array('c', 'd'))); |
|
| 460 | 5 | * |
|
| 461 | 5 | * // This results in array( |
|
| 462 | 5 | * // 'fragment' => array('x', 'y'), |
|
| 463 | 5 | * // 'attributes' => array('title' => array(t('X'), t('Y')), 'class' => array('a', 'b', 'c', 'd')) |
|
| 464 | 5 | * // ). |
|
| 465 | 5 | * $incorrect = array_merge_recursive($link_options_1, $link_options_2); |
|
| 466 | * |
||
| 467 | * // This results in array( |
||
| 468 | 5 | * // 'fragment' => 'y', |
|
| 469 | 5 | * // 'attributes' => array('title' => t('Y'), 'class' => array('a', 'b', 'c', 'd')) |
|
| 470 | 5 | * // ). |
|
| 471 | 5 | * $correct = $this->arrayMergeDeep($link_options_1, $link_options_2); |
|
| 472 | 5 | * @endcode |
|
| 473 | * |
||
| 474 | 5 | * Note: This function was derived from Drupal's drupal_array_merge_deep(). |
|
| 475 | * |
||
| 476 | 75 | * @param array ... |
|
| 477 | 75 | * Arrays to merge. |
|
| 478 | * |
||
| 479 | 75 | * @return array |
|
| 480 | * The merged array. |
||
| 481 | */ |
||
| 482 | protected function arrayMergeDeep() |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Update Links with a 'self.version' constraint with the root package's |
||
| 509 | * version. |
||
| 510 | * |
||
| 511 | 95 | * @param string $type Link type |
|
| 512 | * @param array $links |
||
| 513 | * @param RootPackageInterface $root |
||
| 514 | * @return array |
||
| 515 | */ |
||
| 516 | protected function replaceSelfVersionDependencies( |
||
| 557 | 95 | ||
| 558 | /** |
||
| 559 | 95 | * Get a full featured Package from a RootPackageInterface. |
|
| 560 | * |
||
| 561 | * In Composer versions before 599ad77 the RootPackageInterface only |
||
| 562 | * defines a sub-set of operations needed by composer-merge-plugin and |
||
| 563 | * RootAliasPackage only implemented those methods defined by the |
||
| 564 | * interface. Most of the unimplemented methods in RootAliasPackage can be |
||
| 565 | * worked around because the getter methods that are implemented proxy to |
||
| 566 | * the aliased package which we can modify by unwrapping. The exception |
||
| 567 | * being modifying the 'conflicts', 'provides' and 'replaces' collections. |
||
| 568 | * We have no way to actually modify those collections unfortunately in |
||
| 569 | * older versions of Composer. |
||
| 570 | * |
||
| 571 | * @param RootPackageInterface $root |
||
| 572 | * @param string $method Method needed |
||
| 573 | * @return RootPackageInterface|RootPackage |
||
| 574 | */ |
||
| 575 | public static function unwrapIfNeeded( |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Update the root packages reference information. |
||
| 592 | * |
||
| 593 | * @param RootPackageInterface $root |
||
| 594 | */ |
||
| 595 | protected function mergeReferences(RootPackageInterface $root) |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Extract vcs revision from version constraint (dev-master#abc123. |
||
| 615 | * |
||
| 616 | * @param array $requires |
||
| 617 | * @param array $references |
||
| 618 | * @return array |
||
| 619 | * @see RootPackageLoader::extractReferences() |
||
| 620 | */ |
||
| 621 | protected function extractReferences(array $requires, array $references) |
||
| 637 | } |
||
| 638 | // vim:sw=4:ts=4:sts=4:et: |
||
| 639 |
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: