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 |
||
| 26 | class GenerateVersionsCommand extends Command |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | private $composerCmd; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $contextDir; |
||
| 37 | |||
| 38 | /* |
||
| 39 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
| 40 | */ |
||
| 41 | private $output; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * {@inheritDoc} |
||
| 45 | * |
||
| 46 | * @return void |
||
| 47 | */ |
||
| 48 | protected function configure() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * {@inheritDoc} |
||
| 60 | * |
||
| 61 | * @param InputInterface $input input |
||
| 62 | * @param OutputInterface $output output |
||
| 63 | * |
||
| 64 | * @return void |
||
| 65 | */ |
||
| 66 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * gets all versions |
||
| 88 | * |
||
| 89 | * @return array version numbers of packages |
||
| 90 | */ |
||
| 91 | public function getPackageVersions() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * returns the version of graviton or wrapper using git |
||
| 108 | * |
||
| 109 | * @return array |
||
| 110 | */ |
||
| 111 | private function getContextVersion() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * returns version for every installed package |
||
| 130 | * |
||
| 131 | * @param array $versions versions array |
||
| 132 | * @return array |
||
| 133 | */ |
||
| 134 | private function getInstalledPackagesVersion($versions) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * runs a composer command depending on the context |
||
| 153 | * |
||
| 154 | * @param string $command composer args |
||
| 155 | * @return string |
||
| 156 | * |
||
| 157 | * @throws \RuntimeException |
||
| 158 | */ |
||
| 159 | View Code Duplication | private function runComposerInContext($command) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * runs a git command depending on the context |
||
| 176 | * |
||
| 177 | * @param string $command git args |
||
| 178 | * @return string |
||
| 179 | * |
||
| 180 | * @throws \RuntimeException |
||
| 181 | */ |
||
| 182 | View Code Duplication | private function runGitInContext($command) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * checks if the package version is configured |
||
| 195 | * |
||
| 196 | * @param string $packageName package name |
||
| 197 | * @return boolean |
||
| 198 | * |
||
| 199 | * @throws \RuntimeException |
||
| 200 | */ |
||
| 201 | private function isDesiredVersion($packageName) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * reads configuration information from the given file into an array. |
||
| 222 | * |
||
| 223 | * @param string $filePath Absolute path to the configuration file. |
||
| 224 | * |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | private function getConfiguration($filePath) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Returns the version out of a given version string |
||
| 237 | * |
||
| 238 | * @param string $versionString SemVer version string |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | public function getVersionNumber($versionString) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Get a version string string using a regular expression |
||
| 254 | * |
||
| 255 | * @param string $versionString SemVer version string |
||
| 256 | * @return string |
||
| 257 | */ |
||
| 258 | private function getVersionOrBranchName($versionString) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Normalizing the incorrect SemVer string to a valid one |
||
| 278 | * |
||
| 279 | * At the moment, we are getting the version of the root package ('self') using the |
||
| 280 | * 'composer show -s'-command. Unfortunately Composer is adding an unnecessary ending. |
||
| 281 | * |
||
| 282 | * @param string $versionString SemVer version string |
||
| 283 | * @param string $prefix Version prefix |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | private function normalizeVersionString($versionString, $prefix = 'v') |
||
| 297 | } |
||
| 298 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: