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() |
|
89 | |||
90 | /* ------------------------------------------------------------------------------------------------ |
||
91 | | Main Functions |
||
92 | | ------------------------------------------------------------------------------------------------ |
||
93 | */ |
||
94 | /** |
||
95 | * Merge this package into a RootPackage. |
||
96 | * |
||
97 | * @param \Composer\Package\RootPackageInterface $root |
||
98 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
99 | */ |
||
100 | 85 | public function mergeInto(RootPackageInterface $root, PluginState $state) |
|
101 | { |
||
102 | 85 | $this->addRepositories($root); |
|
103 | |||
104 | 85 | $this->mergeRequires($root, $state); |
|
105 | 85 | $this->mergeAutoload($root); |
|
106 | |||
107 | 85 | if ($state->isDevMode()) { |
|
108 | 85 | $this->mergeDevRequires($root, $state); |
|
109 | 85 | $this->mergeDevAutoload($root); |
|
110 | 68 | } |
|
111 | |||
112 | 85 | $this->mergePackageLinks('conflict', $root); |
|
113 | 85 | $this->mergePackageLinks('replace', $root); |
|
114 | 85 | $this->mergePackageLinks('provide', $root); |
|
115 | |||
116 | 85 | $this->mergeSuggests($root); |
|
117 | 85 | $this->mergeExtra($root, $state); |
|
118 | 85 | } |
|
119 | |||
120 | /** |
||
121 | * Add a collection of repositories described by the given configuration |
||
122 | * to the given package and the global repository manager. |
||
123 | * |
||
124 | * @param \Composer\Package\RootPackageInterface $root |
||
125 | */ |
||
126 | 85 | private function addRepositories(RootPackageInterface $root) |
|
140 | |||
141 | /** |
||
142 | * Add a repository to collection of repositories. |
||
143 | * |
||
144 | * @param \Composer\Repository\RepositoryManager $repoManager |
||
145 | * @param array $repositories |
||
146 | * @param array $repoJson |
||
147 | */ |
||
148 | 10 | private function addRepository( |
|
164 | |||
165 | /** |
||
166 | * Merge require into a RootPackage. |
||
167 | * |
||
168 | * @param \Composer\Package\RootPackageInterface $root |
||
169 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
170 | */ |
||
171 | 85 | private function mergeRequires(RootPackageInterface $root, PluginState $state) |
|
193 | |||
194 | /** |
||
195 | * Merge require-dev into RootPackage. |
||
196 | * |
||
197 | * @param \Composer\Package\RootPackageInterface $root |
||
198 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
199 | */ |
||
200 | 85 | private function mergeDevRequires(RootPackageInterface $root, PluginState $state) |
|
222 | |||
223 | /** |
||
224 | * Update Links with a 'self.version' constraint with the root package's version. |
||
225 | * |
||
226 | * @param string $type |
||
227 | * @param array $links |
||
228 | * @param \Composer\Package\RootPackageInterface $root |
||
229 | * |
||
230 | * @return array |
||
231 | */ |
||
232 | 65 | protected function replaceSelfVersionDependencies( |
|
262 | |||
263 | /** |
||
264 | * Merge two collections of package links and collect duplicates for subsequent processing. |
||
265 | * |
||
266 | * @param array $origin Primary collection |
||
267 | * @param array $merge Additional collection |
||
268 | * @param bool $replace Replace existing links ? |
||
269 | * @param array $duplicateLinks Duplicate storage |
||
270 | * |
||
271 | * @return array Merged collection |
||
272 | */ |
||
273 | 55 | private function mergeLinks(array $origin, array $merge, $replace, array &$duplicateLinks) |
|
289 | |||
290 | /** |
||
291 | * Merge autoload into a RootPackage. |
||
292 | * |
||
293 | * @param \Composer\Package\RootPackageInterface $root |
||
294 | */ |
||
295 | 85 | private function mergeAutoload(RootPackageInterface $root) |
|
306 | |||
307 | /** |
||
308 | * Merge autoload-dev into a RootPackage. |
||
309 | * |
||
310 | * @param \Composer\Package\RootPackageInterface $root |
||
311 | */ |
||
312 | 85 | private function mergeDevAutoload(RootPackageInterface $root) |
|
323 | |||
324 | /** |
||
325 | * Extract and merge stability flags from the given collection of |
||
326 | * requires and merge them into a RootPackage. |
||
327 | * |
||
328 | * @param \Composer\Package\RootPackageInterface $root |
||
329 | * @param array $requires |
||
330 | */ |
||
331 | 55 | private function mergeStabilityFlags(RootPackageInterface $root, array $requires) |
|
342 | |||
343 | /** |
||
344 | * Merge package links of the given type into a RootPackageInterface |
||
345 | * |
||
346 | * @param string $type 'conflict', 'replace' or 'provide' |
||
347 | * @param \Composer\Package\RootPackageInterface $root |
||
348 | */ |
||
349 | 85 | protected function mergePackageLinks($type, RootPackageInterface $root) |
|
375 | |||
376 | /** |
||
377 | * Merge suggested packages into a RootPackage. |
||
378 | * |
||
379 | * @param \Composer\Package\RootPackageInterface $root |
||
380 | */ |
||
381 | 85 | private function mergeSuggests(RootPackageInterface $root) |
|
390 | |||
391 | /** |
||
392 | * Merge extra config into a RootPackage. |
||
393 | * |
||
394 | * @param \Composer\Package\RootPackageInterface $root |
||
395 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
396 | */ |
||
397 | 85 | private function mergeExtra(RootPackageInterface $root, PluginState $state) |
|
412 | |||
413 | /** |
||
414 | * Get extra config. |
||
415 | * |
||
416 | * @param \Composer\Package\RootPackageInterface $root |
||
417 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
418 | * @param array $extra |
||
419 | * |
||
420 | * @return array |
||
421 | */ |
||
422 | 15 | private function getExtra( |
|
440 | |||
441 | /** |
||
442 | * Get a full featured Package from a RootPackageInterface. |
||
443 | * |
||
444 | * @param \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage $root |
||
445 | * @param string $method |
||
446 | * |
||
447 | * @return \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage |
||
448 | */ |
||
449 | 85 | private static function unwrapIfNeeded( |
|
456 | } |
||
457 |
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