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 | /* ------------------------------------------------------------------------------------------------ |
||
41 | | Constructor |
||
42 | | ------------------------------------------------------------------------------------------------ |
||
43 | */ |
||
44 | /** |
||
45 | * Make a Package instance. |
||
46 | * |
||
47 | * @param string $path |
||
48 | * @param \Composer\Composer $composer |
||
49 | * @param \Arcanedev\Composer\Utilities\Logger $logger |
||
50 | */ |
||
51 | 85 | public function __construct($path, Composer $composer, Logger $logger) |
|
59 | |||
60 | /* ------------------------------------------------------------------------------------------------ |
||
61 | | Getters & Setters |
||
62 | | ------------------------------------------------------------------------------------------------ |
||
63 | */ |
||
64 | /** |
||
65 | * Get list of additional packages to require if precessing recursively. |
||
66 | * |
||
67 | * @return array |
||
68 | */ |
||
69 | 80 | public function getRequires() |
|
75 | |||
76 | /** |
||
77 | * Get list of additional packages to include if precessing recursively. |
||
78 | * |
||
79 | * @return array |
||
80 | */ |
||
81 | 80 | public function getIncludes() |
|
87 | |||
88 | /* ------------------------------------------------------------------------------------------------ |
||
89 | | Main Functions |
||
90 | | ------------------------------------------------------------------------------------------------ |
||
91 | */ |
||
92 | /** |
||
93 | * Merge this package into a RootPackage. |
||
94 | * |
||
95 | * @param \Composer\Package\RootPackageInterface $root |
||
96 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
97 | */ |
||
98 | 85 | public function mergeInto(RootPackageInterface $root, PluginState $state) |
|
117 | |||
118 | /** |
||
119 | * Add a collection of repositories described by the given configuration |
||
120 | * to the given package and the global repository manager. |
||
121 | * |
||
122 | * @param \Composer\Package\RootPackageInterface $root |
||
123 | */ |
||
124 | 85 | private function addRepositories(RootPackageInterface $root) |
|
138 | |||
139 | /** |
||
140 | * Add a repository to collection of repositories. |
||
141 | * |
||
142 | * @param \Composer\Repository\RepositoryManager $repoManager |
||
143 | * @param array $repositories |
||
144 | * @param array $repoJson |
||
145 | */ |
||
146 | 10 | private function addRepository( |
|
162 | |||
163 | /** |
||
164 | * Merge require into a RootPackage. |
||
165 | * |
||
166 | * @param \Composer\Package\RootPackageInterface $root |
||
167 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
168 | */ |
||
169 | 85 | private function mergeRequires(RootPackageInterface $root, PluginState $state) |
|
191 | |||
192 | /** |
||
193 | * Merge require-dev into RootPackage. |
||
194 | * |
||
195 | * @param \Composer\Package\RootPackageInterface $root |
||
196 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
197 | */ |
||
198 | 85 | private function mergeDevRequires(RootPackageInterface $root, PluginState $state) |
|
220 | |||
221 | /** |
||
222 | * Merge two collections of package links and collect duplicates for subsequent processing. |
||
223 | * |
||
224 | * @param array $origin Primary collection |
||
225 | * @param array $merge Additional collection |
||
226 | * @param bool $replace Replace existing links ? |
||
227 | * @param array $duplicateLinks Duplicate storage |
||
228 | * |
||
229 | * @return array Merged collection |
||
230 | */ |
||
231 | 55 | private function mergeLinks(array $origin, array $merge, $replace, array &$duplicateLinks) |
|
247 | |||
248 | /** |
||
249 | * Merge autoload into a RootPackage. |
||
250 | * |
||
251 | * @param \Composer\Package\RootPackageInterface $root |
||
252 | */ |
||
253 | 85 | private function mergeAutoload(RootPackageInterface $root) |
|
264 | |||
265 | /** |
||
266 | * Merge autoload-dev into a RootPackage. |
||
267 | * |
||
268 | * @param \Composer\Package\RootPackageInterface $root |
||
269 | */ |
||
270 | 85 | private function mergeDevAutoload(RootPackageInterface $root) |
|
281 | |||
282 | /** |
||
283 | * Extract and merge stability flags from the given collection of |
||
284 | * requires and merge them into a RootPackage. |
||
285 | * |
||
286 | * @param \Composer\Package\RootPackageInterface $root |
||
287 | * @param array $requires |
||
288 | */ |
||
289 | 55 | private function mergeStabilityFlags(RootPackageInterface $root, array $requires) |
|
300 | |||
301 | /** |
||
302 | * Merge package links of the given type into a RootPackageInterface |
||
303 | * |
||
304 | * @param string $type 'conflict', 'replace' or 'provide' |
||
305 | * @param \Composer\Package\RootPackageInterface $root |
||
306 | */ |
||
307 | 85 | protected function mergePackageLinks($type, RootPackageInterface $root) |
|
333 | |||
334 | /** |
||
335 | * Merge suggested packages into a RootPackage. |
||
336 | * |
||
337 | * @param \Composer\Package\RootPackageInterface $root |
||
338 | */ |
||
339 | 85 | private function mergeSuggests(RootPackageInterface $root) |
|
348 | |||
349 | /** |
||
350 | * Merge extra config into a RootPackage. |
||
351 | * |
||
352 | * @param \Composer\Package\RootPackageInterface $root |
||
353 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
354 | */ |
||
355 | 85 | private function mergeExtra(RootPackageInterface $root, PluginState $state) |
|
370 | |||
371 | /** |
||
372 | * Get extra config. |
||
373 | * |
||
374 | * @param \Composer\Package\RootPackageInterface $root |
||
375 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
376 | * @param array $extra |
||
377 | * |
||
378 | * @return array |
||
379 | */ |
||
380 | 15 | private function getExtra( |
|
398 | |||
399 | /** |
||
400 | * Update Links with a 'self.version' constraint with the root package's version. |
||
401 | * |
||
402 | * @param string $type |
||
403 | * @param array $links |
||
404 | * @param \Composer\Package\RootPackageInterface $root |
||
405 | * |
||
406 | * @return array |
||
407 | */ |
||
408 | 65 | protected function replaceSelfVersionDependencies( |
|
438 | |||
439 | /** |
||
440 | * Get a full featured Package from a RootPackageInterface. |
||
441 | * |
||
442 | * @param \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage $root |
||
443 | * @param string $method |
||
444 | * |
||
445 | * @return \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage |
||
446 | */ |
||
447 | 85 | private static function unwrapIfNeeded( |
|
454 | } |
||
455 |
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