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\\Maintenance\\Util\\ComposerLoader', |
||
29 | ]; |
||
30 | |||
31 | /** |
||
32 | * Minimum required stability defined in the site composer.json |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $minimumStability; |
||
37 | |||
38 | /** |
||
39 | * Whether or not to prefer stable packages |
||
40 | * |
||
41 | * @var bool |
||
42 | */ |
||
43 | protected $preferStable; |
||
44 | |||
45 | /** |
||
46 | * Known stability values |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $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 | protected 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 | protected 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 | protected 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 | protected 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 | protected function getLocalPackage($packageName) |
||
257 | |||
258 | /** |
||
259 | * Retrieve the pure numerical version |
||
260 | * |
||
261 | * @param string $version |
||
262 | * @return string|null |
||
263 | */ |
||
264 | protected function getPureVersion($version) |
||
276 | |||
277 | /** |
||
278 | * Determine the stability of a given version |
||
279 | * |
||
280 | * @param string $version |
||
281 | * @return string |
||
282 | */ |
||
283 | protected 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 | protected 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 | protected 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 | protected function recordUpdate($package, $installed, $latest) |
||
363 | |||
364 | /** |
||
365 | * runs the actual steps to verify if there are updates available |
||
366 | * |
||
367 | * @param SS_HTTPRequest $request |
||
368 | */ |
||
369 | public function run($request) |
||
403 | |||
404 | /** |
||
405 | * prints a message during the run of the task |
||
406 | * |
||
407 | * @param string $text |
||
408 | */ |
||
409 | protected function message($text) |
||
417 | |||
418 | /** |
||
419 | * @param string $minimumStability |
||
420 | * @return $this |
||
421 | */ |
||
422 | public function setMinimumStability($minimumStability) |
||
427 | |||
428 | /** |
||
429 | * @return string |
||
430 | */ |
||
431 | public function getMinimumStability() |
||
435 | |||
436 | /** |
||
437 | * @param bool $preferStable |
||
438 | * @return $this |
||
439 | */ |
||
440 | public function setPreferStable($preferStable) |
||
445 | |||
446 | /** |
||
447 | * @return bool |
||
448 | */ |
||
449 | public function getPreferStable() |
||
453 | |||
454 | /** |
||
455 | * @param array $stabilityOptions |
||
456 | * @return $this |
||
457 | */ |
||
458 | public function setStabilityOptions($stabilityOptions) |
||
463 | |||
464 | /** |
||
465 | * @return array |
||
466 | */ |
||
467 | public function getStabilityOptions() |
||
471 | |||
472 | /** |
||
473 | * @return Client |
||
474 | */ |
||
475 | public function getPackagistClient() |
||
479 | |||
480 | /** |
||
481 | * @param Client $packagistClient |
||
482 | */ |
||
483 | public function setPackagistClient(Client $packagistClient) |
||
488 | |||
489 | /** |
||
490 | * @param ComposerLoader $composerLoader |
||
491 | * @return $this |
||
492 | */ |
||
493 | public function setComposerLoader(ComposerLoader $composerLoader) |
||
498 | |||
499 | /** |
||
500 | * @return ComposerLoader |
||
501 | */ |
||
502 | public function getComposerLoader() |
||
506 | } |
||
507 |
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.