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 CommandExecutor 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 CommandExecutor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class CommandExecutor implements CommandExecutorInterface |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var BuildLogger |
||
| 16 | */ |
||
| 17 | protected $logger; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var bool |
||
| 21 | */ |
||
| 22 | protected $verbose; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $lastOutput; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $lastError; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var bool |
||
| 36 | */ |
||
| 37 | public $logExecOutput = true; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The path which findBinary will look in. |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $rootDir; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Current build path |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $buildPath; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Commands with no proper exit mechanism |
||
| 55 | * |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | private static $noExitCommands = [ |
||
| 59 | 'codecept', |
||
| 60 | ]; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Environment variables that should not be inherited |
||
| 64 | * |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | private static $blacklistEnvVars = [ |
||
| 68 | 'PHP_SELF', |
||
| 69 | 'SCRIPT_NAME', |
||
| 70 | 'SCRIPT_FILENAME', |
||
| 71 | 'PATH_TRANSLATED', |
||
| 72 | 'DOCUMENT_ROOT', |
||
| 73 | 'SHELL_VERBOSITY', |
||
| 74 | ]; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param BuildLogger $logger |
||
| 78 | * @param string $rootDir |
||
| 79 | * @param bool $verbose |
||
| 80 | */ |
||
| 81 | public function __construct(BuildLogger $logger, $rootDir, $verbose = false) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Executes shell commands. |
||
| 91 | * |
||
| 92 | * @param array $args |
||
| 93 | * |
||
| 94 | * @return bool Indicates success |
||
| 95 | */ |
||
| 96 | public function executeCommand($args = []) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param string $utf8String |
||
| 170 | * |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | public function replaceIllegalCharacters($utf8String) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Returns the output from the last command run. |
||
| 185 | * |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | public function getLastOutput() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Returns the stderr output from the last command run. |
||
| 195 | * |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | public function getLastError() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param string $binaryPath |
||
| 205 | * @param string $binary |
||
| 206 | * |
||
| 207 | * @return string|false |
||
| 208 | */ |
||
| 209 | View Code Duplication | protected function findBinaryByPath($binaryPath, $binary) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * @param string $composerBin |
||
| 222 | * @param string $binary |
||
| 223 | * |
||
| 224 | * @return string|false |
||
| 225 | */ |
||
| 226 | View Code Duplication | protected function findBinaryLocal($composerBin, $binary) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * @param string $binary |
||
| 239 | * |
||
| 240 | * @return string|false |
||
| 241 | */ |
||
| 242 | protected function findBinaryGlobal($binary) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Uses 'which' to find a system binary by name |
||
| 255 | * |
||
| 256 | * @param string $binary |
||
| 257 | * |
||
| 258 | * @return string|false |
||
| 259 | */ |
||
| 260 | protected function findBinarySystem($binary) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * {@inheritdoc} |
||
| 274 | */ |
||
| 275 | public function findBinary($binary, $priorityPath = 'local', $binaryPath = '', $binaryName = []) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Try to load the composer.json file in the building project |
||
| 362 | * If the bin-dir is configured, return the full path to it |
||
| 363 | * |
||
| 364 | * @param string $path Current build path |
||
| 365 | * |
||
| 366 | * @return string|null |
||
| 367 | */ |
||
| 368 | public function getComposerBinDir($path) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Set the buildPath property. |
||
| 387 | * |
||
| 388 | * @param string $path |
||
| 389 | */ |
||
| 390 | public function setBuildPath($path) |
||
| 394 | |||
| 395 | |||
| 396 | |||
| 397 | |||
| 398 | private function getDefaultEnv() |
||
| 446 | } |
||
| 447 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.