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:
| 1 | <?php |
||
| 17 | class CoreVersionUtils |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | private $composerCmd; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | private $rootDir; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var \Symfony\Component\Yaml\Dumper |
||
| 31 | */ |
||
| 32 | private $yamlDumper; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param string $composerCmd ComposerCommand |
||
| 36 | * @param string $rootDir Path to root dir |
||
| 37 | * @param \Symfony\Component\Yaml\Dumper $yamlDumper Yaml dumper |
||
| 38 | */ |
||
| 39 | public function __construct($composerCmd, $rootDir, $yamlDumper) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * gets all versions |
||
| 48 | * |
||
| 49 | * @return array version numbers of packages |
||
|
|
|||
| 50 | */ |
||
| 51 | public function getPackageVersions() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * returns the version of graviton or wrapper |
||
| 67 | * |
||
| 68 | * @return array |
||
| 69 | */ |
||
| 70 | private function getContextVersion() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * returns version for every installed package |
||
| 89 | * |
||
| 90 | * @param array $versions versions array |
||
| 91 | * @return array |
||
| 92 | */ |
||
| 93 | private function getInstalledPackagesVersion($versions) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * runs a composer command depending on the context |
||
| 112 | * |
||
| 113 | * @param string $command composer args |
||
| 114 | * @return string |
||
| 115 | * |
||
| 116 | * @throws \RuntimeException |
||
| 117 | * @throws \LogicException |
||
| 118 | */ |
||
| 119 | View Code Duplication | private function runComposerInContext($command) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * runs a git command depending on the context |
||
| 133 | * |
||
| 134 | * @param string $command git args |
||
| 135 | * @return string |
||
| 136 | * |
||
| 137 | * @throws \RuntimeException |
||
| 138 | * @throws \LogicException |
||
| 139 | */ |
||
| 140 | View Code Duplication | private function runGitInContext($command) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * checks if the package version is configured |
||
| 154 | * |
||
| 155 | * @param string $packageName package name |
||
| 156 | * @return boolean |
||
| 157 | * |
||
| 158 | * @throws \RuntimeException |
||
| 159 | */ |
||
| 160 | private function isDesiredVersion($packageName) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * read and parses version config file |
||
| 181 | * |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | public function getVersionConfig() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * checks if context is a wrapper or not |
||
| 195 | * |
||
| 196 | * @return boolean |
||
| 197 | */ |
||
| 198 | private function isWrapperContext() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * reads configuration information from the given file into an array. |
||
| 209 | * |
||
| 210 | * @param string $filePath Absolute path to the configuration file. |
||
| 211 | * |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | private function getConfiguration($filePath) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Returns the version out of a given version string |
||
| 224 | * |
||
| 225 | * @param string $versionString SemVer version string |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | public function getVersionNumber($versionString) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Get a version string string using a regular expression |
||
| 241 | * |
||
| 242 | * @param string $versionString SemVer version string |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | private function getVersionOrBranchName($versionString) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Normalizing the incorrect SemVer string to a valid one |
||
| 265 | * |
||
| 266 | * At the moment, we are getting the version of the root package ('self') using the |
||
| 267 | * 'composer show -s'-command. Unfortunately Composer is adding an unnecessary ending. |
||
| 268 | * |
||
| 269 | * @param string $versionString SemVer version string |
||
| 270 | * @param string $prefix Version prefix |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | private function normalizeVersionString($versionString, $prefix = 'v') |
||
| 284 | } |
||
| 285 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.