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 |
||
| 10 | class NullableField implements CliField { |
||
| 11 | |||
| 12 | /** @var FieldRegistry */ |
||
| 13 | private $fields; |
||
| 14 | |||
| 15 | /** @var ParameterReader */ |
||
| 16 | private $reader; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @param FieldRegistry $fields |
||
| 20 | * @param ParameterReader $reader |
||
| 21 | */ |
||
| 22 | public function __construct(FieldRegistry $fields, ParameterReader $reader) { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param Parameter $parameter |
||
| 29 | * @return bool |
||
| 30 | */ |
||
| 31 | public function handles(Parameter $parameter) { |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param Parameter $parameter |
||
| 37 | * @param mixed $serialized |
||
| 38 | * @return mixed |
||
| 39 | */ |
||
| 40 | public function inflate(Parameter $parameter, $serialized) { |
||
| 50 | |||
| 51 | View Code Duplication | private function getInnerParameter(Parameter $parameter) { |
|
| 59 | |||
| 60 | /** |
||
| 61 | * @param Parameter $parameter |
||
| 62 | * @return string |
||
| 63 | */ |
||
| 64 | public function getDescription(Parameter $parameter) { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param $innerParameter |
||
| 70 | * @return CliField |
||
| 71 | * @throws \Exception |
||
| 72 | */ |
||
| 73 | private function getField($innerParameter) { |
||
| 76 | } |
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.