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) |
|
| 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 | * @param bool $prepend Should repositories be prepended to repository manager. |
||
| 183 | */ |
||
| 184 | 100 | protected function addRepositories(RootPackageInterface $root, $prepend) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Merge require or require-dev into a RootPackageInterface |
||
| 229 | * |
||
| 230 | * @param string $type 'require' or 'require-dev' |
||
| 231 | * @param RootPackageInterface $root |
||
| 232 | * @param PluginState $state |
||
| 233 | */ |
||
| 234 | 100 | protected function mergeRequires( |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Merge two collections of package links and collect duplicates for |
||
| 266 | * subsequent processing. |
||
| 267 | * |
||
| 268 | * @param string $type 'require' or 'require-dev' |
||
| 269 | * @param array $origin Primary collection |
||
| 270 | * @param array $merge Additional collection |
||
| 271 | * @param PluginState $state |
||
| 272 | * @return array Merged collection |
||
| 273 | */ |
||
| 274 | 65 | protected function mergeOrDefer( |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Merge autoload or autoload-dev into a RootPackageInterface |
||
| 299 | * |
||
| 300 | * @param string $type 'autoload' or 'devAutoload' |
||
| 301 | * @param RootPackageInterface $root |
||
| 302 | */ |
||
| 303 | 100 | protected function mergeAutoload($type, RootPackageInterface $root) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Fix a collection of paths that are relative to this package to be |
||
| 322 | * relative to the base package. |
||
| 323 | * |
||
| 324 | * @param array $paths |
||
| 325 | * @return array |
||
| 326 | */ |
||
| 327 | 10 | protected function fixRelativePaths(array $paths) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Extract and merge stability flags from the given collection of |
||
| 343 | * requires and merge them into a RootPackageInterface |
||
| 344 | * |
||
| 345 | * @param RootPackageInterface $root |
||
| 346 | * @param array $requires |
||
| 347 | */ |
||
| 348 | 65 | protected function mergeStabilityFlags( |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Merge package links of the given type into a RootPackageInterface |
||
| 364 | * |
||
| 365 | * @param string $type 'conflict', 'replace' or 'provide' |
||
| 366 | * @param RootPackageInterface $root |
||
| 367 | */ |
||
| 368 | 100 | protected function mergePackageLinks($type, RootPackageInterface $root) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Merge suggested packages into a RootPackageInterface |
||
| 394 | * |
||
| 395 | * @param RootPackageInterface $root |
||
| 396 | */ |
||
| 397 | 100 | protected function mergeSuggests(RootPackageInterface $root) |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Merge extra config into a RootPackageInterface |
||
| 411 | * |
||
| 412 | * @param RootPackageInterface $root |
||
| 413 | * @param PluginState $state |
||
| 414 | */ |
||
| 415 | 100 | public function mergeExtra(RootPackageInterface $root, PluginState $state) |
|
| 446 | |||
| 447 | /** |
||
| 448 | * Update Links with a 'self.version' constraint with the root package's |
||
| 449 | * version. |
||
| 450 | * |
||
| 451 | * @param string $type Link type |
||
| 452 | * @param array $links |
||
| 453 | * @param RootPackageInterface $root |
||
| 454 | * @return array |
||
| 455 | */ |
||
| 456 | 75 | protected function replaceSelfVersionDependencies( |
|
| 497 | |||
| 498 | /** |
||
| 499 | * Get a full featured Package from a RootPackageInterface. |
||
| 500 | * |
||
| 501 | * In Composer versions before 599ad77 the RootPackageInterface only |
||
| 502 | * defines a sub-set of operations needed by composer-merge-plugin and |
||
| 503 | * RootAliasPackage only implemented those methods defined by the |
||
| 504 | * interface. Most of the unimplemented methods in RootAliasPackage can be |
||
| 505 | * worked around because the getter methods that are implemented proxy to |
||
| 506 | * the aliased package which we can modify by unwrapping. The exception |
||
| 507 | * being modifying the 'conflicts', 'provides' and 'replaces' collections. |
||
| 508 | * We have no way to actually modify those collections unfortunately in |
||
| 509 | * older versions of Composer. |
||
| 510 | * |
||
| 511 | * @param RootPackageInterface $root |
||
| 512 | * @param string $method Method needed |
||
| 513 | * @return RootPackageInterface|RootPackage |
||
| 514 | */ |
||
| 515 | 100 | public static function unwrapIfNeeded( |
|
| 529 | |||
| 530 | /** |
||
| 531 | * Update the root packages reference information. |
||
| 532 | * |
||
| 533 | * @param RootPackageInterface $root |
||
| 534 | */ |
||
| 535 | 100 | protected function mergeReferences(RootPackageInterface $root) |
|
| 552 | |||
| 553 | /** |
||
| 554 | * Extract vcs revision from version constraint (dev-master#abc123. |
||
| 555 | * |
||
| 556 | * @param array $requires |
||
| 557 | * @param array $references |
||
| 558 | * @return array |
||
| 559 | * @see RootPackageLoader::extractReferences() |
||
| 560 | */ |
||
| 561 | 100 | protected function extractReferences(array $requires, array $references) |
|
| 577 | } |
||
| 578 | // vim:sw=4:ts=4:sts=4:et: |
||
| 579 |
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: