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 | abstract class AbstractFormRenderer implements FormRendererInterface |
||
20 | { |
||
21 | /** @var DOMDocument $dom */ |
||
22 | protected $dom; |
||
23 | |||
24 | /** @var DomElement $form */ |
||
25 | protected $form; |
||
26 | |||
27 | /** @var bool $displayErrors */ |
||
28 | protected $displayErrors; |
||
29 | |||
30 | /** @var ErrorRendererInterface $errorRenderer */ |
||
31 | protected $errorRenderer; |
||
32 | |||
33 | /** @var DomElement $label The label element*/ |
||
34 | protected $label; |
||
35 | |||
36 | /** @var DomElement $element the field element */ |
||
37 | protected $element; |
||
38 | |||
39 | /** @var DomElement $errors The error block html*/ |
||
40 | protected $errors; |
||
41 | |||
42 | /** @var DomElement $block The containing html block */ |
||
43 | protected $block; |
||
44 | |||
45 | /** @var FieldInterface $field The current field being processed */ |
||
46 | protected $field; |
||
47 | |||
48 | 19 | public function __construct($name) |
|
56 | |||
57 | /** |
||
58 | * @param FormInterface $form |
||
59 | * @param bool $displayErrors |
||
60 | * @return string |
||
61 | */ |
||
62 | 6 | View Code Duplication | public function render(FormInterface $form, $displayErrors = true) |
73 | |||
74 | /** |
||
75 | * @param FormInterface $form |
||
76 | */ |
||
77 | 6 | View Code Duplication | private function setFormAttributes(FormInterface $form) |
91 | |||
92 | /** |
||
93 | * @param FormInterface $form |
||
94 | * @return string |
||
95 | */ |
||
96 | 6 | private function getMethod(FormInterface $form) |
|
100 | |||
101 | /** |
||
102 | * @param FormInterface $form |
||
103 | * @return string |
||
104 | */ |
||
105 | 6 | private function getId(FormInterface $form) |
|
109 | |||
110 | 6 | private function processFields(FieldCollection $fields) |
|
125 | |||
126 | |||
127 | |||
128 | /** |
||
129 | * @return DOMElement|null |
||
130 | */ |
||
131 | 3 | public function renderError() |
|
140 | } |
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.