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 | 170 | 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 | 165 | public function getIncludes() |
|
91 | |||
92 | /** |
||
93 | * Get list of additional packages to require if precessing recursively. |
||
94 | * |
||
95 | * @return array |
||
96 | */ |
||
97 | 165 | 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 | 170 | protected function readPackageJson($path) |
|
127 | |||
128 | /** |
||
129 | * @param array $json |
||
130 | * @return CompletePackage |
||
131 | */ |
||
132 | 170 | protected function loadPackage(array $json) |
|
146 | |||
147 | /** |
||
148 | * Merge this package into a RootPackageInterface |
||
149 | * |
||
150 | * @param RootPackageInterface $root |
||
151 | * @param PluginState $state |
||
152 | */ |
||
153 | 170 | public function mergeInto(RootPackageInterface $root, PluginState $state) |
|
177 | |||
178 | /** |
||
179 | * Merge just the dev portion into a RootPackageInterface |
||
180 | * |
||
181 | * @param RootPackageInterface $root |
||
182 | * @param PluginState $state |
||
183 | */ |
||
184 | 165 | public function mergeDevInto(RootPackageInterface $root, PluginState $state) |
|
190 | |||
191 | /** |
||
192 | * Add a collection of repositories described by the given configuration |
||
193 | * to the given package and the global repository manager. |
||
194 | * |
||
195 | * @param RootPackageInterface $root |
||
196 | */ |
||
197 | 170 | protected function prependRepositories(RootPackageInterface $root) |
|
198 | { |
||
199 | 170 | if (!isset($this->json['repositories'])) { |
|
200 | 150 | return; |
|
201 | } |
||
202 | 20 | $repoManager = $this->composer->getRepositoryManager(); |
|
203 | 20 | $newRepos = array(); |
|
204 | |||
205 | 20 | foreach ($this->json['repositories'] as $repoJson) { |
|
206 | 20 | if (!isset($repoJson['type'])) { |
|
207 | 15 | continue; |
|
208 | } |
||
209 | 20 | $this->logger->info("Prepending {$repoJson['type']} repository"); |
|
210 | 20 | $repo = $repoManager->createRepository( |
|
211 | 20 | $repoJson['type'], |
|
212 | 8 | $repoJson |
|
213 | 4 | ); |
|
214 | 20 | $repoManager->prependRepository($repo); |
|
215 | 20 | $newRepos[] = $repo; |
|
216 | 4 | } |
|
217 | |||
218 | 20 | $unwrapped = self::unwrapIfNeeded($root, 'setRepositories'); |
|
219 | 20 | $unwrapped->setRepositories(array_merge( |
|
220 | 20 | $newRepos, |
|
221 | 20 | $root->getRepositories() |
|
222 | 4 | )); |
|
223 | 20 | } |
|
224 | |||
225 | /** |
||
226 | * Merge require or require-dev into a RootPackageInterface |
||
227 | * |
||
228 | * @param string $type 'require' or 'require-dev' |
||
229 | * @param RootPackageInterface $root |
||
230 | * @param PluginState $state |
||
231 | */ |
||
232 | 170 | protected function mergeRequires( |
|
233 | $type, |
||
234 | RootPackageInterface $root, |
||
235 | PluginState $state |
||
236 | ) { |
||
237 | 170 | $linkType = BasePackage::$supportedLinkTypes[$type]; |
|
238 | 170 | $getter = 'get' . ucfirst($linkType['method']); |
|
239 | 170 | $setter = 'set' . ucfirst($linkType['method']); |
|
240 | |||
241 | 170 | $requires = $this->package->{$getter}(); |
|
242 | 170 | if (empty($requires)) { |
|
243 | 135 | return; |
|
244 | } |
||
245 | |||
246 | 95 | $this->mergeStabilityFlags($root, $requires); |
|
247 | |||
248 | 95 | $requires = $this->replaceSelfVersionDependencies( |
|
249 | 95 | $type, |
|
250 | 57 | $requires, |
|
251 | 38 | $root |
|
252 | 19 | ); |
|
253 | |||
254 | 95 | $root->{$setter}($this->mergeOrDefer( |
|
255 | 95 | $type, |
|
256 | 95 | $root->{$getter}(), |
|
257 | 57 | $requires, |
|
258 | 38 | $state |
|
259 | 19 | )); |
|
260 | 95 | } |
|
261 | |||
262 | /** |
||
263 | * Merge two collections of package links and collect duplicates for |
||
264 | * subsequent processing. |
||
265 | * |
||
266 | * @param string $type 'require' or 'require-dev' |
||
267 | * @param array $origin Primary collection |
||
268 | * @param array $merge Additional collection |
||
269 | * @param PluginState $state |
||
270 | * @return array Merged collection |
||
271 | */ |
||
272 | 95 | protected function mergeOrDefer( |
|
302 | |||
303 | /** |
||
304 | * Merge autoload or autoload-dev into a RootPackageInterface |
||
305 | * |
||
306 | * @param string $type 'autoload' or 'devAutoload' |
||
307 | * @param RootPackageInterface $root |
||
308 | */ |
||
309 | 170 | protected function mergeAutoload($type, RootPackageInterface $root) |
|
325 | |||
326 | /** |
||
327 | * Fix a collection of paths that are relative to this package to be |
||
328 | * relative to the base package. |
||
329 | * |
||
330 | * @param array $paths |
||
331 | * @return array |
||
332 | */ |
||
333 | 15 | protected function fixRelativePaths(array $paths) |
|
334 | { |
||
335 | 15 | $base = dirname($this->path); |
|
336 | 15 | $base = ($base === '.') ? '' : "{$base}/"; |
|
337 | |||
338 | 15 | array_walk_recursive( |
|
339 | 15 | $paths, |
|
340 | 12 | function (&$path) use ($base) { |
|
341 | 15 | $path = "{$base}{$path}"; |
|
342 | 15 | } |
|
343 | 3 | ); |
|
344 | 15 | return $paths; |
|
345 | } |
||
346 | |||
347 | /** |
||
348 | * Extract and merge stability flags from the given collection of |
||
349 | * requires and merge them into a RootPackageInterface |
||
350 | * |
||
351 | * @param RootPackageInterface $root |
||
352 | * @param array $requires |
||
353 | */ |
||
354 | 95 | protected function mergeStabilityFlags( |
|
367 | |||
368 | /** |
||
369 | * Merge package links of the given type into a RootPackageInterface |
||
370 | * |
||
371 | * @param string $type 'conflict', 'replace' or 'provide' |
||
372 | * @param RootPackageInterface $root |
||
373 | */ |
||
374 | 170 | protected function mergePackageLinks($type, RootPackageInterface $root) |
|
397 | |||
398 | /** |
||
399 | * Merge suggested packages into a RootPackageInterface |
||
400 | * |
||
401 | * @param RootPackageInterface $root |
||
402 | */ |
||
403 | 170 | protected function mergeSuggests(RootPackageInterface $root) |
|
404 | { |
||
405 | 170 | $suggests = $this->package->getSuggests(); |
|
406 | 170 | if (!empty($suggests)) { |
|
407 | 15 | $unwrapped = self::unwrapIfNeeded($root, 'setSuggests'); |
|
408 | 15 | $unwrapped->setSuggests(array_merge( |
|
409 | 15 | $root->getSuggests(), |
|
410 | 6 | $suggests |
|
411 | 3 | )); |
|
412 | 3 | } |
|
413 | 170 | } |
|
414 | |||
415 | /** |
||
416 | * Merge extra config into a RootPackageInterface |
||
417 | * |
||
418 | * @param RootPackageInterface $root |
||
419 | * @param PluginState $state |
||
420 | */ |
||
421 | 170 | public function mergeExtra(RootPackageInterface $root, PluginState $state) |
|
453 | |||
454 | /** |
||
455 | * Merge scripts config into a RootPackageInterface |
||
456 | * |
||
457 | * @param RootPackageInterface $root |
||
458 | * @param PluginState $state |
||
459 | */ |
||
460 | 170 | public function mergeScripts(RootPackageInterface $root, PluginState $state) |
|
480 | |||
481 | /** |
||
482 | * Merges two arrays either via arrayMergeDeep or via array_merge. |
||
483 | * |
||
484 | * @param bool $mergeDeep |
||
485 | * @param array $array1 |
||
486 | * @param array $array2 |
||
487 | * @return array |
||
488 | */ |
||
489 | 30 | public static function mergeExtraArray($mergeDeep, $array1, $array2) |
|
497 | |||
498 | /** |
||
499 | * Update Links with a 'self.version' constraint with the root package's |
||
500 | * version. |
||
501 | * |
||
502 | * @param string $type Link type |
||
503 | * @param array $links |
||
504 | * @param RootPackageInterface $root |
||
505 | * @return array |
||
506 | */ |
||
507 | 110 | protected function replaceSelfVersionDependencies( |
|
508 | $type, |
||
509 | array $links, |
||
510 | RootPackageInterface $root |
||
511 | ) { |
||
512 | 110 | $linkType = BasePackage::$supportedLinkTypes[$type]; |
|
513 | 110 | $version = $root->getVersion(); |
|
514 | 110 | $prettyVersion = $root->getPrettyVersion(); |
|
515 | 110 | $vp = $this->versionParser; |
|
516 | |||
517 | 110 | $method = 'get' . ucfirst($linkType['method']); |
|
518 | 110 | $packages = $root->$method(); |
|
519 | |||
520 | 110 | return array_map( |
|
521 | 110 | function ($link) use ($linkType, $version, $prettyVersion, $vp, $packages) { |
|
522 | 110 | if ('self.version' === $link->getPrettyConstraint()) { |
|
523 | 15 | if (isset($packages[$link->getSource()])) { |
|
524 | /** @var Link $package */ |
||
525 | 10 | $package = $packages[$link->getSource()]; |
|
526 | 10 | return new Link( |
|
527 | 10 | $link->getSource(), |
|
528 | 10 | $link->getTarget(), |
|
529 | 10 | $vp->parseConstraints($package->getConstraint()->getPrettyString()), |
|
530 | 10 | $linkType['description'], |
|
531 | 10 | $package->getPrettyConstraint() |
|
532 | 2 | ); |
|
533 | } |
||
534 | |||
535 | 5 | return new Link( |
|
536 | 5 | $link->getSource(), |
|
537 | 5 | $link->getTarget(), |
|
538 | 5 | $vp->parseConstraints($version), |
|
539 | 5 | $linkType['description'], |
|
540 | 2 | $prettyVersion |
|
541 | 1 | ); |
|
542 | } |
||
543 | 110 | return $link; |
|
544 | 110 | }, |
|
545 | 44 | $links |
|
546 | 22 | ); |
|
547 | } |
||
548 | |||
549 | /** |
||
550 | * Get a full featured Package from a RootPackageInterface. |
||
551 | * |
||
552 | * In Composer versions before 599ad77 the RootPackageInterface only |
||
553 | * defines a sub-set of operations needed by composer-merge-plugin and |
||
554 | * RootAliasPackage only implemented those methods defined by the |
||
555 | * interface. Most of the unimplemented methods in RootAliasPackage can be |
||
556 | * worked around because the getter methods that are implemented proxy to |
||
557 | * the aliased package which we can modify by unwrapping. The exception |
||
558 | * being modifying the 'conflicts', 'provides' and 'replaces' collections. |
||
559 | * We have no way to actually modify those collections unfortunately in |
||
560 | * older versions of Composer. |
||
561 | * |
||
562 | * @param RootPackageInterface $root |
||
563 | * @param string $method Method needed |
||
564 | * @return RootPackageInterface|RootPackage |
||
565 | */ |
||
566 | 170 | public static function unwrapIfNeeded( |
|
580 | |||
581 | /** |
||
582 | * Update the root packages reference information. |
||
583 | * |
||
584 | * @param RootPackageInterface $root |
||
585 | */ |
||
586 | 170 | protected function mergeReferences(RootPackageInterface $root) |
|
603 | |||
604 | /** |
||
605 | * Extract vcs revision from version constraint (dev-master#abc123. |
||
606 | * |
||
607 | * @param array $requires |
||
608 | * @param array $references |
||
609 | * @return array |
||
610 | * @see RootPackageLoader::extractReferences() |
||
611 | */ |
||
612 | 170 | protected function extractReferences(array $requires, array $references) |
|
627 | } |
||
628 | // vim:sw=4:ts=4:sts=4:et: |
||
629 |