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 EnumerationField implements WebField { |
||
11 | |||
12 | /** @var FieldRegistry */ |
||
13 | private $fields; |
||
14 | |||
15 | /** |
||
16 | * EnumerationField constructor. |
||
17 | * @param FieldRegistry $fields |
||
18 | */ |
||
19 | public function __construct(FieldRegistry $fields) { |
||
22 | |||
23 | /** |
||
24 | * @param Parameter $parameter |
||
25 | * @return bool |
||
26 | */ |
||
27 | public function handles(Parameter $parameter) { |
||
30 | |||
31 | /** |
||
32 | * @param Parameter $parameter |
||
33 | * @param string $serialized |
||
34 | * @return mixed |
||
35 | */ |
||
36 | public function inflate(Parameter $parameter, $serialized) { |
||
40 | |||
41 | /** |
||
42 | * @param Parameter $parameter |
||
43 | * @param mixed $value |
||
44 | * @return string |
||
45 | */ |
||
46 | public function render(Parameter $parameter, $value) { |
||
52 | |||
53 | View Code Duplication | private function renderOptions(Parameter $parameter, $value) { |
|
66 | |||
67 | /** |
||
68 | * @param Parameter $parameter |
||
69 | * @return array|\rtens\domin\delivery\web\Element[] |
||
70 | */ |
||
71 | public function headElements(Parameter $parameter) { |
||
74 | |||
75 | /** |
||
76 | * @param Parameter $parameter |
||
77 | * @return array With captions indexed by values |
||
78 | */ |
||
79 | protected function getOptions(Parameter $parameter) { |
||
82 | |||
83 | /** |
||
84 | * @param Parameter $parameter |
||
85 | * @return EnumerationType |
||
86 | */ |
||
87 | private function getType(Parameter $parameter) { |
||
90 | |||
91 | /** |
||
92 | * @param Parameter $parameter |
||
93 | * @return \watoki\reflect\Type |
||
94 | */ |
||
95 | protected function getOptionType(Parameter $parameter) { |
||
98 | } |
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.