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 |
||
12 | class FlexibleContentBuilder extends FieldBuilder |
||
13 | { |
||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | private $layouts = []; |
||
18 | |||
19 | /** |
||
20 | * @param string $name Field name |
||
21 | * @param string $type Field name |
||
22 | * @param array $config Field configuration |
||
23 | */ |
||
24 | public function __construct($name, $type = 'flexible_content', $config = []) |
||
32 | |||
33 | /** |
||
34 | * Return a configuration array |
||
35 | * @return array |
||
36 | */ |
||
37 | public function build() |
||
43 | |||
44 | /** |
||
45 | * Return a configuration array for each layout |
||
46 | * @return array |
||
47 | */ |
||
48 | private function buildLayouts() |
||
55 | |||
56 | /** |
||
57 | * Apply transformations to a layout |
||
58 | * @param array $layout Layout configuration array |
||
59 | * @return array Transformed layout configuration array |
||
60 | */ |
||
61 | private function transformLayout($layout) |
||
71 | |||
72 | /** |
||
73 | * Add a layout, which is a FieldsBuilder. `addLayout` can be chained to add |
||
74 | * multiple layouts to the Flexible Content field. |
||
75 | * @param string|FieldsBuilder $layout layout name. |
||
76 | * Alternatively supply a FieldsBuilder to reuse existing fields. The name |
||
77 | * will be inferred from the FieldsBuilder's name. |
||
78 | * @param array $args filed configuration |
||
79 | * @return FieldsBuilder |
||
80 | */ |
||
81 | View Code Duplication | public function addLayout($layout, $args = []) |
|
94 | |||
95 | /** |
||
96 | * Add multiple layouts either via an array or from another builder |
||
97 | * @param FieldsBuilder|array $layouts |
||
98 | * @return $this |
||
99 | */ |
||
100 | View Code Duplication | public function addLayouts($layouts) |
|
113 | |||
114 | /** |
||
115 | * Configures the layout FieldsBuilder |
||
116 | * @param FieldsBuilder $layout |
||
117 | * @param array $args FieldGroup Configuration |
||
118 | * @return FieldsBuilder Configured Layout |
||
119 | */ |
||
120 | protected function initializeLayout(FieldsBuilder $layout, $args = []) |
||
133 | |||
134 | /** |
||
135 | * End the current Flexible Content field, return to parent context |
||
136 | * @return Builder |
||
137 | */ |
||
138 | public function endFlexibleContent() |
||
142 | |||
143 | /** |
||
144 | * Add layout to internal array |
||
145 | * @param FieldsBuilder $layout |
||
146 | * @return void |
||
147 | */ |
||
148 | protected function pushLayout($layout) |
||
152 | |||
153 | /** |
||
154 | * @return FieldsBuilder[] |
||
155 | */ |
||
156 | public function getLayouts() |
||
160 | |||
161 | /** |
||
162 | * Gerenates the default button label. |
||
163 | * @return string |
||
164 | */ |
||
165 | private function getDefaultButtonLabel() |
||
169 | } |
||
170 |
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.