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 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | private static $requiredKeys = array('sha256', 'version', 'url'); |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $manifestUrl; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | private $manifest; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private $availableVersions; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | private $localVersion; |
||
45 | |||
46 | /** |
||
47 | * @var bool |
||
48 | */ |
||
49 | private $allowMajor = false; |
||
50 | |||
51 | /** |
||
52 | * @var bool |
||
53 | */ |
||
54 | private $allowUnstable = false; |
||
55 | |||
56 | /** |
||
57 | * @var int |
||
58 | */ |
||
59 | private $manifestTimeout = 60; |
||
60 | |||
61 | /** |
||
62 | * @var int |
||
63 | */ |
||
64 | private $downloadTimeout = 60; |
||
65 | |||
66 | /** |
||
67 | * @var bool |
||
68 | */ |
||
69 | private $ignorePhpReq = false; |
||
70 | |||
71 | /** |
||
72 | * Set version string of the local phar |
||
73 | * |
||
74 | * @param string $version |
||
75 | */ |
||
76 | public function setCurrentLocalVersion($version) |
||
81 | |||
82 | /** |
||
83 | * @param int $downloadTimeout |
||
84 | * @return self |
||
85 | */ |
||
86 | public function setDownloadTimeout($downloadTimeout) |
||
91 | |||
92 | /** |
||
93 | * @param int $manifestTimeout |
||
94 | * @return self |
||
95 | */ |
||
96 | public function setManifestTimeout($manifestTimeout) |
||
101 | |||
102 | /** |
||
103 | * If set, ignores any restrictions based on currently running PHP version. |
||
104 | * @return self |
||
105 | */ |
||
106 | public function ignorePhpRequirements() |
||
111 | |||
112 | /** |
||
113 | * If set, ignores any restrictions based on currently running PHP version. |
||
114 | * @return self |
||
115 | */ |
||
116 | public function allowMajorVersionUpdates() |
||
121 | |||
122 | /** |
||
123 | * If set, ignores any restrictions based on currently running PHP version. |
||
124 | * @return self |
||
125 | */ |
||
126 | public function allowUnstableVersionUpdates() |
||
131 | |||
132 | public function setManifestUrl($url) |
||
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | public function getCurrentLocalVersion(Updater $updater) |
||
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | */ |
||
149 | public function download(Updater $updater) |
||
188 | |||
189 | /** |
||
190 | * {@inheritdoc} |
||
191 | */ |
||
192 | public function getCurrentRemoteVersion(Updater $updater) |
||
215 | |||
216 | /** |
||
217 | * Find update/upgrade notes for the new remote version. |
||
218 | * |
||
219 | * @param Updater $updater |
||
220 | * @param bool $useBaseNote Return if no version specific update notes found. |
||
221 | * |
||
222 | * @return string|false A string if notes are found, or false otherwise. |
||
223 | */ |
||
224 | public function getUpdateNotes(Updater $updater, $useBaseNote = false) |
||
252 | |||
253 | /** |
||
254 | * Gets available versions to update to. |
||
255 | * |
||
256 | * @return array An array keyed by the version name, whose elements are arrays |
||
257 | * containing version information ('name', 'sha256', and 'url'). |
||
258 | */ |
||
259 | private function getAvailableVersions() |
||
280 | |||
281 | /** |
||
282 | * Download and decode the JSON manifest file. |
||
283 | * |
||
284 | * @return array |
||
285 | */ |
||
286 | private function retrieveManifest() |
||
310 | |||
311 | /** |
||
312 | * Get version information for the latest remote version. |
||
313 | * |
||
314 | * @param Updater $updater |
||
315 | * |
||
316 | * @return array |
||
317 | */ |
||
318 | private function getRemoteVersionInfo(Updater $updater) |
||
330 | |||
331 | /** |
||
332 | * Filter a list of versions to those that match the current local version. |
||
333 | * |
||
334 | * @param string[] $versions |
||
335 | * |
||
336 | * @return string[] |
||
337 | */ |
||
338 | private function filterByLocalMajorVersion(array $versions) |
||
347 | |||
348 | /** |
||
349 | * Filter a list of versions to those that allow the current PHP version. |
||
350 | * |
||
351 | * @param string[] $versions |
||
352 | * |
||
353 | * @return string[] |
||
354 | */ |
||
355 | private function filterByPhpVersion(array $versions) |
||
369 | } |
||
370 |
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..