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 SimpleFormRenderer |
||
12 | { |
||
13 | /** |
||
14 | * @param FormInterface|FormElementInterface $element |
||
15 | * @param array $options |
||
16 | * |
||
17 | * @return string |
||
18 | */ |
||
19 | public function render($element, $options = []) |
||
31 | |||
32 | /** |
||
33 | * @param FormInterface $form |
||
34 | * |
||
35 | * @return string |
||
36 | */ |
||
37 | public function openingForm(FormInterface $form) |
||
41 | |||
42 | /** |
||
43 | * @return string |
||
44 | */ |
||
45 | public function closingForm() |
||
49 | |||
50 | /** |
||
51 | * @param FormElementInterface $element |
||
52 | * |
||
53 | * @param array $options |
||
54 | * |
||
55 | * @return string |
||
56 | */ |
||
57 | private function renderElement(FormElementInterface $element, $options) |
||
89 | |||
90 | /** |
||
91 | * Format the attributes options of the element. |
||
92 | * |
||
93 | * @param array $options |
||
94 | * |
||
95 | * @return string |
||
96 | */ |
||
97 | private function formatOptions($options) |
||
114 | |||
115 | /** |
||
116 | * @param Select $element |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | private function renderSelectElement(Select $element) |
||
137 | |||
138 | |||
139 | } |
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.