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 | 80 | /** |
|
| 67 | * @param string $path Path to composer.json file |
||
| 68 | 80 | * @param Composer $composer |
|
| 69 | 80 | * @param Logger $logger |
|
| 70 | 80 | */ |
|
| 71 | 80 | public function __construct($path, Composer $composer, Logger $logger) |
|
| 80 | 75 | ||
| 81 | /** |
||
| 82 | 75 | * Get list of additional packages to include if precessing recursively. |
|
| 83 | 75 | * |
|
| 84 | * @return array |
||
| 85 | */ |
||
| 86 | public function getIncludes() |
||
| 91 | 75 | ||
| 92 | /** |
||
| 93 | 75 | * Get list of additional packages to require if precessing recursively. |
|
| 94 | 75 | * |
|
| 95 | * @return array |
||
| 96 | */ |
||
| 97 | 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 | 80 | * been provided in the file. This is consistent with the default root |
|
| 109 | * package loading behavior of Composer. |
||
| 110 | 80 | * |
|
| 111 | 80 | * @param string $path |
|
| 112 | 80 | * @return array |
|
| 113 | 80 | */ |
|
| 114 | 80 | protected function readPackageJson($path) |
|
| 127 | |||
| 128 | 80 | /** |
|
| 129 | 80 | * @param string $json |
|
| 130 | * @return CompletePackage |
||
| 131 | */ |
||
| 132 | protected function loadPackage($json) |
||
| 146 | |||
| 147 | 80 | /** |
|
| 148 | * Merge this package into a RootPackageInterface |
||
| 149 | 80 | * |
|
| 150 | * @param RootPackageInterface $root |
||
| 151 | 80 | * @param PluginState $state |
|
| 152 | 80 | */ |
|
| 153 | 75 | public function mergeInto(RootPackageInterface $root, PluginState $state) |
|
| 189 | 5 | ||
| 190 | 5 | /** |
|
| 191 | * Add a collection of repositories described by the given configuration |
||
| 192 | 5 | * to the given package and the global repository manager. |
|
| 193 | 5 | * |
|
| 194 | 5 | * @param RootPackageInterface $root |
|
| 195 | 5 | */ |
|
| 196 | protected function addRepositories(RootPackageInterface $root) |
||
| 223 | |||
| 224 | /** |
||
| 225 | 55 | * Merge require or require-dev into a RootPackageInterface |
|
| 226 | * |
||
| 227 | 55 | * @param string $type 'require' or 'require-dev' |
|
| 228 | 55 | * @param RootPackageInterface $root |
|
| 229 | 55 | * @param PluginState $state |
|
| 230 | */ |
||
| 231 | 55 | protected function mergeRequires( |
|
| 260 | 55 | ||
| 261 | 55 | /** |
|
| 262 | 55 | * Merge two collections of package links and collect duplicates for |
|
| 263 | * subsequent processing. |
||
| 264 | 10 | * |
|
| 265 | 10 | * @param string $type 'require' or 'require-dev' |
|
| 266 | 10 | * @param array $origin Primary collection |
|
| 267 | 10 | * @param array $merge Additional collection |
|
| 268 | * @param PluginState $state |
||
| 269 | 55 | * @return array Merged collection |
|
| 270 | 55 | */ |
|
| 271 | 55 | protected function mergeOrDefer( |
|
| 293 | 5 | ||
| 294 | 5 | /** |
|
| 295 | 5 | * Merge autoload or autoload-dev into a RootPackageInterface |
|
| 296 | * |
||
| 297 | * @param string $type 'autoload' or 'devAutoload' |
||
| 298 | * @param RootPackageInterface $root |
||
| 299 | */ |
||
| 300 | protected function mergeAutoload($type, RootPackageInterface $root) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Fix a collection of paths that are relative to this package to be |
||
| 319 | * relative to the base package. |
||
| 320 | * |
||
| 321 | * @param array $paths |
||
| 322 | * @return array |
||
| 323 | */ |
||
| 324 | protected function fixRelativePaths(array $paths) |
||
| 337 | 55 | ||
| 338 | /** |
||
| 339 | * Extract and merge stability flags from the given collection of |
||
| 340 | * requires and merge them into a RootPackageInterface |
||
| 341 | * |
||
| 342 | * @param RootPackageInterface $root |
||
| 343 | * @param array $requires |
||
| 344 | */ |
||
| 345 | 80 | protected function mergeStabilityFlags( |
|
| 358 | |||
| 359 | /** |
||
| 360 | 10 | * Merge package links of the given type into a RootPackageInterface |
|
| 361 | 10 | * |
|
| 362 | 10 | * @param string $type 'conflict', 'replace' or 'provide' |
|
| 363 | 10 | * @param RootPackageInterface $root |
|
| 364 | 10 | */ |
|
| 365 | 80 | protected function mergePackageLinks($type, RootPackageInterface $root) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Merge suggested packages into a RootPackageInterface |
||
| 389 | * |
||
| 390 | 80 | * @param RootPackageInterface $root |
|
| 391 | */ |
||
| 392 | 80 | protected function mergeSuggests(RootPackageInterface $root) |
|
| 403 | 5 | ||
| 404 | 5 | /** |
|
| 405 | * Merge extra config into a RootPackageInterface |
||
| 406 | 5 | * |
|
| 407 | 10 | * @param RootPackageInterface $root |
|
| 408 | 10 | * @param PluginState $state |
|
| 409 | 10 | */ |
|
| 410 | 10 | public function mergeExtra(RootPackageInterface $root, PluginState $state) |
|
| 441 | 60 | ||
| 442 | 60 | /** |
|
| 443 | 60 | * Update Links with a 'self.version' constraint with the root package's |
|
| 444 | 5 | * version. |
|
| 445 | 5 | * |
|
| 446 | 5 | * @param string $type Link type |
|
| 447 | 5 | * @param array $links |
|
| 448 | 5 | * @param RootPackageInterface $root |
|
| 449 | * @return array |
||
| 450 | 5 | */ |
|
| 451 | protected function replaceSelfVersionDependencies( |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Get a full featured Package from a RootPackageInterface. |
||
| 479 | 80 | * |
|
| 480 | * In Composer versions before 599ad77 the RootPackageInterface only |
||
| 481 | 80 | * defines a sub-set of operations needed by composer-merge-plugin and |
|
| 482 | * RootAliasPackage only implemented those methods defined by the |
||
| 483 | * interface. Most of the unimplemented methods in RootAliasPackage can be |
||
| 484 | * worked around because the getter methods that are implemented proxy to |
||
| 485 | 80 | * the aliased package which we can modify by unwrapping. The exception |
|
| 486 | * being modifying the 'conflicts', 'provides' and 'replaces' collections. |
||
| 487 | * We have no way to actually modify those collections unfortunately in |
||
| 488 | * older versions of Composer. |
||
| 489 | * |
||
| 490 | * @param RootPackageInterface $root |
||
| 491 | * @param string $method Method needed |
||
| 492 | * @return RootPackageInterface|RootPackage |
||
| 493 | */ |
||
| 494 | public static function unwrapIfNeeded( |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Extract vcs revision from version constraint (dev-master#abc123. |
||
| 509 | * |
||
| 510 | * @param array $requires |
||
| 511 | * @param array $references |
||
| 512 | * @return array |
||
| 513 | * @see RootPackageLoader::extractReferences() |
||
| 514 | */ |
||
| 515 | protected function extractReferences(array $requires, array $references) |
||
| 527 | } |
||
| 528 | // vim:sw=4:ts=4:sts=4:et: |
||
| 529 |
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: