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 |
||
12 | class NumberField implements WebField { |
||
13 | |||
14 | /** |
||
15 | * @param Parameter $parameter |
||
16 | * @return bool |
||
17 | */ |
||
18 | View Code Duplication | public function handles(Parameter $parameter) { |
|
26 | |||
27 | /** |
||
28 | * @param Parameter $parameter |
||
29 | * @param string $serialized |
||
30 | * @return int|float|double |
||
31 | */ |
||
32 | View Code Duplication | public function inflate(Parameter $parameter, $serialized) { |
|
45 | |||
46 | /** |
||
47 | * @param Parameter $parameter |
||
48 | * @param int|float|double|null $value |
||
49 | * @return string |
||
50 | */ |
||
51 | public function render(Parameter $parameter, $value) { |
||
59 | |||
60 | /** |
||
61 | * @param Parameter $parameter |
||
62 | * @return string |
||
63 | */ |
||
64 | private function getType(Parameter $parameter) { |
||
72 | |||
73 | /** |
||
74 | * @param Parameter $parameter |
||
75 | * @return array|Element[] |
||
76 | */ |
||
77 | public function headElements(Parameter $parameter) { |
||
80 | } |
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.