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 |
||
| 34 | class ExtraPackage |
||
| 35 | { |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var Composer $composer |
||
| 39 | */ |
||
| 40 | protected $composer; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var Logger $logger |
||
| 44 | */ |
||
| 45 | protected $logger; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string $path |
||
| 49 | */ |
||
| 50 | protected $path; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array $json |
||
| 54 | */ |
||
| 55 | protected $json; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var CompletePackage $package |
||
| 59 | */ |
||
| 60 | protected $package; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var VersionParser $versionParser |
||
| 64 | */ |
||
| 65 | protected $versionParser; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param string $path Path to composer.json file |
||
| 69 | * @param Composer $composer |
||
| 70 | * @param Logger $logger |
||
| 71 | */ |
||
| 72 | 95 | public function __construct($path, Composer $composer, Logger $logger) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Get list of additional packages to include if precessing recursively. |
||
| 84 | * |
||
| 85 | * @return array |
||
| 86 | */ |
||
| 87 | 90 | public function getIncludes() |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Get list of additional packages to require if precessing recursively. |
||
| 95 | * |
||
| 96 | * @return array |
||
| 97 | */ |
||
| 98 | 90 | public function getRequires() |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Read the contents of a composer.json style file into an array. |
||
| 106 | * |
||
| 107 | * The package contents are fixed up to be usable to create a Package |
||
| 108 | * object by providing dummy "name" and "version" values if they have not |
||
| 109 | * been provided in the file. This is consistent with the default root |
||
| 110 | * package loading behavior of Composer. |
||
| 111 | * |
||
| 112 | * @param string $path |
||
| 113 | * @return array |
||
| 114 | */ |
||
| 115 | 95 | protected function readPackageJson($path) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * @param string $json |
||
| 131 | * @return CompletePackage |
||
| 132 | */ |
||
| 133 | 95 | protected function loadPackage($json) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Merge this package into a RootPackageInterface |
||
| 150 | * |
||
| 151 | * @param RootPackageInterface $root |
||
| 152 | * @param PluginState $state |
||
| 153 | */ |
||
| 154 | 95 | public function mergeInto(RootPackageInterface $root, PluginState $state) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Add a collection of repositories described by the given configuration |
||
| 180 | * to the given package and the global repository manager. |
||
| 181 | * |
||
| 182 | * @param RootPackageInterface $root |
||
| 183 | */ |
||
| 184 | 95 | protected function addRepositories(RootPackageInterface $root) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Merge require or require-dev into a RootPackageInterface |
||
| 214 | * |
||
| 215 | * @param string $type 'require' or 'require-dev' |
||
| 216 | * @param RootPackageInterface $root |
||
| 217 | * @param PluginState $state |
||
| 218 | */ |
||
| 219 | 95 | protected function mergeRequires( |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Merge two collections of package links and collect duplicates for |
||
| 251 | * subsequent processing. |
||
| 252 | * |
||
| 253 | * @param string $type 'require' or 'require-dev' |
||
| 254 | * @param array $origin Primary collection |
||
| 255 | * @param array $merge Additional collection |
||
| 256 | * @param PluginState $state |
||
| 257 | * @return array Merged collection |
||
| 258 | */ |
||
| 259 | 65 | protected function mergeOrDefer( |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Merge autoload or autoload-dev into a RootPackageInterface |
||
| 284 | * |
||
| 285 | * @param string $type 'autoload' or 'devAutoload' |
||
| 286 | * @param RootPackageInterface $root |
||
| 287 | */ |
||
| 288 | 95 | protected function mergeAutoload($type, RootPackageInterface $root) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Fix a collection of paths that are relative to this package to be |
||
| 307 | * relative to the base package. |
||
| 308 | * |
||
| 309 | * @param array $paths |
||
| 310 | * @return array |
||
| 311 | */ |
||
| 312 | 10 | protected function fixRelativePaths(array $paths) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Extract and merge stability flags from the given collection of |
||
| 328 | * requires and merge them into a RootPackageInterface |
||
| 329 | * |
||
| 330 | * @param RootPackageInterface $root |
||
| 331 | * @param array $requires |
||
| 332 | */ |
||
| 333 | 65 | protected function mergeStabilityFlags( |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Merge package links of the given type into a RootPackageInterface |
||
| 349 | * |
||
| 350 | * @param string $type 'conflict', 'replace' or 'provide' |
||
| 351 | * @param RootPackageInterface $root |
||
| 352 | */ |
||
| 353 | 95 | protected function mergePackageLinks($type, RootPackageInterface $root) |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Merge suggested packages into a RootPackageInterface |
||
| 379 | * |
||
| 380 | * @param RootPackageInterface $root |
||
| 381 | */ |
||
| 382 | 95 | protected function mergeSuggests(RootPackageInterface $root) |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Merge extra config into a RootPackageInterface |
||
| 396 | * |
||
| 397 | * @param RootPackageInterface $root |
||
| 398 | * @param PluginState $state |
||
| 399 | */ |
||
| 400 | 95 | public function mergeExtra(RootPackageInterface $root, PluginState $state) |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Merges two arrays either via arrayMergeDeep or via array_merge. |
||
| 435 | * |
||
| 436 | * @param bool $mergeDeep |
||
| 437 | * @param array $array1 |
||
| 438 | * @param array $array2 |
||
| 439 | * @return array |
||
| 440 | */ |
||
| 441 | 15 | public static function mergeExtraArray($mergeDeep, $array1, $array2) |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Update Links with a 'self.version' constraint with the root package's |
||
| 452 | * version. |
||
| 453 | * |
||
| 454 | * @param string $type Link type |
||
| 455 | * @param array $links |
||
| 456 | * @param RootPackageInterface $root |
||
| 457 | * @return array |
||
| 458 | */ |
||
| 459 | 75 | protected function replaceSelfVersionDependencies( |
|
| 500 | |||
| 501 | /** |
||
| 502 | * Get a full featured Package from a RootPackageInterface. |
||
| 503 | * |
||
| 504 | * In Composer versions before 599ad77 the RootPackageInterface only |
||
| 505 | * defines a sub-set of operations needed by composer-merge-plugin and |
||
| 506 | * RootAliasPackage only implemented those methods defined by the |
||
| 507 | * interface. Most of the unimplemented methods in RootAliasPackage can be |
||
| 508 | * worked around because the getter methods that are implemented proxy to |
||
| 509 | * the aliased package which we can modify by unwrapping. The exception |
||
| 510 | * being modifying the 'conflicts', 'provides' and 'replaces' collections. |
||
| 511 | * We have no way to actually modify those collections unfortunately in |
||
| 512 | * older versions of Composer. |
||
| 513 | * |
||
| 514 | * @param RootPackageInterface $root |
||
| 515 | * @param string $method Method needed |
||
| 516 | * @return RootPackageInterface|RootPackage |
||
| 517 | */ |
||
| 518 | 95 | public static function unwrapIfNeeded( |
|
| 532 | |||
| 533 | /** |
||
| 534 | * Update the root packages reference information. |
||
| 535 | * |
||
| 536 | * @param RootPackageInterface $root |
||
| 537 | */ |
||
| 538 | 95 | protected function mergeReferences(RootPackageInterface $root) |
|
| 555 | |||
| 556 | /** |
||
| 557 | * Extract vcs revision from version constraint (dev-master#abc123. |
||
| 558 | * |
||
| 559 | * @param array $requires |
||
| 560 | * @param array $references |
||
| 561 | * @return array |
||
| 562 | * @see RootPackageLoader::extractReferences() |
||
| 563 | */ |
||
| 564 | 95 | protected function extractReferences(array $requires, array $references) |
|
| 580 | } |
||
| 581 | // vim:sw=4:ts=4:sts=4:et: |
||
| 582 |
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: