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 | * @param string $path Path to composer.json file |
||
| 63 | * @param Composer $composer |
||
| 64 | * @param Logger $logger |
||
| 65 | */ |
||
| 66 | 90 | public function __construct($path, Composer $composer, Logger $logger) |
|
| 67 | { |
||
| 68 | 90 | $this->path = $path; |
|
| 69 | 90 | $this->composer = $composer; |
|
| 70 | 90 | $this->logger = $logger; |
|
| 71 | 90 | $this->json = $this->readPackageJson($path); |
|
| 72 | 90 | $this->package = $this->loadPackage($this->json); |
|
|
|
|||
| 73 | 90 | } |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Get list of additional packages to include if precessing recursively. |
||
| 77 | * |
||
| 78 | * @return array |
||
| 79 | */ |
||
| 80 | 85 | public function getIncludes() |
|
| 81 | { |
||
| 82 | 85 | return isset($this->json['extra']['merge-plugin']['include']) ? |
|
| 83 | 85 | $this->json['extra']['merge-plugin']['include'] : array(); |
|
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Get list of additional packages to require if precessing recursively. |
||
| 88 | * |
||
| 89 | * @return array |
||
| 90 | */ |
||
| 91 | 85 | public function getRequires() |
|
| 92 | { |
||
| 93 | 85 | return isset($this->json['extra']['merge-plugin']['require']) ? |
|
| 94 | 85 | $this->json['extra']['merge-plugin']['require'] : array(); |
|
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Read the contents of a composer.json style file into an array. |
||
| 99 | * |
||
| 100 | * The package contents are fixed up to be usable to create a Package |
||
| 101 | * object by providing dummy "name" and "version" values if they have not |
||
| 102 | * been provided in the file. This is consistent with the default root |
||
| 103 | * package loading behavior of Composer. |
||
| 104 | * |
||
| 105 | * @param string $path |
||
| 106 | * @return array |
||
| 107 | */ |
||
| 108 | 90 | protected function readPackageJson($path) |
|
| 109 | { |
||
| 110 | 90 | $file = new JsonFile($path); |
|
| 111 | 90 | $json = $file->read(); |
|
| 112 | 90 | if (!isset($json['name'])) { |
|
| 113 | 85 | $json['name'] = 'merge-plugin/' . |
|
| 114 | 85 | strtr($path, DIRECTORY_SEPARATOR, '-'); |
|
| 115 | 85 | } |
|
| 116 | 90 | if (!isset($json['version'])) { |
|
| 117 | 90 | $json['version'] = '1.0.0'; |
|
| 118 | 90 | } |
|
| 119 | 90 | return $json; |
|
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param string $json |
||
| 124 | * @return CompletePackage |
||
| 125 | */ |
||
| 126 | 90 | protected function loadPackage($json) |
|
| 127 | { |
||
| 128 | 90 | $loader = new ArrayLoader(); |
|
| 129 | 90 | $package = $loader->load($json); |
|
| 130 | // @codeCoverageIgnoreStart |
||
| 131 | if (!$package instanceof CompletePackage) { |
||
| 132 | throw new UnexpectedValueException( |
||
| 133 | 'Expected instance of CompletePackage, got ' . |
||
| 134 | get_class($package) |
||
| 135 | ); |
||
| 136 | } |
||
| 137 | // @codeCoverageIgnoreEnd |
||
| 138 | 90 | return $package; |
|
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Merge this package into a RootPackageInterface |
||
| 143 | * |
||
| 144 | * @param RootPackageInterface $root |
||
| 145 | * @param PluginState $state |
||
| 146 | */ |
||
| 147 | 90 | public function mergeInto(RootPackageInterface $root, PluginState $state) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Add a collection of repositories described by the given configuration |
||
| 172 | * to the given package and the global repository manager. |
||
| 173 | * |
||
| 174 | * @param RootPackageInterface $root |
||
| 175 | */ |
||
| 176 | 90 | protected function addRepositories(RootPackageInterface $root) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Merge require or require-dev into a RootPackageInterface |
||
| 206 | * |
||
| 207 | * @param string $type 'require' or 'require-dev' |
||
| 208 | * @param RootPackageInterface $root |
||
| 209 | * @param PluginState $state |
||
| 210 | */ |
||
| 211 | 90 | protected function mergeRequires( |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Merge two collections of package links and collect duplicates for |
||
| 243 | * subsequent processing. |
||
| 244 | * |
||
| 245 | * @param string $type 'require' or 'require-dev' |
||
| 246 | * @param array $origin Primary collection |
||
| 247 | * @param array $merge Additional collection |
||
| 248 | * @param PluginState $state |
||
| 249 | * @return array Merged collection |
||
| 250 | */ |
||
| 251 | 60 | protected function mergeOrDefer( |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Merge autoload or autoload-dev into a RootPackageInterface |
||
| 276 | * |
||
| 277 | * @param string $type 'autoload' or 'devAutoload' |
||
| 278 | * @param RootPackageInterface $root |
||
| 279 | */ |
||
| 280 | 90 | protected function mergeAutoload($type, RootPackageInterface $root) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Fix a collection of paths that are relative to this package to be |
||
| 299 | * relative to the base package. |
||
| 300 | * |
||
| 301 | * @param array $paths |
||
| 302 | * @return array |
||
| 303 | */ |
||
| 304 | 10 | protected function fixRelativePaths(array $paths) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Extract and merge stability flags from the given collection of |
||
| 320 | * requires and merge them into a RootPackageInterface |
||
| 321 | * |
||
| 322 | * @param RootPackageInterface $root |
||
| 323 | * @param array $requires |
||
| 324 | */ |
||
| 325 | 60 | protected function mergeStabilityFlags( |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Merge package links of the given type into a RootPackageInterface |
||
| 341 | * |
||
| 342 | * @param string $type 'conflict', 'replace' or 'provide' |
||
| 343 | * @param RootPackageInterface $root |
||
| 344 | */ |
||
| 345 | 90 | protected function mergePackageLinks($type, RootPackageInterface $root) |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Merge suggested packages into a RootPackageInterface |
||
| 371 | * |
||
| 372 | * @param RootPackageInterface $root |
||
| 373 | */ |
||
| 374 | 90 | protected function mergeSuggests(RootPackageInterface $root) |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Merge extra config into a RootPackageInterface |
||
| 388 | * |
||
| 389 | * @param RootPackageInterface $root |
||
| 390 | * @param PluginState $state |
||
| 391 | */ |
||
| 392 | 90 | public function mergeExtra(RootPackageInterface $root, PluginState $state) |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Merges multiple arrays, recursively, and returns the merged array. |
||
| 430 | * |
||
| 431 | * This function is similar to PHP's array_merge_recursive() function, but it |
||
| 432 | * handles non-array values differently. When merging values that are not both |
||
| 433 | 70 | * arrays, the latter value replaces the former rather than merging with it. |
|
| 434 | * |
||
| 435 | * Example: |
||
| 436 | * @code |
||
| 437 | * $link_options_1 = array('fragment' => 'x', 'attributes' => array('title' => t('X'), 'class' => array('a', 'b'))); |
||
| 438 | 70 | * $link_options_2 = array('fragment' => 'y', 'attributes' => array('title' => t('Y'), 'class' => array('c', 'd'))); |
|
| 439 | 70 | * |
|
| 440 | 70 | * // This results in array( |
|
| 441 | 70 | * // 'fragment' => array('x', 'y'), |
|
| 442 | * // 'attributes' => array('title' => array(t('X'), t('Y')), 'class' => array('a', 'b', 'c', 'd')) |
||
| 443 | 70 | * // ). |
|
| 444 | 70 | * $incorrect = array_merge_recursive($link_options_1, $link_options_2); |
|
| 445 | * |
||
| 446 | 70 | * // This results in array( |
|
| 447 | 70 | * // 'fragment' => 'y', |
|
| 448 | 70 | * // 'attributes' => array('title' => t('Y'), 'class' => array('a', 'b', 'c', 'd')) |
|
| 449 | 10 | * // ). |
|
| 450 | * $correct = $this->arrayMergeDeep($link_options_1, $link_options_2); |
||
| 451 | 5 | * @endcode |
|
| 452 | 5 | * |
|
| 453 | 5 | * Note: This function was derived from Drupal's drupal_array_merge_deep(). |
|
| 454 | 5 | * |
|
| 455 | 5 | * @param array ... |
|
| 456 | 5 | * Arrays to merge. |
|
| 457 | 5 | * |
|
| 458 | 5 | * @return array |
|
| 459 | * The merged array. |
||
| 460 | */ |
||
| 461 | 5 | protected function arrayMergeDeep() |
|
| 485 | |||
| 486 | /** |
||
| 487 | * Update Links with a 'self.version' constraint with the root package's |
||
| 488 | * version. |
||
| 489 | * |
||
| 490 | * @param string $type Link type |
||
| 491 | * @param array $links |
||
| 492 | 90 | * @param RootPackageInterface $root |
|
| 493 | * @return array |
||
| 494 | */ |
||
| 495 | protected function replaceSelfVersionDependencies( |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Get a full featured Package from a RootPackageInterface. |
||
| 539 | * |
||
| 540 | * In Composer versions before 599ad77 the RootPackageInterface only |
||
| 541 | * defines a sub-set of operations needed by composer-merge-plugin and |
||
| 542 | * RootAliasPackage only implemented those methods defined by the |
||
| 543 | * interface. Most of the unimplemented methods in RootAliasPackage can be |
||
| 544 | * worked around because the getter methods that are implemented proxy to |
||
| 545 | * the aliased package which we can modify by unwrapping. The exception |
||
| 546 | * being modifying the 'conflicts', 'provides' and 'replaces' collections. |
||
| 547 | * We have no way to actually modify those collections unfortunately in |
||
| 548 | * older versions of Composer. |
||
| 549 | * |
||
| 550 | * @param RootPackageInterface $root |
||
| 551 | * @param string $method Method needed |
||
| 552 | * @return RootPackageInterface|RootPackage |
||
| 553 | */ |
||
| 554 | public static function unwrapIfNeeded( |
||
| 568 | } |
||
| 569 | // vim:sw=4:ts=4:sts=4:et: |
||
| 570 |
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: