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 |
||
| 11 | class NullableField implements WebField { |
||
| 12 | |||
| 13 | const NULL_VALUE = '____IS_NULL____'; |
||
| 14 | |||
| 15 | /** @var FieldRegistry */ |
||
| 16 | private $fields; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @param FieldRegistry $fields |
||
| 20 | */ |
||
| 21 | public function __construct(FieldRegistry $fields) { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @param Parameter $parameter |
||
| 27 | * @return bool |
||
| 28 | */ |
||
| 29 | public function handles(Parameter $parameter) { |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param Parameter $parameter |
||
| 35 | * @param mixed $serialized |
||
| 36 | * @return null|mixed |
||
| 37 | */ |
||
| 38 | public function inflate(Parameter $parameter, $serialized) { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param Parameter $parameter |
||
| 49 | * @param mixed $value |
||
| 50 | * @return string |
||
| 51 | */ |
||
| 52 | public function render(Parameter $parameter, $value) { |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param Parameter $parameter |
||
| 80 | * @return array|Element[] |
||
| 81 | */ |
||
| 82 | public function headElements(Parameter $parameter) { |
||
| 91 | |||
| 92 | View Code Duplication | private function getInnerParameter(Parameter $parameter) { |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @param Parameter $innerParameter |
||
| 103 | * @return WebField |
||
| 104 | */ |
||
| 105 | private function getInnerField(Parameter $innerParameter) { |
||
| 108 | } |
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.