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 DependencyAnalyzer 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 DependencyAnalyzer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class DependencyAnalyzer { |
||
| 33 | |||
| 34 | /** @var Platform */ |
||
| 35 | private $platform; |
||
| 36 | /** @var \OCP\IL10N */ |
||
| 37 | private $l; |
||
| 38 | /** @var array */ |
||
| 39 | private $appInfo; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param Platform $platform |
||
| 43 | * @param \OCP\IL10N $l |
||
| 44 | */ |
||
| 45 | function __construct(Platform $platform, IL10N $l) { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param array $app |
||
| 52 | * @returns array of missing dependencies |
||
| 53 | */ |
||
| 54 | public function analyze(array $app) { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Truncates both versions to the lowest common version, e.g. |
||
| 74 | * 5.1.2.3 and 5.1 will be turned into 5.1 and 5.1, |
||
| 75 | * 5.2.6.5 and 5.1 will be turned into 5.2 and 5.1 |
||
| 76 | * @param string $first |
||
| 77 | * @param string $second |
||
| 78 | * @return string[] first element is the first version, second element is the |
||
| 79 | * second version |
||
| 80 | */ |
||
| 81 | private function normalizeVersions($first, $second) { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Parameters will be normalized and then passed into version_compare |
||
| 95 | * in the same order they are specified in the method header |
||
| 96 | * @param string $first |
||
| 97 | * @param string $second |
||
| 98 | * @param string $operator |
||
| 99 | * @return bool result similar to version_compare |
||
| 100 | */ |
||
| 101 | private function compare($first, $second, $operator) { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Checks if a version is bigger than another version |
||
| 114 | * @param string $first |
||
| 115 | * @param string $second |
||
| 116 | * @return bool true if the first version is bigger than the second |
||
| 117 | */ |
||
| 118 | private function compareBigger($first, $second) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Checks if a version is smaller than another version |
||
| 124 | * @param string $first |
||
| 125 | * @param string $second |
||
| 126 | * @return bool true if the first version is smaller than the second |
||
| 127 | */ |
||
| 128 | private function compareSmaller($first, $second) { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param array $dependencies |
||
| 134 | * @return array |
||
| 135 | */ |
||
| 136 | private function analyzePhpVersion(array $dependencies) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @param array $dependencies |
||
| 161 | * @return array |
||
| 162 | */ |
||
| 163 | View Code Duplication | private function analyzeDatabases(array $dependencies) { |
|
| 185 | |||
| 186 | /** |
||
| 187 | * @param array $dependencies |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | private function analyzeCommands(array $dependencies) { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param array $dependencies |
||
| 215 | * @return array |
||
| 216 | */ |
||
| 217 | private function analyzeLibraries(array $dependencies) { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param array $dependencies |
||
| 257 | * @return array |
||
| 258 | */ |
||
| 259 | View Code Duplication | private function analyzeOS(array $dependencies) { |
|
| 282 | |||
| 283 | /** |
||
| 284 | * @param array $dependencies |
||
| 285 | * @param array $appInfo |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | private function analyzeOC(array $dependencies, array $appInfo) { |
||
| 289 | $missing = []; |
||
| 290 | $minVersion = null; |
||
| 291 | if (isset($dependencies['owncloud']['@attributes']['min-version'])) { |
||
| 292 | $minVersion = $dependencies['owncloud']['@attributes']['min-version']; |
||
| 293 | } elseif (isset($appInfo['requiremin'])) { |
||
| 294 | $minVersion = $appInfo['requiremin']; |
||
| 295 | } elseif (isset($appInfo['require'])) { |
||
| 296 | $minVersion = $appInfo['require']; |
||
| 297 | } |
||
| 298 | $maxVersion = null; |
||
| 299 | if (isset($dependencies['owncloud']['@attributes']['max-version'])) { |
||
| 300 | $maxVersion = $dependencies['owncloud']['@attributes']['max-version']; |
||
| 301 | } elseif (isset($appInfo['requiremax'])) { |
||
| 302 | $maxVersion = $appInfo['requiremax']; |
||
| 303 | } |
||
| 304 | |||
| 305 | View Code Duplication | if (!is_null($minVersion)) { |
|
| 306 | if ($this->compareSmaller($this->platform->getOcVersion(), $minVersion)) { |
||
| 307 | $missing[] = (string)$this->l->t('Server version %s or higher is required.', $this->toVisibleVersion($minVersion)); |
||
| 308 | } |
||
| 309 | } |
||
| 310 | View Code Duplication | if (!is_null($maxVersion)) { |
|
| 311 | if ($this->compareBigger($this->platform->getOcVersion(), $maxVersion)) { |
||
| 312 | $missing[] = (string)$this->l->t('Server version %s or lower is required.', $this->toVisibleVersion($maxVersion)); |
||
| 313 | } |
||
| 314 | } |
||
| 315 | return $missing; |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Map the internal version number to the Nextcloud version |
||
| 320 | * |
||
| 321 | * @param string $version |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | protected function toVisibleVersion($version) { |
||
| 325 | switch ($version) { |
||
| 326 | case '9.1': |
||
| 327 | return '10'; |
||
| 328 | case '9.2': |
||
| 329 | return '11'; |
||
| 330 | default: |
||
| 331 | if (strpos($version, '9.1.') === 0) { |
||
| 332 | $version = '10.0.' . substr($version, 4); |
||
| 333 | } else if (strpos($version, '9.2.') === 0) { |
||
| 334 | $version = '11.0.' . substr($version, 4); |
||
| 335 | } |
||
| 336 | return $version; |
||
| 337 | } |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @param $element |
||
| 342 | * @return mixed |
||
| 343 | */ |
||
| 344 | private function getValue($element) { |
||
| 349 | } |
||
| 350 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.