Complex classes like CheckComposerUpdatesTask 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 CheckComposerUpdatesTask, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class CheckComposerUpdatesTask extends BuildTask |
||
|
|||
15 | { |
||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $title = 'Composer update checker'; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $description = 'Checks if any composer dependencies can be updated.'; |
||
25 | |||
26 | private static $dependencies = [ |
||
27 | 'PackagistClient' => '%$Packagist\\Api\\Client', |
||
28 | 'ComposerLoader' => '%$BringYourOwnIdeas\\UpdateChecker\\ComposerLoader', |
||
29 | ]; |
||
30 | |||
31 | /** |
||
32 | * Minimum required stability defined in the site composer.json |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | private $minimumStability; |
||
37 | |||
38 | /** |
||
39 | * Whether or not to prefer stable packages |
||
40 | * |
||
41 | * @var bool |
||
42 | */ |
||
43 | private $preferStable; |
||
44 | |||
45 | /** |
||
46 | * Known stability values |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | private $stabilityOptions = array( |
||
51 | 'dev', |
||
52 | 'alpha', |
||
53 | 'beta', |
||
54 | 'rc', |
||
55 | 'stable' |
||
56 | ); |
||
57 | |||
58 | /** |
||
59 | * @var Client |
||
60 | */ |
||
61 | protected $packagistClient; |
||
62 | |||
63 | /** |
||
64 | * @var ComposerLoader |
||
65 | */ |
||
66 | protected $composerLoader; |
||
67 | |||
68 | /** |
||
69 | * Retrieve an array of primary composer dependencies from composer.json |
||
70 | * |
||
71 | * @return array |
||
72 | */ |
||
73 | private function getPackages() |
||
101 | |||
102 | /** |
||
103 | * Return an array of all Composer dependencies from composer.lock |
||
104 | * |
||
105 | * Example: `['silverstripe/cms' => '3.4.1', ...]` |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | private function getDependencies() |
||
117 | |||
118 | /** |
||
119 | * Check if an available version is better than the current version, |
||
120 | * considering stability requirements |
||
121 | * |
||
122 | * Returns FALSE if no update is available. |
||
123 | * Returns the best available version if an update is available. |
||
124 | * |
||
125 | * @param string $currentVersion |
||
126 | * @param string[] $availableVersions |
||
127 | * @return bool|string |
||
128 | */ |
||
129 | private function hasUpdate($currentVersion, $availableVersions) |
||
210 | |||
211 | /** |
||
212 | * Check the latest hash on the dev-master branch, and return it if different to the local hash |
||
213 | * |
||
214 | * FALSE is returned if the hash is the same. |
||
215 | * |
||
216 | * @param $availableVersions |
||
217 | * @return bool|string |
||
218 | */ |
||
219 | private function hasUpdateOnDevMaster($availableVersions) |
||
239 | |||
240 | /** |
||
241 | * Return details from composer.lock for a specific package |
||
242 | * |
||
243 | * @param string $packageName |
||
244 | * @return object |
||
245 | * @throws Exception if package cannot be found in composer.lock |
||
246 | */ |
||
247 | private function getLocalPackage($packageName) |
||
257 | |||
258 | /** |
||
259 | * Retrieve the pure numerical version |
||
260 | * |
||
261 | * @param string $version |
||
262 | * @return string|null |
||
263 | */ |
||
264 | private function getPureVersion($version) |
||
276 | |||
277 | /** |
||
278 | * Determine the stability of a given version |
||
279 | * |
||
280 | * @param string $version |
||
281 | * @return string |
||
282 | */ |
||
283 | private function getStability($version) |
||
295 | |||
296 | /** |
||
297 | * Return a numerical representation of a stability |
||
298 | * |
||
299 | * Higher is more stable |
||
300 | * |
||
301 | * @param string $stability |
||
302 | * @return int |
||
303 | * @throws Exception If the stability is unknown |
||
304 | */ |
||
305 | private function getStabilityIndex($stability) |
||
317 | |||
318 | /** |
||
319 | * Check if a stability meets a given minimum requirement |
||
320 | * |
||
321 | * @param $currentStability |
||
322 | * @param $possibleStability |
||
323 | * @return bool |
||
324 | */ |
||
325 | private function isStableEnough($currentStability, $possibleStability) |
||
332 | |||
333 | /** |
||
334 | * Record package details in the database |
||
335 | * |
||
336 | * @param string $package Name of the Composer Package |
||
337 | * @param string $installed Currently installed version |
||
338 | * @param string|boolean $latest The latest available version |
||
339 | */ |
||
340 | private function recordUpdate($package, $installed, $latest) |
||
364 | |||
365 | /** |
||
366 | * runs the actual steps to verify if there are updates available |
||
367 | * |
||
368 | * @param SS_HTTPRequest $request |
||
369 | */ |
||
370 | public function run($request) |
||
404 | |||
405 | /** |
||
406 | * prints a message during the run of the task |
||
407 | * |
||
408 | * @param string $text |
||
409 | */ |
||
410 | protected function message($text) |
||
418 | |||
419 | /** |
||
420 | * @param string $minimumStability |
||
421 | * @return $this |
||
422 | */ |
||
423 | public function setMinimumStability($minimumStability) |
||
428 | |||
429 | /** |
||
430 | * @return string |
||
431 | */ |
||
432 | public function getMinimumStability() |
||
436 | |||
437 | /** |
||
438 | * @param bool $preferStable |
||
439 | * @return $this |
||
440 | */ |
||
441 | public function setPreferStable($preferStable) |
||
446 | |||
447 | /** |
||
448 | * @return bool |
||
449 | */ |
||
450 | public function getPreferStable() |
||
454 | |||
455 | /** |
||
456 | * @param array $stabilityOptions |
||
457 | * @return $this |
||
458 | */ |
||
459 | public function setStabilityOptions($stabilityOptions) |
||
464 | |||
465 | /** |
||
466 | * @return array |
||
467 | */ |
||
468 | public function getStabilityOptions() |
||
472 | |||
473 | /** |
||
474 | * @return Client |
||
475 | */ |
||
476 | public function getPackagistClient() |
||
480 | |||
481 | /** |
||
482 | * @param Client $packagistClient |
||
483 | */ |
||
484 | public function setPackagistClient(Client $packagistClient) |
||
489 | |||
490 | /** |
||
491 | * @param ComposerLoader $composerLoader |
||
492 | * @return $this |
||
493 | */ |
||
494 | public function setComposerLoader(ComposerLoader $composerLoader) |
||
499 | |||
500 | /** |
||
501 | * @return ComposerLoader |
||
502 | */ |
||
503 | public function getComposerLoader() |
||
507 | } |
||
508 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.