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 |
||
19 | class CheckboxRender extends AbstractFieldRender implements FieldRendererInterface |
||
20 | { |
||
21 | /** @var DOMDocumentFragment $div */ |
||
22 | private $fragment; |
||
23 | |||
24 | /** @var bool $isMultiCheckbox */ |
||
25 | private $isMultiCheckbox = false; |
||
26 | |||
27 | private $counter = 0; |
||
28 | |||
29 | /** |
||
30 | * @param FieldInterface $field |
||
31 | * @param DOMElement $element |
||
32 | 9 | * @return DOMNode |
|
33 | */ |
||
34 | View Code Duplication | public function renderBlock(FieldInterface $field, DOMElement $element) |
|
62 | |||
63 | |||
64 | /** |
||
65 | * @param FieldInterface $field |
||
66 | * @param $value |
||
67 | * @param $labelText |
||
68 | 7 | * @return DOMElement |
|
69 | */ |
||
70 | 7 | private function processOption(FieldInterface $field, $value, $labelText, $inline) |
|
74 | |||
75 | /** |
||
76 | * @param CheckBox $field |
||
77 | * @param $value |
||
78 | * @param $labelText |
||
79 | * @return DOMElement |
||
80 | */ |
||
81 | private function renderCheckbox(CheckBox $field, $value, $labelText, $inline) |
||
92 | |||
93 | /** |
||
94 | * @param FieldInterface $field |
||
95 | * @param string $labelText |
||
96 | * @return DOMElement |
||
97 | */ |
||
98 | 7 | View Code Duplication | private function getLabel(FieldInterface $field, string $labelText): DOMElement |
109 | 7 | ||
110 | 7 | /** |
|
111 | * @param FieldInterface $field |
||
112 | 7 | * @param $value |
|
113 | 3 | * @param $labelText |
|
114 | * @return DOMElement |
||
115 | */ |
||
116 | 7 | private function renderCheckboxInline(FieldInterface $field, $value) |
|
132 | } |
||
133 |
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.