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 | const STABLE = 'stable'; |
||
| 26 | |||
| 27 | const UNSTABLE = 'unstable'; |
||
| 28 | |||
| 29 | const ANY = 'any'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | private $requiredKeys = array(self::SHA256, 'version', 'url'); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private $hashAlgo = self::SHA256; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private $manifestUrl; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | private $manifest; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | private $availableVersions; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | private $localVersion; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var bool |
||
| 63 | */ |
||
| 64 | private $allowMajor = false; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var bool |
||
| 68 | */ |
||
| 69 | private $allowUnstable = false; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var bool |
||
| 73 | */ |
||
| 74 | private $requireUnstable = false; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var int |
||
| 78 | */ |
||
| 79 | private $manifestTimeout = 60; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var int |
||
| 83 | */ |
||
| 84 | private $downloadTimeout = 60; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var bool |
||
| 88 | */ |
||
| 89 | private $ignorePhpReq = false; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Set version string of the local phar |
||
| 93 | * |
||
| 94 | * @param string $version |
||
| 95 | */ |
||
| 96 | public function setCurrentLocalVersion($version) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param int $downloadTimeout |
||
| 104 | * @return self |
||
| 105 | */ |
||
| 106 | public function setDownloadTimeout($downloadTimeout) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param int $manifestTimeout |
||
| 114 | * @return self |
||
| 115 | */ |
||
| 116 | public function setManifestTimeout($manifestTimeout) |
||
| 121 | |||
| 122 | public function useSha1() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * If set, ignores any restrictions based on currently running PHP version. |
||
| 130 | * @return self |
||
| 131 | */ |
||
| 132 | public function ignorePhpRequirements() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * If set, ignores any restrictions based on currently running PHP version. |
||
| 140 | * @return self |
||
| 141 | */ |
||
| 142 | public function allowMajorVersionUpdates() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Set target stability |
||
| 150 | * |
||
| 151 | * @param string $stability |
||
| 152 | */ |
||
| 153 | public function setStability($stability) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * If set, ignores any restrictions based on currently running PHP version. |
||
| 177 | * @return self |
||
| 178 | */ |
||
| 179 | public function allowUnstableVersionUpdates() |
||
| 184 | |||
| 185 | public function setManifestUrl($url) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * {@inheritdoc} |
||
| 193 | */ |
||
| 194 | public function getCurrentLocalVersion(Updater $updater) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * {@inheritdoc} |
||
| 201 | */ |
||
| 202 | public function download(Updater $updater) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * {@inheritdoc} |
||
| 245 | */ |
||
| 246 | public function getCurrentRemoteVersion(Updater $updater) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Find update/upgrade notes for the new remote version. |
||
| 274 | * |
||
| 275 | * @param Updater $updater |
||
| 276 | * @param bool $useBaseNote Return main note if no version specific update notes found. |
||
| 277 | * |
||
| 278 | * @return string|false A string if notes are found, or false otherwise. |
||
| 279 | */ |
||
| 280 | public function getUpdateNotes(Updater $updater, $useBaseNote = false) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Gets available versions to update to. |
||
| 311 | * |
||
| 312 | * @return array An array keyed by the version name, whose elements are arrays |
||
| 313 | * containing version information ('name', $this->hashAlgo, and 'url'). |
||
| 314 | */ |
||
| 315 | private function getAvailableVersions() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Download and decode the JSON manifest file. |
||
| 339 | * |
||
| 340 | * @return array |
||
| 341 | */ |
||
| 342 | private function retrieveManifest() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Get version information for the latest remote version. |
||
| 369 | * |
||
| 370 | * @param Updater $updater |
||
| 371 | * |
||
| 372 | * @return array |
||
| 373 | */ |
||
| 374 | private function getRemoteVersionInfo(Updater $updater) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Filter a list of versions to those that match the current local version. |
||
| 389 | * |
||
| 390 | * @param string[] $versions |
||
| 391 | * |
||
| 392 | * @return string[] |
||
| 393 | */ |
||
| 394 | private function filterByLocalMajorVersion(array $versions) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Filter a list of versions to those that allow the current PHP version. |
||
| 406 | * |
||
| 407 | * @param string[] $versions |
||
| 408 | * |
||
| 409 | * @return string[] |
||
| 410 | */ |
||
| 411 | private function filterByPhpVersion(array $versions) |
||
| 425 | } |
||
| 426 |
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..