Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ManifestStrategy 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 ManifestStrategy, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | final class ManifestStrategy implements StrategyInterface |
||
20 | { |
||
21 | const SHA256 = 'sha256'; |
||
22 | |||
23 | const SHA1 = 'sha1'; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | private $requiredKeys = array(self::SHA256, 'version', 'url'); |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $hashAlgo = self::SHA256; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $manifestUrl; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | private $manifest; |
||
44 | |||
45 | /** |
||
46 | * @var array |
||
47 | */ |
||
48 | private $availableVersions; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | private $localVersion; |
||
54 | |||
55 | /** |
||
56 | * @var bool |
||
57 | */ |
||
58 | private $allowMajor = false; |
||
59 | |||
60 | /** |
||
61 | * @var bool |
||
62 | */ |
||
63 | private $allowUnstable = false; |
||
64 | |||
65 | /** |
||
66 | * @var int |
||
67 | */ |
||
68 | private $manifestTimeout = 60; |
||
69 | |||
70 | /** |
||
71 | * @var int |
||
72 | */ |
||
73 | private $downloadTimeout = 60; |
||
74 | |||
75 | /** |
||
76 | * @var bool |
||
77 | */ |
||
78 | private $ignorePhpReq = false; |
||
79 | |||
80 | /** |
||
81 | * Set version string of the local phar |
||
82 | * |
||
83 | * @param string $version |
||
84 | */ |
||
85 | public function setCurrentLocalVersion($version) |
||
90 | |||
91 | /** |
||
92 | * @param int $downloadTimeout |
||
93 | * @return self |
||
94 | */ |
||
95 | public function setDownloadTimeout($downloadTimeout) |
||
100 | |||
101 | /** |
||
102 | * @param int $manifestTimeout |
||
103 | * @return self |
||
104 | */ |
||
105 | public function setManifestTimeout($manifestTimeout) |
||
110 | |||
111 | public function useSha1() |
||
116 | |||
117 | /** |
||
118 | * If set, ignores any restrictions based on currently running PHP version. |
||
119 | * @return self |
||
120 | */ |
||
121 | public function ignorePhpRequirements() |
||
126 | |||
127 | /** |
||
128 | * If set, ignores any restrictions based on currently running PHP version. |
||
129 | * @return self |
||
130 | */ |
||
131 | public function allowMajorVersionUpdates() |
||
136 | |||
137 | /** |
||
138 | * If set, ignores any restrictions based on currently running PHP version. |
||
139 | * @return self |
||
140 | */ |
||
141 | public function allowUnstableVersionUpdates() |
||
146 | |||
147 | public function setManifestUrl($url) |
||
152 | |||
153 | /** |
||
154 | * {@inheritdoc} |
||
155 | */ |
||
156 | public function getCurrentLocalVersion(Updater $updater) |
||
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | public function download(Updater $updater) |
||
204 | |||
205 | /** |
||
206 | * {@inheritdoc} |
||
207 | */ |
||
208 | public function getCurrentRemoteVersion(Updater $updater) |
||
231 | |||
232 | /** |
||
233 | * Find update/upgrade notes for the new remote version. |
||
234 | * |
||
235 | * @param Updater $updater |
||
236 | * @param bool $useBaseNote Return main note if no version specific update notes found. |
||
237 | * |
||
238 | * @return string|false A string if notes are found, or false otherwise. |
||
239 | */ |
||
240 | public function getUpdateNotes(Updater $updater, $useBaseNote = false) |
||
268 | |||
269 | /** |
||
270 | * Gets available versions to update to. |
||
271 | * |
||
272 | * @return array An array keyed by the version name, whose elements are arrays |
||
273 | * containing version information ('name', $this->hashAlgo, and 'url'). |
||
274 | */ |
||
275 | private function getAvailableVersions() |
||
296 | |||
297 | /** |
||
298 | * Download and decode the JSON manifest file. |
||
299 | * |
||
300 | * @return array |
||
301 | */ |
||
302 | private function retrieveManifest() |
||
326 | |||
327 | /** |
||
328 | * Get version information for the latest remote version. |
||
329 | * |
||
330 | * @param Updater $updater |
||
331 | * |
||
332 | * @return array |
||
333 | */ |
||
334 | private function getRemoteVersionInfo(Updater $updater) |
||
346 | |||
347 | /** |
||
348 | * Filter a list of versions to those that match the current local version. |
||
349 | * |
||
350 | * @param string[] $versions |
||
351 | * |
||
352 | * @return string[] |
||
353 | */ |
||
354 | private function filterByLocalMajorVersion(array $versions) |
||
363 | |||
364 | /** |
||
365 | * Filter a list of versions to those that allow the current PHP version. |
||
366 | * |
||
367 | * @param string[] $versions |
||
368 | * |
||
369 | * @return string[] |
||
370 | */ |
||
371 | private function filterByPhpVersion(array $versions) |
||
385 | } |
||
386 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..