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 | const SHA256 = 'sha256'; |
||
| 23 | |||
| 24 | const SHA1 = 'sha1'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private $requiredKeys = array(self::SHA256, 'version', 'url'); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | private $hashAlgo = self::SHA256; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private $manifestUrl; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | private $manifest; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | private $availableVersions; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | private $localVersion; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var bool |
||
| 58 | */ |
||
| 59 | private $allowMajor = false; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var bool |
||
| 63 | */ |
||
| 64 | private $allowUnstable = false; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var int |
||
| 68 | */ |
||
| 69 | private $manifestTimeout = 60; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var int |
||
| 73 | */ |
||
| 74 | private $downloadTimeout = 60; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var bool |
||
| 78 | */ |
||
| 79 | private $ignorePhpReq = false; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Set version string of the local phar |
||
| 83 | * |
||
| 84 | * @param string $version |
||
| 85 | */ |
||
| 86 | public function setCurrentLocalVersion($version) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param int $downloadTimeout |
||
| 94 | * @return self |
||
| 95 | */ |
||
| 96 | public function setDownloadTimeout($downloadTimeout) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param int $manifestTimeout |
||
| 104 | * @return self |
||
| 105 | */ |
||
| 106 | public function setManifestTimeout($manifestTimeout) |
||
| 111 | |||
| 112 | public function useSha1() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * If set, ignores any restrictions based on currently running PHP version. |
||
| 120 | * @return self |
||
| 121 | */ |
||
| 122 | public function ignorePhpRequirements() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * If set, ignores any restrictions based on currently running PHP version. |
||
| 130 | * @return self |
||
| 131 | */ |
||
| 132 | public function allowMajorVersionUpdates() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * If set, ignores any restrictions based on currently running PHP version. |
||
| 140 | * @return self |
||
| 141 | */ |
||
| 142 | public function allowUnstableVersionUpdates() |
||
| 147 | |||
| 148 | public function setManifestUrl($url) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * {@inheritdoc} |
||
| 156 | */ |
||
| 157 | public function getCurrentLocalVersion(Updater $updater) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * {@inheritdoc} |
||
| 164 | */ |
||
| 165 | public function download(Updater $updater) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * {@inheritdoc} |
||
| 208 | */ |
||
| 209 | public function getCurrentRemoteVersion(Updater $updater) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Find update/upgrade notes for the new remote version. |
||
| 235 | * |
||
| 236 | * @param Updater $updater |
||
| 237 | * @param bool $useBaseNote Return main note if no version specific update notes found. |
||
| 238 | * |
||
| 239 | * @return string|false A string if notes are found, or false otherwise. |
||
| 240 | */ |
||
| 241 | public function getUpdateNotes(Updater $updater, $useBaseNote = false) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Gets available versions to update to. |
||
| 272 | * |
||
| 273 | * @return array An array keyed by the version name, whose elements are arrays |
||
| 274 | * containing version information ('name', $this->hashAlgo, and 'url'). |
||
| 275 | */ |
||
| 276 | private function getAvailableVersions() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Download and decode the JSON manifest file. |
||
| 300 | * |
||
| 301 | * @return array |
||
| 302 | */ |
||
| 303 | private function retrieveManifest() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Get version information for the latest remote version. |
||
| 330 | * |
||
| 331 | * @param Updater $updater |
||
| 332 | * |
||
| 333 | * @return array |
||
| 334 | */ |
||
| 335 | private function getRemoteVersionInfo(Updater $updater) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Filter a list of versions to those that match the current local version. |
||
| 350 | * |
||
| 351 | * @param string[] $versions |
||
| 352 | * |
||
| 353 | * @return string[] |
||
| 354 | */ |
||
| 355 | private function filterByLocalMajorVersion(array $versions) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Filter a list of versions to those that allow the current PHP version. |
||
| 367 | * |
||
| 368 | * @param string[] $versions |
||
| 369 | * |
||
| 370 | * @return string[] |
||
| 371 | */ |
||
| 372 | private function filterByPhpVersion(array $versions) |
||
| 386 | } |
||
| 387 |
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..