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 |
||
22 | class EntityForm extends WidgetBase { |
||
23 | |||
24 | /** |
||
25 | * The entity type bundle info service. |
||
26 | * |
||
27 | * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface |
||
28 | */ |
||
29 | protected $entityTypeBundleInfo; |
||
30 | |||
31 | /** |
||
32 | * Constructs widget plugin. |
||
33 | * |
||
34 | * @param array $configuration |
||
35 | * A configuration array containing information about the plugin instance. |
||
36 | * @param string $plugin_id |
||
37 | * The plugin_id for the plugin instance. |
||
38 | * @param mixed $plugin_definition |
||
39 | * The plugin implementation definition. |
||
40 | * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher |
||
41 | * Event dispatcher service. |
||
42 | * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager |
||
43 | * The entity type manager service. |
||
44 | * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info |
||
45 | * The entity type bundle info service. |
||
46 | */ |
||
47 | public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info) { |
||
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | View Code Duplication | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
|
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | public function defaultConfiguration() { |
||
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | public function getForm(array &$original_form, FormStateInterface $form_state, array $aditional_widget_parameters) { |
||
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | public function submit(array &$element, array &$form, FormStateInterface $form_state) { |
||
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | public function buildConfigurationForm(array $form, FormStateInterface $form_state) { |
||
151 | |||
152 | /** |
||
153 | * AJAX callback for bundle dropdown update. |
||
154 | */ |
||
155 | public function updateBundle($form, FormStateInterface $form_state) { |
||
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | */ |
||
162 | public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { |
||
166 | |||
167 | } |
||
168 |
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.