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 |
||
25 | class Upload extends WidgetBase { |
||
26 | |||
27 | /** |
||
28 | * The module handler service. |
||
29 | * |
||
30 | * @var \Drupal\Core\Extension\ModuleHandlerInterface |
||
31 | */ |
||
32 | protected $moduleHandler; |
||
33 | |||
34 | /** |
||
35 | * The token service. |
||
36 | * |
||
37 | * @var \Drupal\Core\Utility\Token |
||
38 | */ |
||
39 | protected $token; |
||
40 | |||
41 | /** |
||
42 | * Upload constructor. |
||
43 | * |
||
44 | * @param array $configuration |
||
45 | * A configuration array containing information about the plugin instance. |
||
46 | * @param string $plugin_id |
||
47 | * The plugin_id for the plugin instance. |
||
48 | * @param mixed $plugin_definition |
||
49 | * The plugin implementation definition. |
||
50 | * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher |
||
51 | * Event dispatcher service. |
||
52 | * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager |
||
53 | * The entity type manager service. |
||
54 | * @param \Drupal\entity_browser\WidgetValidationManager $validation_manager |
||
55 | * The Widget Validation Manager service. |
||
56 | * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler |
||
57 | * The module handler. |
||
58 | * @param \Drupal\Core\Utility\Token $token |
||
59 | * The token service. |
||
60 | */ |
||
61 | public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityTypeManagerInterface $entity_type_manager, WidgetValidationManager $validation_manager, ModuleHandlerInterface $module_handler, Token $token) { |
||
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | View Code Duplication | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
|
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | public function defaultConfiguration() { |
||
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | public function getForm(array &$original_form, FormStateInterface $form_state, array $additional_widget_parameters) { |
||
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | protected function prepareEntities(array $form, FormStateInterface $form_state) { |
||
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | public function submit(array &$element, array &$form, FormStateInterface $form_state) { |
||
145 | |||
146 | /** |
||
147 | * Clear values from upload form element. |
||
148 | * |
||
149 | * @param array $element |
||
150 | * Upload form element. |
||
151 | * @param \Drupal\Core\Form\FormStateInterface $form_state |
||
152 | * Form state object. |
||
153 | */ |
||
154 | protected function clearFormValues(array &$element, FormStateInterface $form_state) { |
||
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | public function buildConfigurationForm(array $form, FormStateInterface $form_state) { |
||
201 | |||
202 | /** |
||
203 | * Validates a list of file extensions. |
||
204 | * |
||
205 | * @See \Drupal\file\Plugin\Field\FieldType\FileItem::validateExtensions |
||
206 | */ |
||
207 | public static function validateExtensions($element, FormStateInterface $form_state) { |
||
220 | |||
221 | } |
||
222 |
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.