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 |
||
14 | class RadioRender extends AbstractFieldRender implements FieldRendererInterface |
||
15 | { |
||
16 | /** @var DOMDocumentFragment $div */ |
||
17 | private $fragment; |
||
18 | |||
19 | private $counter = 0; |
||
20 | |||
21 | /** |
||
22 | * @param FieldInterface $field |
||
23 | * @param DOMElement $element |
||
24 | 10 | * @return DOMNode |
|
25 | */ |
||
26 | View Code Duplication | public function renderBlock(FieldInterface $field, DOMElement $element) |
|
53 | |||
54 | /** |
||
55 | * @param FieldInterface $field |
||
56 | * @param $value |
||
57 | * @param $labelText |
||
58 | * @param $inline |
||
59 | 8 | * @return DOMElement |
|
60 | */ |
||
61 | 8 | private function renderRadio(FieldInterface $field, $value, $labelText, $inline) |
|
73 | 7 | ||
74 | /** |
||
75 | 7 | * @param FieldInterface $field |
|
76 | 7 | * @param string $labelText |
|
77 | 7 | * @return DOMElement |
|
78 | 7 | */ |
|
79 | 7 | View Code Duplication | private function getLabel(FieldInterface $field, string $labelText): DOMElement |
90 | |||
91 | 8 | /** |
|
92 | 8 | * @param FieldInterface $field |
|
93 | 8 | * @param $value |
|
94 | * @param $labelText |
||
95 | 8 | * @return DOMElement |
|
96 | 8 | */ |
|
97 | 8 | private function renderRadioInline(FieldInterface $field, $value, $labelText) |
|
112 | } |
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.