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 | * @var VersionParser $versionParser |
||
70 | */ |
||
71 | protected $versionParser; |
||
72 | |||
73 | /** |
||
74 | * @param string $path Path to composer.json file |
||
75 | * @param Composer $composer |
||
76 | * @param Logger $logger |
||
77 | */ |
||
78 | 174 | public function __construct($path, Composer $composer, Logger $logger, IOInterface $io) |
|
88 | |||
89 | /** |
||
90 | * Get list of additional packages to include if precessing recursively. |
||
91 | * |
||
92 | * @return array |
||
93 | */ |
||
94 | 169 | public function getIncludes() |
|
99 | |||
100 | /** |
||
101 | * Get list of additional packages to require if precessing recursively. |
||
102 | * |
||
103 | * @return array |
||
104 | */ |
||
105 | 169 | public function getRequires() |
|
110 | |||
111 | /** |
||
112 | * Read the contents of a composer.json style file into an array. |
||
113 | * |
||
114 | * The package contents are fixed up to be usable to create a Package |
||
115 | * object by providing dummy "name" and "version" values if they have not |
||
116 | * been provided in the file. This is consistent with the default root |
||
117 | * package loading behavior of Composer. |
||
118 | * |
||
119 | * @param string $path |
||
120 | * @return array |
||
121 | */ |
||
122 | 174 | protected function readPackageJson($path) |
|
140 | |||
141 | /** |
||
142 | * @param array $json |
||
143 | * @return CompletePackage |
||
144 | */ |
||
145 | 174 | protected function loadPackage(array $json) |
|
159 | |||
160 | /** |
||
161 | * Merge this package into a RootPackageInterface |
||
162 | * |
||
163 | * @param RootPackageInterface $root |
||
164 | * @param PluginState $state |
||
165 | */ |
||
166 | 174 | public function mergeInto(RootPackageInterface $root, PluginState $state) |
|
190 | |||
191 | /** |
||
192 | * Merge just the dev portion into a RootPackageInterface |
||
193 | * |
||
194 | * @param RootPackageInterface $root |
||
195 | * @param PluginState $state |
||
196 | */ |
||
197 | 169 | public function mergeDevInto(RootPackageInterface $root, PluginState $state) |
|
203 | |||
204 | /** |
||
205 | * Add a collection of repositories described by the given configuration |
||
206 | * to the given package and the global repository manager. |
||
207 | * |
||
208 | * @param RootPackageInterface $root |
||
209 | */ |
||
210 | 174 | protected function prependRepositories(RootPackageInterface $root) |
|
237 | |||
238 | /** |
||
239 | * Merge require or require-dev into a RootPackageInterface |
||
240 | * |
||
241 | * @param string $type 'require' or 'require-dev' |
||
242 | * @param RootPackageInterface $root |
||
243 | * @param PluginState $state |
||
244 | */ |
||
245 | 174 | protected function mergeRequires( |
|
274 | |||
275 | /** |
||
276 | * Merge two collections of package links and collect duplicates for |
||
277 | * subsequent processing. |
||
278 | * |
||
279 | * @param string $type 'require' or 'require-dev' |
||
280 | * @param array $origin Primary collection |
||
281 | * @param array $merge Additional collection |
||
282 | * @param PluginState $state |
||
283 | * @return array Merged collection |
||
284 | */ |
||
285 | 99 | protected function mergeOrDefer( |
|
315 | |||
316 | /** |
||
317 | * Merge autoload or autoload-dev into a RootPackageInterface |
||
318 | * |
||
319 | * @param string $type 'autoload' or 'devAutoload' |
||
320 | * @param RootPackageInterface $root |
||
321 | */ |
||
322 | 174 | protected function mergeAutoload($type, RootPackageInterface $root) |
|
338 | |||
339 | /** |
||
340 | * Fix a collection of paths that are relative to this package to be |
||
341 | * relative to the base package. |
||
342 | * |
||
343 | * @param array $paths |
||
344 | * @return array |
||
345 | */ |
||
346 | 15 | protected function fixRelativePaths(array $paths) |
|
359 | |||
360 | /** |
||
361 | * Extract and merge stability flags from the given collection of |
||
362 | * requires and merge them into a RootPackageInterface |
||
363 | * |
||
364 | * @param RootPackageInterface $root |
||
365 | * @param array $requires |
||
366 | */ |
||
367 | 99 | protected function mergeStabilityFlags( |
|
380 | |||
381 | /** |
||
382 | * Merge package links of the given type into a RootPackageInterface |
||
383 | * |
||
384 | * @param string $type 'conflict', 'replace' or 'provide' |
||
385 | * @param RootPackageInterface $root |
||
386 | */ |
||
387 | 174 | protected function mergePackageLinks($type, RootPackageInterface $root) |
|
410 | |||
411 | /** |
||
412 | * Merge suggested packages into a RootPackageInterface |
||
413 | * |
||
414 | * @param RootPackageInterface $root |
||
415 | */ |
||
416 | 174 | protected function mergeSuggests(RootPackageInterface $root) |
|
427 | |||
428 | /** |
||
429 | * Merge extra config into a RootPackageInterface |
||
430 | * |
||
431 | * @param RootPackageInterface $root |
||
432 | * @param PluginState $state |
||
433 | */ |
||
434 | 174 | public function mergeExtra(RootPackageInterface $root, PluginState $state) |
|
466 | |||
467 | /** |
||
468 | * Merge scripts config into a RootPackageInterface |
||
469 | * |
||
470 | * @param RootPackageInterface $root |
||
471 | * @param PluginState $state |
||
472 | */ |
||
473 | 174 | public function mergeScripts(RootPackageInterface $root, PluginState $state) |
|
493 | |||
494 | /** |
||
495 | * Merges two arrays either via arrayMergeDeep or via array_merge. |
||
496 | * |
||
497 | * @param bool $mergeDeep |
||
498 | * @param array $array1 |
||
499 | * @param array $array2 |
||
500 | * @return array |
||
501 | */ |
||
502 | 30 | public static function mergeExtraArray($mergeDeep, $array1, $array2) |
|
510 | |||
511 | /** |
||
512 | * Update Links with a 'self.version' constraint with the root package's |
||
513 | * version. |
||
514 | * |
||
515 | * @param string $type Link type |
||
516 | * @param array $links |
||
517 | * @param RootPackageInterface $root |
||
518 | * @return array |
||
519 | */ |
||
520 | 114 | protected function replaceSelfVersionDependencies( |
|
561 | |||
562 | /** |
||
563 | * Get a full featured Package from a RootPackageInterface. |
||
564 | * |
||
565 | * In Composer versions before 599ad77 the RootPackageInterface only |
||
566 | * defines a sub-set of operations needed by composer-merge-plugin and |
||
567 | * RootAliasPackage only implemented those methods defined by the |
||
568 | * interface. Most of the unimplemented methods in RootAliasPackage can be |
||
569 | * worked around because the getter methods that are implemented proxy to |
||
570 | * the aliased package which we can modify by unwrapping. The exception |
||
571 | * being modifying the 'conflicts', 'provides' and 'replaces' collections. |
||
572 | * We have no way to actually modify those collections unfortunately in |
||
573 | * older versions of Composer. |
||
574 | * |
||
575 | * @param RootPackageInterface $root |
||
576 | * @param string $method Method needed |
||
577 | * @return RootPackageInterface|RootPackage |
||
578 | */ |
||
579 | 174 | public static function unwrapIfNeeded( |
|
593 | |||
594 | /** |
||
595 | * Update the root packages reference information. |
||
596 | * |
||
597 | * @param RootPackageInterface $root |
||
598 | */ |
||
599 | 174 | protected function mergeReferences(RootPackageInterface $root) |
|
616 | |||
617 | /** |
||
618 | * Extract vcs revision from version constraint (dev-master#abc123. |
||
619 | * |
||
620 | * @param array $requires |
||
621 | * @param array $references |
||
622 | * @return array |
||
623 | * @see RootPackageLoader::extractReferences() |
||
624 | */ |
||
625 | 174 | protected function extractReferences(array $requires, array $references) |
|
641 | } |
||
642 | // vim:sw=4:ts=4:sts=4:et: |
||
643 |