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 |
||
| 35 | class ExtraPackage |
||
| 36 | { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var Composer $composer |
||
| 40 | */ |
||
| 41 | protected $composer; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var Logger $logger |
||
| 45 | */ |
||
| 46 | protected $logger; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var IOInterface $io |
||
| 50 | */ |
||
| 51 | protected $io; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string $path |
||
| 55 | */ |
||
| 56 | protected $path; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var array $json |
||
| 60 | */ |
||
| 61 | protected $json; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var CompletePackage $package |
||
| 65 | */ |
||
| 66 | protected $package; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param string $path Path to composer.json file |
||
| 70 | * @param Composer $composer |
||
| 71 | * @param Logger $logger |
||
| 72 | */ |
||
| 73 | 84 | public function __construct($path, Composer $composer, Logger $logger, IOInterface $io) |
|
| 74 | { |
||
| 75 | 84 | $this->path = $path; |
|
| 76 | 84 | $this->composer = $composer; |
|
| 77 | 84 | $this->logger = $logger; |
|
| 78 | 84 | $this->io = $io; |
|
| 79 | 84 | $this->json = $this->readPackageJson($path); |
|
| 80 | 84 | $this->package = $this->loadPackage($this->json); |
|
|
|
|||
| 81 | 84 | } |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Get list of additional packages to include if precessing recursively. |
||
| 85 | * |
||
| 86 | * @return array |
||
| 87 | */ |
||
| 88 | 79 | public function getIncludes() |
|
| 89 | { |
||
| 90 | 79 | return isset($this->json['extra']['merge-plugin']['include']) ? |
|
| 91 | 79 | $this->json['extra']['merge-plugin']['include'] : array(); |
|
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Get list of additional packages to require if precessing recursively. |
||
| 96 | * |
||
| 97 | * @return array |
||
| 98 | */ |
||
| 99 | 79 | public function getRequires() |
|
| 100 | { |
||
| 101 | 79 | return isset($this->json['extra']['merge-plugin']['require']) ? |
|
| 102 | 79 | $this->json['extra']['merge-plugin']['require'] : array(); |
|
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Read the contents of a composer.json style file into an array. |
||
| 107 | * |
||
| 108 | * The package contents are fixed up to be usable to create a Package |
||
| 109 | * object by providing dummy "name" and "version" values if they have not |
||
| 110 | * been provided in the file. This is consistent with the default root |
||
| 111 | * package loading behavior of Composer. |
||
| 112 | * |
||
| 113 | * @param string $path |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | 84 | protected function readPackageJson($path) |
|
| 117 | { |
||
| 118 | 84 | if(substr($path, 0, 4) === 'http'){ |
|
| 119 | 4 | $file = new JsonFile($path, new RemoteFilesystem($this->io)); |
|
| 120 | 4 | }else{ |
|
| 121 | 80 | $file = new JsonFile($path); |
|
| 122 | } |
||
| 123 | |||
| 124 | 84 | $json = $file->read(); |
|
| 125 | 84 | if (!isset($json['name'])) { |
|
| 126 | 84 | $json['name'] = 'merge-plugin/' . |
|
| 127 | 84 | strtr($path, DIRECTORY_SEPARATOR, '-'); |
|
| 128 | 84 | } |
|
| 129 | 84 | if (!isset($json['version'])) { |
|
| 130 | 84 | $json['version'] = '1.0.0'; |
|
| 131 | 84 | } |
|
| 132 | 84 | return $json; |
|
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param string $json |
||
| 137 | * @return CompletePackage |
||
| 138 | */ |
||
| 139 | 84 | protected function loadPackage($json) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Merge this package into a RootPackageInterface |
||
| 156 | * |
||
| 157 | * @param RootPackageInterface $root |
||
| 158 | * @param PluginState $state |
||
| 159 | */ |
||
| 160 | 84 | public function mergeInto(RootPackageInterface $root, PluginState $state) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Add a collection of repositories described by the given configuration |
||
| 185 | * to the given package and the global repository manager. |
||
| 186 | * |
||
| 187 | * @param RootPackageInterface $root |
||
| 188 | */ |
||
| 189 | 84 | protected function addRepositories(RootPackageInterface $root) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Merge require or require-dev into a RootPackageInterface |
||
| 219 | * |
||
| 220 | * @param string $type 'require' or 'require-dev' |
||
| 221 | * @param RootPackageInterface $root |
||
| 222 | * @param PluginState $state |
||
| 223 | */ |
||
| 224 | 84 | protected function mergeRequires( |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Merge two collections of package links and collect duplicates for |
||
| 256 | * subsequent processing. |
||
| 257 | * |
||
| 258 | * @param string $type 'require' or 'require-dev' |
||
| 259 | * @param array $origin Primary collection |
||
| 260 | * @param array $merge Additional collection |
||
| 261 | * @param PluginState $state |
||
| 262 | * @return array Merged collection |
||
| 263 | */ |
||
| 264 | 59 | protected function mergeOrDefer( |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Merge autoload or autoload-dev into a RootPackageInterface |
||
| 289 | * |
||
| 290 | * @param string $type 'autoload' or 'devAutoload' |
||
| 291 | * @param RootPackageInterface $root |
||
| 292 | */ |
||
| 293 | 84 | protected function mergeAutoload($type, RootPackageInterface $root) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Fix a collection of paths that are relative to this package to be |
||
| 312 | * relative to the base package. |
||
| 313 | * |
||
| 314 | * @param array $paths |
||
| 315 | * @return array |
||
| 316 | */ |
||
| 317 | 5 | protected function fixRelativePaths(array $paths) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Extract and merge stability flags from the given collection of |
||
| 333 | * requires and merge them into a RootPackageInterface |
||
| 334 | * |
||
| 335 | * @param RootPackageInterface $root |
||
| 336 | * @param array $requires |
||
| 337 | */ |
||
| 338 | 59 | protected function mergeStabilityFlags( |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Merge package links of the given type into a RootPackageInterface |
||
| 354 | * |
||
| 355 | * @param string $type 'conflict', 'replace' or 'provide' |
||
| 356 | * @param RootPackageInterface $root |
||
| 357 | */ |
||
| 358 | 84 | protected function mergePackageLinks($type, RootPackageInterface $root) |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Merge suggested packages into a RootPackageInterface |
||
| 382 | * |
||
| 383 | * @param RootPackageInterface $root |
||
| 384 | */ |
||
| 385 | 84 | protected function mergeSuggests(RootPackageInterface $root) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Merge extra config into a RootPackageInterface |
||
| 399 | * |
||
| 400 | * @param RootPackageInterface $root |
||
| 401 | * @param PluginState $state |
||
| 402 | */ |
||
| 403 | 84 | public function mergeExtra(RootPackageInterface $root, PluginState $state) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Update Links with a 'self.version' constraint with the root package's |
||
| 437 | * version. |
||
| 438 | * |
||
| 439 | * @param string $type Link type |
||
| 440 | * @param array $links |
||
| 441 | * @param RootPackageInterface $root |
||
| 442 | * @return array |
||
| 443 | */ |
||
| 444 | 64 | protected function replaceSelfVersionDependencies( |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Get a full featured Package from a RootPackageInterface. |
||
| 473 | * |
||
| 474 | * In Composer versions before 599ad77 the RootPackageInterface only |
||
| 475 | * defines a sub-set of operations needed by composer-merge-plugin and |
||
| 476 | * RootAliasPackage only implemented those methods defined by the |
||
| 477 | * interface. Most of the unimplemented methods in RootAliasPackage can be |
||
| 478 | * worked around because the getter methods that are implemented proxy to |
||
| 479 | * the aliased package which we can modify by unwrapping. The exception |
||
| 480 | * being modifying the 'conflicts', 'provides' and 'replaces' collections. |
||
| 481 | * We have no way to actually modify those collections unfortunately in |
||
| 482 | * older versions of Composer. |
||
| 483 | * |
||
| 484 | * @param RootPackageInterface $root |
||
| 485 | * @param string $method Method needed |
||
| 486 | * @return RootPackageInterface|RootPackage |
||
| 487 | */ |
||
| 488 | 84 | public static function unwrapIfNeeded( |
|
| 500 | } |
||
| 501 | // vim:sw=4:ts=4:sts=4:et: |
||
| 502 |
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: