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 |
||
23 | class Upload extends WidgetBase { |
||
24 | |||
25 | /** |
||
26 | * The module handler service. |
||
27 | * |
||
28 | * @var \Drupal\Core\Extension\ModuleHandlerInterface |
||
29 | */ |
||
30 | protected $moduleHandler; |
||
31 | |||
32 | /** |
||
33 | * The token service. |
||
34 | * |
||
35 | * @var \Drupal\Core\Utility\Token |
||
36 | */ |
||
37 | protected $token; |
||
38 | |||
39 | /** |
||
40 | * Constructs upload plugin. |
||
41 | * |
||
42 | * @param array $configuration |
||
43 | * A configuration array containing information about the plugin instance. |
||
44 | * @param string $plugin_id |
||
45 | * The plugin_id for the plugin instance. |
||
46 | * @param mixed $plugin_definition |
||
47 | * The plugin implementation definition. |
||
48 | * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher |
||
49 | * Event dispatcher service. |
||
50 | * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager |
||
51 | * The entity manager. |
||
52 | * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler |
||
53 | * The module handler. |
||
54 | * @param \Drupal\Core\Utility\Token $token |
||
55 | * The token service. |
||
56 | */ |
||
57 | public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, Token $token) { |
||
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
||
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | */ |
||
81 | public function defaultConfiguration() { |
||
86 | |||
87 | /** |
||
88 | * {@inheritdoc} |
||
89 | */ |
||
90 | View Code Duplication | public function getForm(array &$original_form, FormStateInterface $form_state, array $aditional_widget_parameters) { |
|
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | public function validate(array &$form, FormStateInterface $form_state) { |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | public function submit(array &$element, array &$form, FormStateInterface $form_state) { |
||
123 | |||
124 | /** |
||
125 | * Clear values from upload form element. |
||
126 | * |
||
127 | * @param array $element |
||
128 | * Upload form element. |
||
129 | * @param \Drupal\Core\Form\FormStateInterface $form_state |
||
130 | * Form state object. |
||
131 | */ |
||
132 | protected function clearFormValues(array &$element, FormStateInterface $form_state) { |
||
138 | |||
139 | /** |
||
140 | * @param FormStateInterface $form_state |
||
141 | * Form state object. |
||
142 | * |
||
143 | * @return \Drupal\file\FileInterface[] |
||
144 | * Array of files. |
||
145 | */ |
||
146 | protected function extractFiles(FormStateInterface $form_state) { |
||
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | */ |
||
162 | public function buildConfigurationForm(array $form, FormStateInterface $form_state) { |
||
179 | |||
180 | } |
||
181 |
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.