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 |
||
33 | class MultiGroupWidget extends AdminWidget implements |
||
34 | FormInterface, |
||
35 | LayoutAwareInterface, |
||
36 | ObjectContainerInterface |
||
37 | { |
||
38 | use FormTrait; |
||
39 | use LayoutAwareTrait; |
||
40 | use ObjectContainerTrait; |
||
41 | |||
42 | /** |
||
43 | * The cache of snake-cased words. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected static $snakeCache = []; |
||
48 | |||
49 | /** |
||
50 | * The cache of camel-cased words. |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | protected static $camelCache = []; |
||
55 | |||
56 | /** |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $controllerIdent; |
||
60 | |||
61 | /** |
||
62 | * @var FactoryInterface |
||
63 | */ |
||
64 | protected $widgetFactory; |
||
65 | |||
66 | /** |
||
67 | * @var boolean |
||
68 | */ |
||
69 | protected $isMergingData; |
||
70 | |||
71 | /** |
||
72 | * @var array|mixed |
||
73 | */ |
||
74 | protected $formGroups; |
||
75 | |||
76 | /** |
||
77 | * @var array|mixed |
||
78 | */ |
||
79 | protected $widgetMetadata; |
||
80 | |||
81 | /** |
||
82 | * @var array|mixed |
||
83 | */ |
||
84 | protected $extraFormGroups; |
||
85 | |||
86 | /** |
||
87 | * The form group's storage target. |
||
88 | * |
||
89 | * @var ModelStructureProperty|null |
||
90 | */ |
||
91 | protected $storageProperty; |
||
92 | |||
93 | /** |
||
94 | * Comparison function used by {@see uasort()}. |
||
95 | * |
||
96 | * @param PrioritizableInterface $a Sortable entity A. |
||
97 | * @param PrioritizableInterface $b Sortable entity B. |
||
98 | * @return integer Sorting value: -1 or 1. |
||
99 | */ |
||
100 | View Code Duplication | protected function sortItemsByPriority( |
|
113 | |||
114 | /** |
||
115 | * @param array $data The widget data. |
||
116 | * @return self |
||
117 | */ |
||
118 | public function setData(array $data) |
||
126 | |||
127 | /** |
||
128 | * @param Container $container The DI container. |
||
129 | * @return void |
||
130 | */ |
||
131 | protected function setDependencies(Container $container) |
||
140 | |||
141 | /** |
||
142 | * @return string |
||
143 | */ |
||
144 | public function defaultGroupType() |
||
148 | |||
149 | /** |
||
150 | * Retrieve the default data sources (when setting data on an entity). |
||
151 | * |
||
152 | * @return string[] |
||
153 | */ |
||
154 | protected function defaultDataSources() |
||
158 | |||
159 | /** |
||
160 | * Set an widget factory. |
||
161 | * |
||
162 | * @param FactoryInterface $factory The factory to create widgets. |
||
163 | * @return self |
||
164 | */ |
||
165 | protected function setWidgetFactory(FactoryInterface $factory) |
||
171 | |||
172 | /** |
||
173 | * Retrieve the widget factory. |
||
174 | * |
||
175 | * @throws RuntimeException If the widget factory was not previously set. |
||
176 | * @return FactoryInterface |
||
177 | */ |
||
178 | public function widgetFactory() |
||
189 | |||
190 | /** |
||
191 | * @return mixed |
||
192 | */ |
||
193 | public function formGroups() |
||
197 | |||
198 | /** |
||
199 | * @param mixed $formGroups FormGroups for MultiGroupWidget. |
||
200 | * @return self |
||
201 | */ |
||
202 | public function setFormGroups($formGroups) |
||
208 | |||
209 | /** |
||
210 | * @return array|mixed |
||
211 | */ |
||
212 | public function widgetMetadata() |
||
216 | |||
217 | /** |
||
218 | * @param array|mixed $widgetMetadata WidgetMetadata for MultiGroupWidget. |
||
219 | * @return self |
||
220 | */ |
||
221 | public function setWidgetMetadata($widgetMetadata) |
||
237 | |||
238 | /** |
||
239 | * Set the form group's storage target. |
||
240 | * |
||
241 | * Must be a property of the form's object model that will receive an associative array |
||
242 | * of the form group's data. |
||
243 | * |
||
244 | * @param string|ModelStructureProperty $propertyIdent The property identifier—or instance—of a storage property. |
||
245 | * @throws \InvalidArgumentException If the property identifier is not a string. |
||
246 | * @throws \UnexpectedValueException If a property is invalid. |
||
247 | * @return self |
||
248 | */ |
||
249 | public function setStorageProperty($propertyIdent) |
||
287 | |||
288 | /** |
||
289 | * Retrieve the form group's storage property master. |
||
290 | * |
||
291 | * @throws RuntimeException If the storage property was not previously set. |
||
292 | * @return ModelStructureProperty |
||
293 | */ |
||
294 | public function storageProperty() |
||
305 | } |
||
306 |
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.