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 |
||
10 | class CheckComposerUpdatesTask extends BuildTask |
||
|
|||
11 | { |
||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $title = 'Composer update checker'; |
||
16 | |||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $description = 'Checks if any composer dependencies can be updated.'; |
||
21 | |||
22 | /** |
||
23 | * Deserialized JSON from composer.lock |
||
24 | * |
||
25 | * @var object |
||
26 | */ |
||
27 | protected $composerLock; |
||
28 | |||
29 | /** |
||
30 | * Minimum required stability defined in the site composer.json |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $minimumStability; |
||
35 | |||
36 | /** |
||
37 | * Whether or not to prefer stable packages |
||
38 | * |
||
39 | * @var bool |
||
40 | */ |
||
41 | protected $preferStable; |
||
42 | |||
43 | /** |
||
44 | * Known stability values |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $stabilityOptions = array( |
||
49 | 'dev', |
||
50 | 'alpha', |
||
51 | 'beta', |
||
52 | 'rc', |
||
53 | 'stable' |
||
54 | ); |
||
55 | |||
56 | /** |
||
57 | * Whether to write all log messages or not |
||
58 | * |
||
59 | * @var bool |
||
60 | */ |
||
61 | protected $extendedLogging = true; |
||
62 | |||
63 | /** |
||
64 | * Retrieve an array of primary composer dependencies from composer.json |
||
65 | * |
||
66 | * @return array |
||
67 | */ |
||
68 | protected function getPackages() |
||
105 | |||
106 | /** |
||
107 | * Return an array of all Composer dependencies from composer.lock |
||
108 | * |
||
109 | * @return array(package => hash) |
||
110 | */ |
||
111 | protected function getDependencies() |
||
135 | |||
136 | /** |
||
137 | * Check if an available version is better than the current version, |
||
138 | * considering stability requirements |
||
139 | * |
||
140 | * Returns FALSE if no update is available. |
||
141 | * Returns the best available version if an update is available. |
||
142 | * |
||
143 | * @param string $currentVersion |
||
144 | * @param string $availableVersions |
||
145 | * @return bool|string |
||
146 | */ |
||
147 | protected function hasUpdate($currentVersion, $availableVersions) |
||
228 | |||
229 | /** |
||
230 | * Check the latest hash on the dev-master branch, and return it if different to the local hash |
||
231 | * |
||
232 | * FALSE is returned if the hash is the same. |
||
233 | * |
||
234 | * @param $availableVersions |
||
235 | * @return bool|string |
||
236 | */ |
||
237 | protected function hasUpdateOnDevMaster($availableVersions) |
||
257 | |||
258 | /** |
||
259 | * Return details from composer.lock for a specific package |
||
260 | * |
||
261 | * @param string $packageName |
||
262 | * @return object |
||
263 | * @throws Exception if package cannot be found in composer.lock |
||
264 | */ |
||
265 | protected function getLocalPackage($packageName) |
||
275 | |||
276 | /** |
||
277 | * Retrieve the pure numerical version |
||
278 | * |
||
279 | * @param string $version |
||
280 | * @return string|null |
||
281 | */ |
||
282 | protected function getPureVersion($version) |
||
294 | |||
295 | /** |
||
296 | * Determine the stability of a given version |
||
297 | * |
||
298 | * @param string $version |
||
299 | * @return string |
||
300 | */ |
||
301 | protected function getStability($version) |
||
313 | |||
314 | /** |
||
315 | * Return a numerical representation of a stability |
||
316 | * |
||
317 | * Higher is more stable |
||
318 | * |
||
319 | * @param string $stability |
||
320 | * @return int |
||
321 | * @throws Exception If the stability is unknown |
||
322 | */ |
||
323 | protected function getStabilityIndex($stability) |
||
335 | |||
336 | /** |
||
337 | * Check if a stability meets a given minimum requirement |
||
338 | * |
||
339 | * @param $currentStability |
||
340 | * @param $possibleStability |
||
341 | * @return bool |
||
342 | */ |
||
343 | protected function isStableEnough($currentStability, $possibleStability) |
||
350 | |||
351 | /** |
||
352 | * Record package details in the database |
||
353 | * |
||
354 | * @param string $package Name of the Composer Package |
||
355 | * @param string $installed Currently installed version |
||
356 | * @param string|boolean $latest The latest available version |
||
357 | */ |
||
358 | protected function recordUpdate($package, $installed, $latest) |
||
382 | |||
383 | /** |
||
384 | * runs the actual steps to verify if there are updates available |
||
385 | * |
||
386 | * @param SS_HTTPRequest $request |
||
387 | */ |
||
388 | public function run($request) |
||
425 | |||
426 | /** |
||
427 | * prints a message during the run of the task |
||
428 | * |
||
429 | * @param string $text |
||
430 | */ |
||
431 | protected function message($text) |
||
439 | } |
||
440 |
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.