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 |
||
| 16 | class DirectoryExplorer |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * CLI tool. |
||
| 20 | * |
||
| 21 | * @var CLImate CLImate instance. |
||
| 22 | */ |
||
| 23 | protected $cli; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Analyser. |
||
| 27 | * |
||
| 28 | * @var SpiderLinuxCommand analyser instance. |
||
| 29 | */ |
||
| 30 | protected $analyser; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Command line arguments. |
||
| 34 | * |
||
| 35 | * @var array list of arguments. |
||
| 36 | */ |
||
| 37 | protected $arguments; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Analysis targets paths. |
||
| 41 | * |
||
| 42 | * @var array list of files and directories paths. |
||
| 43 | */ |
||
| 44 | protected $analysedPaths; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Composer binaries directory path. |
||
| 48 | * |
||
| 49 | * @var string directory path. |
||
| 50 | */ |
||
| 51 | protected $binariesPath; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Set dependencies and initialize CLI. |
||
| 55 | * |
||
| 56 | * @param CLImate $climate CLImate instance. |
||
| 57 | * @param string $binariesPath Composer binaries path. |
||
| 58 | * @param array $arguments command line arguments. |
||
| 59 | */ |
||
| 60 | View Code Duplication | public function __construct(CLImate $climate, $binariesPath, array $arguments) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Run Code-Analyser command. |
||
| 74 | * |
||
| 75 | * @return boolean true if it didn't find code issues or ran successfully. |
||
| 76 | */ |
||
| 77 | View Code Duplication | public function run() |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Create a DiffOutputFilter based on a git-diff param. |
||
| 103 | * |
||
| 104 | * @param string $gitDiff git diff arguments. |
||
| 105 | * @return DiffOutputFilter filter instance. |
||
| 106 | */ |
||
| 107 | View Code Duplication | protected function getGitDiffFilter($gitDiff) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Initialize output. |
||
| 126 | * |
||
| 127 | * @throws UnexpectedValueException on invalid format value. |
||
| 128 | * @return AbstractOutput |
||
| 129 | */ |
||
| 130 | View Code Duplication | protected function getOutput() |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Command line arguments list for CLImate. |
||
| 151 | * |
||
| 152 | * @return array CLI list of arguments. |
||
| 153 | */ |
||
| 154 | View Code Duplication | protected function getArguments() |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Get a list of paths to be ignored by the analysis. |
||
| 199 | * |
||
| 200 | * @return string[] a list of file and/or directory paths. |
||
| 201 | */ |
||
| 202 | public function getIgnoredPaths() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Parse a string of comma separated files and/or directories to be analysed. |
||
| 211 | * |
||
| 212 | * @param string $pathsString the path argument value. |
||
| 213 | * @return void |
||
| 214 | */ |
||
| 215 | protected function setAnalysedPathsFromString($pathsString) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Set target files and/or directories to be analysed. Fix relative paths. |
||
| 224 | * |
||
| 225 | * @param string[] $paths target paths. |
||
| 226 | * @return void |
||
| 227 | */ |
||
| 228 | View Code Duplication | protected function setAnalysedPaths(array $paths) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Analysis target paths. |
||
| 241 | * |
||
| 242 | * @return string[] a list of analysed paths (usually just one). |
||
| 243 | */ |
||
| 244 | public function getAnalysedPaths() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Running script path. |
||
| 251 | * |
||
| 252 | * @return string current script directory. |
||
| 253 | */ |
||
| 254 | public function getWorkingDirectory() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Output format. |
||
| 261 | * |
||
| 262 | * @return string format type. |
||
| 263 | */ |
||
| 264 | public function getOutputFormat() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * CLI output description. |
||
| 271 | * |
||
| 272 | * @return string description. |
||
| 273 | */ |
||
| 274 | public function getDescription() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * SpiderLinuxCommand instance. |
||
| 281 | * |
||
| 282 | * @return SpiderLinuxCommand instance. |
||
| 283 | */ |
||
| 284 | public function getAnalyser() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * List of output format classes. |
||
| 310 | * |
||
| 311 | * @return array array where the key is a format and its value the class. |
||
| 312 | */ |
||
| 313 | View Code Duplication | protected function getOutputFormatClasses() |
|
| 314 | { |
||
| 315 | return [ |
||
| 316 | 'text' => 'Finder\Logic\Output\TextOutput', |
||
| 317 | 'json' => 'Finder\Logic\Output\JsonOutput', |
||
| 318 | 'xml' => 'Finder\Logic\Output\XmlOutput', |
||
| 319 | 'csv' => 'Finder\Logic\Output\CsvOutput', |
||
| 320 | 'html' => 'Finder\Logic\Output\HtmlOutput', |
||
| 321 | ]; |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Get argument value from user informed arguments. |
||
| 326 | * |
||
| 327 | * @param string $name argument name. |
||
| 328 | * @return Mixed argument value. |
||
| 329 | */ |
||
| 330 | protected function getArgumentValue($name) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Check if the user supplied an argument. |
||
| 337 | * |
||
| 338 | * @param string $name argument name. |
||
| 339 | * @return boolean if the argument has informed or not. |
||
| 340 | */ |
||
| 341 | protected function hasArgumentValue($name) |
||
| 345 | } |
||
| 346 |
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.