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 | protected $versionParser; |
||
62 | |||
63 | /** |
||
64 | * @param string $path Path to composer.json file |
||
65 | * @param Composer $composer |
||
66 | 80 | * @param Logger $logger |
|
67 | */ |
||
68 | 80 | public function __construct($path, Composer $composer, Logger $logger) |
|
77 | |||
78 | /** |
||
79 | * Get list of additional packages to include if precessing recursively. |
||
80 | 75 | * |
|
81 | * @return array |
||
82 | 75 | */ |
|
83 | 75 | public function getIncludes() |
|
88 | |||
89 | /** |
||
90 | * Get list of additional packages to require if precessing recursively. |
||
91 | 75 | * |
|
92 | * @return array |
||
93 | 75 | */ |
|
94 | 75 | public function getRequires() |
|
99 | |||
100 | /** |
||
101 | * Read the contents of a composer.json style file into an array. |
||
102 | * |
||
103 | * The package contents are fixed up to be usable to create a Package |
||
104 | * object by providing dummy "name" and "version" values if they have not |
||
105 | * been provided in the file. This is consistent with the default root |
||
106 | * package loading behavior of Composer. |
||
107 | * |
||
108 | 80 | * @param string $path |
|
109 | * @return array |
||
110 | 80 | */ |
|
111 | 80 | protected function readPackageJson($path) |
|
124 | |||
125 | /** |
||
126 | 80 | * @param string $json |
|
127 | * @return CompletePackage |
||
128 | 80 | */ |
|
129 | 80 | protected function loadPackage($json) |
|
143 | |||
144 | /** |
||
145 | * Merge this package into a RootPackageInterface |
||
146 | * |
||
147 | 80 | * @param RootPackageInterface $root |
|
148 | * @param PluginState $state |
||
149 | 80 | */ |
|
150 | public function mergeInto(RootPackageInterface $root, PluginState $state) |
||
186 | 5 | ||
187 | /** |
||
188 | 5 | * Add a collection of repositories described by the given configuration |
|
189 | 5 | * to the given package and the global repository manager. |
|
190 | 5 | * |
|
191 | * @param RootPackageInterface $root |
||
192 | 5 | */ |
|
193 | 5 | protected function addRepositories(RootPackageInterface $root) |
|
220 | 80 | ||
221 | 80 | /** |
|
222 | 70 | * Merge require or require-dev into a RootPackageInterface |
|
223 | * |
||
224 | * @param string $type 'require' or 'require-dev' |
||
225 | 55 | * @param RootPackageInterface $root |
|
226 | * @param PluginState $state |
||
227 | 55 | */ |
|
228 | 55 | protected function mergeRequires( |
|
257 | 55 | ||
258 | 55 | /** |
|
259 | 55 | * Merge two collections of package links and collect duplicates for |
|
260 | 55 | * subsequent processing. |
|
261 | 55 | * |
|
262 | 55 | * @param string $type 'require' or 'require-dev' |
|
263 | * @param array $origin Primary collection |
||
264 | 10 | * @param array $merge Additional collection |
|
265 | 10 | * @param PluginState $state |
|
266 | 10 | * @return array Merged collection |
|
267 | 10 | */ |
|
268 | protected function mergeOrDefer( |
||
290 | 5 | ||
291 | 5 | /** |
|
292 | 5 | * Merge autoload or autoload-dev into a RootPackageInterface |
|
293 | 5 | * |
|
294 | 5 | * @param string $type 'autoload' or 'devAutoload' |
|
295 | 5 | * @param RootPackageInterface $root |
|
296 | */ |
||
297 | protected function mergeAutoload($type, RootPackageInterface $root) |
||
313 | 5 | ||
314 | 5 | /** |
|
315 | 5 | * Fix a collection of paths that are relative to this package to be |
|
316 | * relative to the base package. |
||
317 | * |
||
318 | * @param array $paths |
||
319 | * @return array |
||
320 | */ |
||
321 | protected function fixRelativePaths(array $paths) |
||
334 | 55 | ||
335 | 55 | /** |
|
336 | 55 | * Extract and merge stability flags from the given collection of |
|
337 | 55 | * requires and merge them into a RootPackageInterface |
|
338 | * |
||
339 | * @param RootPackageInterface $root |
||
340 | * @param array $requires |
||
341 | */ |
||
342 | protected function mergeStabilityFlags( |
||
355 | |||
356 | /** |
||
357 | * Merge package links of the given type into a RootPackageInterface |
||
358 | * |
||
359 | * @param string $type 'conflict', 'replace' or 'provide' |
||
360 | 10 | * @param RootPackageInterface $root |
|
361 | 10 | */ |
|
362 | 10 | protected function mergePackageLinks($type, RootPackageInterface $root) |
|
383 | |||
384 | /** |
||
385 | * Merge suggested packages into a RootPackageInterface |
||
386 | * |
||
387 | * @param RootPackageInterface $root |
||
388 | */ |
||
389 | protected function mergeSuggests(RootPackageInterface $root) |
||
400 | |||
401 | 15 | /** |
|
402 | 5 | * Merge extra config into a RootPackageInterface |
|
403 | 5 | * |
|
404 | 5 | * @param RootPackageInterface $root |
|
405 | * @param PluginState $state |
||
406 | 5 | */ |
|
407 | 10 | public function mergeExtra(RootPackageInterface $root, PluginState $state) |
|
438 | 60 | ||
439 | 60 | /** |
|
440 | * Update Links with a 'self.version' constraint with the root package's |
||
441 | 60 | * version. |
|
442 | 60 | * |
|
443 | 60 | * @param string $type Link type |
|
444 | 5 | * @param array $links |
|
445 | 5 | * @param RootPackageInterface $root |
|
446 | 5 | * @return array |
|
447 | 5 | */ |
|
448 | 5 | protected function replaceSelfVersionDependencies( |
|
474 | |||
475 | 80 | /** |
|
476 | * Get a full featured Package from a RootPackageInterface. |
||
477 | * |
||
478 | * In Composer versions before 599ad77 the RootPackageInterface only |
||
479 | 80 | * defines a sub-set of operations needed by composer-merge-plugin and |
|
480 | * RootAliasPackage only implemented those methods defined by the |
||
481 | 80 | * interface. Most of the unimplemented methods in RootAliasPackage can be |
|
482 | * worked around because the getter methods that are implemented proxy to |
||
483 | * the aliased package which we can modify by unwrapping. The exception |
||
484 | * being modifying the 'conflicts', 'provides' and 'replaces' collections. |
||
485 | 80 | * We have no way to actually modify those collections unfortunately in |
|
486 | * older versions of Composer. |
||
487 | * |
||
488 | * @param RootPackageInterface $root |
||
489 | * @param string $method Method needed |
||
490 | * @return RootPackageInterface|RootPackage |
||
491 | */ |
||
492 | public static function unwrapIfNeeded( |
||
504 | |||
505 | /** |
||
506 | * Extract vcs revision from version constraint (dev-master#abc123. |
||
507 | * |
||
508 | * @param array $requires |
||
509 | * @param array $references |
||
510 | * @return array |
||
511 | * @see RootPackageLoader::extractReferences() |
||
512 | */ |
||
513 | protected function extractReferences(array $requires, array $references) |
||
525 | } |
||
526 | // vim:sw=4:ts=4:sts=4:et: |
||
527 |
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: