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 |
||
20 | abstract class WidgetBase extends PluginBase implements WidgetInterface, ContainerFactoryPluginInterface { |
||
21 | |||
22 | use PluginConfigurationFormTrait; |
||
23 | |||
24 | /** |
||
25 | * Plugin id. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $id; |
||
30 | |||
31 | /** |
||
32 | * Plugin uuid. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $uuid; |
||
37 | /** |
||
38 | * Plugin label. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $label; |
||
43 | |||
44 | /** |
||
45 | * Plugin weight. |
||
46 | * |
||
47 | * @var int |
||
48 | */ |
||
49 | protected $weight; |
||
50 | |||
51 | /** |
||
52 | * Event dispatcher service. |
||
53 | * |
||
54 | * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface |
||
55 | */ |
||
56 | protected $eventDispatcher; |
||
57 | |||
58 | /** |
||
59 | * Entity type manager service. |
||
60 | * |
||
61 | * @var \Drupal\Core\Entity\EntityTypeManagerInterface |
||
62 | */ |
||
63 | protected $entityTypeManager; |
||
64 | |||
65 | /** |
||
66 | * The Widget Validation Manager service. |
||
67 | * |
||
68 | * @var \Drupal\entity_browser\WidgetValidationManager |
||
69 | */ |
||
70 | protected $validationManager; |
||
71 | |||
72 | /** |
||
73 | * WidgetBase constructor. |
||
74 | * |
||
75 | * @param array $configuration |
||
76 | * A configuration array containing information about the plugin instance. |
||
77 | * @param string $plugin_id |
||
78 | * The plugin_id for the plugin instance. |
||
79 | * @param mixed $plugin_definition |
||
80 | * The plugin implementation definition. |
||
81 | * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher |
||
82 | * Event dispatcher service. |
||
83 | * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager |
||
84 | * The entity type manager service. |
||
85 | * @param \Drupal\entity_browser\WidgetValidationManager $validation_manager |
||
86 | * The Widget Validation Manager service. |
||
87 | */ |
||
88 | View Code Duplication | public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityTypeManagerInterface $entity_type_manager, WidgetValidationManager $validation_manager) { |
|
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | View Code Duplication | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
|
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | public function getForm(array &$original_form, FormStateInterface $form_state, array $additional_widget_parameters) { |
||
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | public function defaultConfiguration() { |
||
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | public function getConfiguration() { |
||
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | public function setConfiguration(array $configuration) { |
||
198 | |||
199 | /** |
||
200 | * {@inheritdoc} |
||
201 | */ |
||
202 | public function calculateDependencies() { |
||
205 | |||
206 | /** |
||
207 | * {@inheritdoc} |
||
208 | */ |
||
209 | public function buildConfigurationForm(array $form, FormStateInterface $form_state) { |
||
227 | |||
228 | /** |
||
229 | * {@inheritdoc} |
||
230 | */ |
||
231 | public function id() { |
||
234 | |||
235 | /** |
||
236 | * {@inheritdoc} |
||
237 | */ |
||
238 | public function uuid() { |
||
241 | |||
242 | /** |
||
243 | * {@inheritdoc} |
||
244 | */ |
||
245 | public function label() { |
||
248 | |||
249 | /** |
||
250 | * {@inheritdoc} |
||
251 | */ |
||
252 | public function setLabel($label) { |
||
256 | |||
257 | /** |
||
258 | * {@inheritdoc} |
||
259 | */ |
||
260 | public function getWeight() { |
||
263 | |||
264 | /** |
||
265 | * {@inheritdoc} |
||
266 | */ |
||
267 | public function setWeight($weight) { |
||
271 | |||
272 | /** |
||
273 | * Prepares the entities without saving them. |
||
274 | * |
||
275 | * We need this method when we want to validate or perform other operations |
||
276 | * before submit. |
||
277 | * |
||
278 | * @param array $form |
||
279 | * Complete form. |
||
280 | * @param \Drupal\Core\Form\FormStateInterface $form_state |
||
281 | * The form state object. |
||
282 | * |
||
283 | * @return \Drupal\Core\Entity\EntityInterface[] |
||
284 | * Array of entities. |
||
285 | */ |
||
286 | abstract protected function prepareEntities(array $form, FormStateInterface $form_state); |
||
287 | |||
288 | /** |
||
289 | * {@inheritdoc} |
||
290 | */ |
||
291 | public function validate(array &$form, FormStateInterface $form_state) { |
||
301 | |||
302 | /** |
||
303 | * Run widget validators. |
||
304 | * |
||
305 | * @param array $entities |
||
306 | * Array of entity ids to validate. |
||
307 | * @param array $validators |
||
308 | * Array of widget validator ids. |
||
309 | * |
||
310 | * @return \Symfony\Component\Validator\ConstraintViolationListInterface |
||
311 | * A list of constraint violations. If the list is empty, validation |
||
312 | * succeeded. |
||
313 | */ |
||
314 | protected function runWidgetValidators(array $entities, $validators = []) { |
||
326 | |||
327 | /** |
||
328 | * {@inheritdoc} |
||
329 | */ |
||
330 | public function submit(array &$element, array &$form, FormStateInterface $form_state) {} |
||
331 | |||
332 | /** |
||
333 | * Dispatches event that informs all subscribers about new selected entities. |
||
334 | * |
||
335 | * @param array $entities |
||
336 | * Array of entities. |
||
337 | */ |
||
338 | protected function selectEntities(array $entities, FormStateInterface $form_state) { |
||
350 | |||
351 | /** |
||
352 | * {@inheritdoc} |
||
353 | */ |
||
354 | public function requiresJsCommands() { |
||
357 | |||
358 | /** |
||
359 | * Allow configuration overrides at runtime based on widget context passed to |
||
360 | * this widget from the Entity Browser element. |
||
361 | * |
||
362 | * Widgets can override this method to replace the default behavior of |
||
363 | * replacing configuration with widget context if array keys match. |
||
364 | * |
||
365 | * @param array $widget_context |
||
366 | * The widget context. |
||
367 | */ |
||
368 | protected function handleWidgetContext($widget_context) { |
||
375 | |||
376 | /** |
||
377 | * {@inheritdoc} |
||
378 | */ |
||
379 | public function access() { |
||
382 | |||
383 | } |
||
384 |
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.