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 |
||
| 13 | class Config implements ConfigInterface |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Array of parameters (with defaults) |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | private $_parameters = [ |
||
| 20 | 'legacy' => Client::LEGACY, |
||
| 21 | 'ssl' => Client::SSL, |
||
| 22 | 'timeout' => Client::TIMEOUT, |
||
| 23 | 'attempts' => Client::ATTEMPTS, |
||
| 24 | 'delay' => Client::ATTEMPTS_DELAY |
||
| 25 | ]; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Set parameter into array |
||
| 29 | * |
||
| 30 | * @param string $name |
||
| 31 | * @param mixed $value |
||
| 32 | * @return ConfigInterface |
||
| 33 | */ |
||
| 34 | public function set(string $name, $value): ConfigInterface |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Return parameter of current config by name |
||
| 67 | * |
||
| 68 | * @param string $parameter |
||
| 69 | * @return mixed |
||
| 70 | */ |
||
| 71 | public function get(string $parameter) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Return array with all parameters of configuration |
||
| 96 | * |
||
| 97 | * @return array |
||
| 98 | */ |
||
| 99 | public function getParameters(): array |
||
| 103 | } |
||
| 104 |
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.