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 |
||
43 | class Modal extends DisplayBase implements DisplayRouterInterface { |
||
44 | |||
45 | /** |
||
46 | * Current route match service. |
||
47 | * |
||
48 | * @var \Drupal\Core\Routing\RouteMatchInterface |
||
49 | */ |
||
50 | protected $currentRouteMatch; |
||
51 | |||
52 | /** |
||
53 | * UUID generator interface. |
||
54 | * |
||
55 | * @var \Drupal\Component\Uuid\UuidInterface |
||
56 | */ |
||
57 | protected $uuidGenerator; |
||
58 | |||
59 | /** |
||
60 | * Current path. |
||
61 | * |
||
62 | * @var \Drupal\Core\Path\CurrentPathStack |
||
63 | */ |
||
64 | protected $currentPath; |
||
65 | |||
66 | /** |
||
67 | * UIID string. |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | protected $uuid = NULL; |
||
72 | |||
73 | /** |
||
74 | * Current request. |
||
75 | * |
||
76 | * @var \Symfony\Component\HttpFoundation\Request |
||
77 | */ |
||
78 | protected $request; |
||
79 | |||
80 | /** |
||
81 | * Constructs display plugin. |
||
82 | * |
||
83 | * @param array $configuration |
||
84 | * A configuration array containing information about the plugin instance. |
||
85 | * @param string $plugin_id |
||
86 | * The plugin_id for the plugin instance. |
||
87 | * @param mixed $plugin_definition |
||
88 | * The plugin implementation definition. |
||
89 | * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher |
||
90 | * Event dispatcher service. |
||
91 | * @param \Drupal\Core\Routing\RouteMatchInterface |
||
92 | * The currently active route match object. |
||
93 | * @param \Drupal\Component\Uuid\UuidInterface |
||
94 | * UUID generator interface. |
||
95 | */ |
||
96 | public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, RouteMatchInterface $current_route_match, UuidInterface $uuid, CurrentPathStack $current_path, Request $request) { |
||
97 | parent::__construct($configuration, $plugin_id, $plugin_definition, $event_dispatcher); |
||
98 | $this->currentRouteMatch = $current_route_match; |
||
99 | $this->uuidGenerator = $uuid; |
||
100 | $this->currentPath = $current_path; |
||
101 | $this->request = $request; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | View Code Duplication | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
|
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | View Code Duplication | public function defaultConfiguration() { |
|
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | public function displayEntityBrowser() { |
||
185 | |||
186 | /** |
||
187 | * Generates the content and opens the modal. |
||
188 | * |
||
189 | * @param array $form |
||
190 | * The form array. |
||
191 | * @param \Drupal\Core\Form\FormStateInterface $form_state |
||
192 | * The form state object. |
||
193 | * |
||
194 | * @return \Drupal\Core\Ajax\AjaxResponse |
||
195 | * An ajax response. |
||
196 | */ |
||
197 | public function openModal(array &$form, FormStateInterface $form_state) { |
||
226 | |||
227 | /** |
||
228 | * {@inheritdoc} |
||
229 | */ |
||
230 | public function selectionCompleted(array $entities) { |
||
234 | |||
235 | /** |
||
236 | * {@inheritdoc} |
||
237 | */ |
||
238 | public function addAjax(array &$form) { |
||
254 | |||
255 | /** |
||
256 | * Ajax callback for entity browser form. |
||
257 | * |
||
258 | * Allows the entity browser form to submit the form via ajax. |
||
259 | * |
||
260 | * @param array $form |
||
261 | * The form array. |
||
262 | * @param FormStateInterface $form_state |
||
263 | * The form state object. |
||
264 | * |
||
265 | * @return \Drupal\Core\Ajax\AjaxResponse |
||
266 | * Response. |
||
267 | */ |
||
268 | public function widgetAjaxCallback(array &$form, FormStateInterface $form_state) { |
||
282 | |||
283 | /** |
||
284 | * Helper function to return commands to return in AjaxResponse. |
||
285 | * |
||
286 | * @return array |
||
287 | * An array of ajax commands. |
||
288 | */ |
||
289 | public function getAjaxCommands(FormStateInterface $form_state) { |
||
297 | |||
298 | /** |
||
299 | * KernelEvents::RESPONSE listener. |
||
300 | * |
||
301 | * Intercepts default response and injects |
||
302 | * response that will trigger JS to propagate selected entities upstream. |
||
303 | * |
||
304 | * @param FilterResponseEvent $event |
||
305 | * Response event. |
||
306 | */ |
||
307 | View Code Duplication | public function propagateSelection(FilterResponseEvent $event) { |
|
327 | |||
328 | /** |
||
329 | * {@inheritdoc} |
||
330 | */ |
||
331 | public function path() { |
||
334 | |||
335 | /** |
||
336 | * {@inheritdoc} |
||
337 | */ |
||
338 | public function getUuid() { |
||
344 | |||
345 | /** |
||
346 | * {@inheritdoc} |
||
347 | */ |
||
348 | public function setUuid($uuid) { |
||
351 | |||
352 | /** |
||
353 | * @inheritDoc |
||
354 | */ |
||
355 | public function __sleep() { |
||
358 | |||
359 | } |
||
360 |
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.