Complex classes like Package 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 Package, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Arcanedev\Composer\Entities; |
||
19 | class Package |
||
20 | { |
||
21 | /* ------------------------------------------------------------------------------------------------ |
||
22 | | Properties |
||
23 | | ------------------------------------------------------------------------------------------------ |
||
24 | */ |
||
25 | /** @var \Composer\Composer $composer */ |
||
26 | protected $composer; |
||
27 | |||
28 | /** @var \Arcanedev\Composer\Utilities\Logger $logger */ |
||
29 | protected $logger; |
||
30 | |||
31 | /** @var string $path */ |
||
32 | protected $path; |
||
33 | |||
34 | /** @var array $json */ |
||
35 | protected $json; |
||
36 | |||
37 | /** @var \Composer\Package\CompletePackage $package */ |
||
38 | protected $package; |
||
39 | |||
40 | /** @var \Composer\Package\Version\VersionParser $versionParser */ |
||
41 | protected $versionParser; |
||
42 | |||
43 | /* ------------------------------------------------------------------------------------------------ |
||
44 | | Constructor |
||
45 | | ------------------------------------------------------------------------------------------------ |
||
46 | */ |
||
47 | /** |
||
48 | * Make a Package instance. |
||
49 | * |
||
50 | * @param string $path |
||
51 | * @param \Composer\Composer $composer |
||
52 | * @param \Arcanedev\Composer\Utilities\Logger $logger |
||
53 | */ |
||
54 | 95 | public function __construct($path, Composer $composer, Logger $logger) |
|
63 | |||
64 | /* ------------------------------------------------------------------------------------------------ |
||
65 | | Getters & Setters |
||
66 | | ------------------------------------------------------------------------------------------------ |
||
67 | */ |
||
68 | /** |
||
69 | * Get list of additional packages to require if precessing recursively. |
||
70 | * |
||
71 | * @return array |
||
72 | */ |
||
73 | 90 | public function getRequires() |
|
79 | |||
80 | /** |
||
81 | * Get list of additional packages to include if precessing recursively. |
||
82 | * |
||
83 | * @return array |
||
84 | */ |
||
85 | 90 | public function getIncludes() |
|
91 | |||
92 | /* ------------------------------------------------------------------------------------------------ |
||
93 | | Main Functions |
||
94 | | ------------------------------------------------------------------------------------------------ |
||
95 | */ |
||
96 | /** |
||
97 | * Merge this package into a RootPackage. |
||
98 | * |
||
99 | * @param \Composer\Package\RootPackageInterface $root |
||
100 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
101 | */ |
||
102 | 95 | public function mergeInto(RootPackageInterface $root, PluginState $state) |
|
119 | |||
120 | /** |
||
121 | * Merge just the dev portion into a RootPackageInterface. |
||
122 | * |
||
123 | * @param \Composer\Package\RootPackageInterface $root |
||
124 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
125 | */ |
||
126 | 95 | public function mergeDevInto(RootPackageInterface $root, PluginState $state) |
|
132 | |||
133 | /** |
||
134 | * Add a collection of repositories described by the given configuration |
||
135 | * to the given package and the global repository manager. |
||
136 | * |
||
137 | * @param \Composer\Package\RootPackageInterface $root |
||
138 | * @param bool $prepend |
||
139 | */ |
||
140 | 95 | private function addRepositories(RootPackageInterface $root, $prepend) |
|
158 | |||
159 | /** |
||
160 | * Add a repository to collection of repositories. |
||
161 | * |
||
162 | * @param \Composer\Repository\RepositoryManager $repoManager |
||
163 | * @param array $repositories |
||
164 | * @param array $repoJson |
||
165 | * @param bool $prepend |
||
166 | */ |
||
167 | 15 | private function addRepository( |
|
187 | |||
188 | /** |
||
189 | * Merge require into a RootPackage. |
||
190 | * |
||
191 | * @param \Composer\Package\RootPackageInterface $root |
||
192 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
193 | */ |
||
194 | 95 | private function mergeRequires(RootPackageInterface $root, PluginState $state) |
|
216 | |||
217 | /** |
||
218 | * Merge require-dev into RootPackage. |
||
219 | * |
||
220 | * @param \Composer\Package\RootPackageInterface $root |
||
221 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
222 | */ |
||
223 | 95 | private function mergeDevRequires(RootPackageInterface $root, PluginState $state) |
|
245 | |||
246 | /** |
||
247 | * Merge two collections of package links and collect duplicates for subsequent processing. |
||
248 | * |
||
249 | * @param \Composer\Package\Link[] $origin Primary collection |
||
250 | * @param array $merge Additional collection |
||
251 | * @param bool $replace Replace existing links ? |
||
252 | * @param array $duplicateLinks Duplicate storage |
||
253 | * |
||
254 | * @return \Composer\Package\Link[] Merged collection |
||
255 | */ |
||
256 | 60 | private function mergeLinks(array $origin, array $merge, $replace, array &$duplicateLinks) |
|
272 | |||
273 | /** |
||
274 | * Merge autoload into a RootPackage. |
||
275 | * |
||
276 | * @param \Composer\Package\RootPackageInterface $root |
||
277 | */ |
||
278 | 95 | private function mergeAutoload(RootPackageInterface $root) |
|
289 | |||
290 | /** |
||
291 | * Merge autoload-dev into a RootPackage. |
||
292 | * |
||
293 | * @param \Composer\Package\RootPackageInterface $root |
||
294 | */ |
||
295 | 95 | private function mergeDevAutoload(RootPackageInterface $root) |
|
306 | |||
307 | /** |
||
308 | * Extract and merge stability flags from the given collection of |
||
309 | * requires and merge them into a RootPackage. |
||
310 | * |
||
311 | * @param \Composer\Package\RootPackageInterface $root |
||
312 | * @param \Composer\Package\Link[] $requires |
||
313 | */ |
||
314 | 60 | private function mergeStabilityFlags(RootPackageInterface $root, array $requires) |
|
325 | |||
326 | /** |
||
327 | * Merge package links of the given type into a RootPackageInterface |
||
328 | * |
||
329 | * @param string $type 'conflict', 'replace' or 'provide' |
||
330 | * @param \Composer\Package\RootPackageInterface $root |
||
331 | */ |
||
332 | 95 | protected function mergePackageLinks($type, RootPackageInterface $root) |
|
358 | |||
359 | /** |
||
360 | * Merge suggested packages into a RootPackage. |
||
361 | * |
||
362 | * @param \Composer\Package\RootPackageInterface $root |
||
363 | */ |
||
364 | 95 | private function mergeSuggests(RootPackageInterface $root) |
|
373 | |||
374 | /** |
||
375 | * Merge extra config into a RootPackage. |
||
376 | * |
||
377 | * @param \Composer\Package\RootPackageInterface $root |
||
378 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
379 | */ |
||
380 | 95 | private function mergeExtra(RootPackageInterface $root, PluginState $state) |
|
395 | |||
396 | /** |
||
397 | * Get extra config. |
||
398 | * |
||
399 | * @param \Composer\Package\RootPackageInterface $root |
||
400 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
401 | * @param array $extra |
||
402 | * |
||
403 | * @return array |
||
404 | */ |
||
405 | 15 | private function getExtra( |
|
423 | |||
424 | /** |
||
425 | * Update the root packages reference information. |
||
426 | * |
||
427 | * @param \Composer\Package\RootPackageInterface $root |
||
428 | */ |
||
429 | 95 | private function mergeReferences(RootPackageInterface $root) |
|
451 | |||
452 | /** |
||
453 | * Extract vcs revision from version constraint (dev-master#abc123). |
||
454 | * @see \Composer\Package\Loader\RootPackageLoader::extractReferences() |
||
455 | * |
||
456 | * @param array $requires |
||
457 | * @param array $references |
||
458 | * |
||
459 | * @return array |
||
460 | */ |
||
461 | 95 | protected function extractReferences(array $requires, array $references) |
|
475 | |||
476 | /** |
||
477 | * Update Links with a 'self.version' constraint with the root package's version. |
||
478 | * |
||
479 | * @param string $type |
||
480 | * @param array $links |
||
481 | * @param \Composer\Package\RootPackageInterface $root |
||
482 | * |
||
483 | * @return array |
||
484 | */ |
||
485 | 70 | protected function replaceSelfVersionDependencies( |
|
515 | |||
516 | /** |
||
517 | * Get a full featured Package from a RootPackageInterface. |
||
518 | * |
||
519 | * @param \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage $root |
||
520 | * @param string $method |
||
521 | * |
||
522 | * @return \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage |
||
523 | */ |
||
524 | 95 | private static function unwrapIfNeeded( |
|
531 | } |
||
532 |
It seems like you are assigning to a variable which was imported through a
use
statement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope