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 |
||
28 | class EntityForm extends WidgetBase { |
||
29 | |||
30 | /** |
||
31 | * The entity type bundle info service. |
||
32 | * |
||
33 | * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface |
||
34 | */ |
||
35 | protected $entityTypeBundleInfo; |
||
36 | |||
37 | /** |
||
38 | * The entity display repository. |
||
39 | * |
||
40 | * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface |
||
41 | */ |
||
42 | protected $entityDisplayRepository; |
||
43 | |||
44 | /** |
||
45 | * Constructs widget plugin. |
||
46 | * |
||
47 | * @param array $configuration |
||
48 | * A configuration array containing information about the plugin instance. |
||
49 | * @param string $plugin_id |
||
50 | * The plugin_id for the plugin instance. |
||
51 | * @param mixed $plugin_definition |
||
52 | * The plugin implementation definition. |
||
53 | * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher |
||
54 | * Event dispatcher service. |
||
55 | * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager |
||
56 | * The entity type manager service. |
||
57 | * @param \Drupal\entity_browser\WidgetValidationManager $validation_manager |
||
58 | * The Widget Validation Manager service. |
||
59 | * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info |
||
60 | * The entity type bundle info service. |
||
61 | * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository |
||
62 | * The entity display repository. |
||
63 | */ |
||
64 | public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityTypeManagerInterface $entity_type_manager, WidgetValidationManager $validation_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityDisplayRepositoryInterface $entity_display_repository) { |
||
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | View Code Duplication | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
|
|
|||
74 | return new static( |
||
75 | $configuration, |
||
76 | $plugin_id, |
||
77 | $plugin_definition, |
||
78 | $container->get('event_dispatcher'), |
||
79 | $container->get('entity_type.manager'), |
||
80 | $container->get('plugin.manager.entity_browser.widget_validation'), |
||
81 | $container->get('entity_type.bundle.info'), |
||
82 | $container->get('entity_display.repository') |
||
83 | ); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | public function defaultConfiguration() { |
||
97 | |||
98 | /** |
||
99 | * {@inheritdoc} |
||
100 | */ |
||
101 | public function getForm(array &$original_form, FormStateInterface $form_state, array $additional_widget_parameters) { |
||
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | protected function prepareEntities(array $form, FormStateInterface $form_state) { |
||
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | public function submit(array &$element, array &$form, FormStateInterface $form_state) { |
||
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | public function buildConfigurationForm(array $form, FormStateInterface $form_state) { |
||
208 | |||
209 | /** |
||
210 | * AJAX callback for bundle dropdown update. |
||
211 | */ |
||
212 | public function updateBundle($form, FormStateInterface $form_state) { |
||
215 | |||
216 | /** |
||
217 | * AJAX callback for the Form Mode dropdown update. |
||
218 | */ |
||
219 | public function updateFormMode($form, FormStateInterface $form_state) { |
||
222 | |||
223 | /** |
||
224 | * AJAX callback to update the two form elements: bundle and form_mode. |
||
225 | */ |
||
226 | public function updateFormElements($form, FormStateInterface $form_state) { |
||
232 | |||
233 | /** |
||
234 | * {@inheritdoc} |
||
235 | */ |
||
236 | public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { |
||
241 | |||
242 | /** |
||
243 | * {@inheritdoc} |
||
244 | */ |
||
245 | public function access() { |
||
250 | |||
251 | } |
||
252 |
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.