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 | abstract class WidgetBase extends PluginBase implements WidgetInterface, ContainerFactoryPluginInterface { |
||
24 | |||
25 | /** |
||
26 | * Plugin id. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $id; |
||
31 | |||
32 | /** |
||
33 | * Plugin uuid. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $uuid; |
||
38 | /** |
||
39 | * Plugin label. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $label; |
||
44 | |||
45 | /** |
||
46 | * Plugin weight. |
||
47 | * |
||
48 | * @var int |
||
49 | */ |
||
50 | protected $weight; |
||
51 | |||
52 | /** |
||
53 | * Event dispatcher service. |
||
54 | * |
||
55 | * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface |
||
56 | */ |
||
57 | protected $eventDispatcher; |
||
58 | |||
59 | /** |
||
60 | * Entity manager service. |
||
61 | * |
||
62 | * @var \Drupal\Core\Entity\EntityManagerInterface |
||
63 | */ |
||
64 | protected $entityManager; |
||
65 | |||
66 | /** |
||
67 | * The Expirable key value store. |
||
68 | * |
||
69 | * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface |
||
70 | */ |
||
71 | private $keyValue; |
||
72 | |||
73 | /** |
||
74 | * The Request object. |
||
75 | * |
||
76 | * @var \Symfony\Component\HttpFoundation\Request |
||
77 | */ |
||
78 | private $request; |
||
79 | |||
80 | /** |
||
81 | * Constructs widget 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 | */ |
||
92 | View Code Duplication | public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityManagerInterface $entity_manager, KeyValueStoreExpirableInterface $key_value, Request $request) { |
|
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | View Code Duplication | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
|
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | public function defaultConfiguration() { |
||
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | public function getConfiguration() { |
||
135 | |||
136 | /** |
||
137 | * {@inheritdoc} |
||
138 | */ |
||
139 | public function setConfiguration(array $configuration) { |
||
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | public function calculateDependencies() { |
||
161 | |||
162 | /** |
||
163 | * {@inheritdoc} |
||
164 | */ |
||
165 | public function id() { |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | public function uuid() { |
||
175 | |||
176 | /** |
||
177 | * {@inheritdoc} |
||
178 | */ |
||
179 | public function label() { |
||
182 | |||
183 | /** |
||
184 | * {@inheritdoc} |
||
185 | */ |
||
186 | public function getWeight() { |
||
189 | |||
190 | /** |
||
191 | * {@inheritdoc} |
||
192 | */ |
||
193 | public function setWeight($weight) { |
||
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | */ |
||
201 | public function validate(array &$form, FormStateInterface $form_state) {} |
||
202 | |||
203 | |||
204 | /** |
||
205 | * {@inheritdoc} |
||
206 | */ |
||
207 | public function runWidgetValidators(array $entities, $validators = []) { |
||
223 | |||
224 | /** |
||
225 | * {@inheritdoc} |
||
226 | */ |
||
227 | public function submit(array &$element, array &$form, FormStateInterface $form_state) {} |
||
228 | |||
229 | /** |
||
230 | * Dispatches event that informs all subscribers about new selected entities. |
||
231 | * |
||
232 | * @param array $entities |
||
233 | * Array of entities. |
||
234 | */ |
||
235 | protected function selectEntities(array $entities, FormStateInterface $form_state) { |
||
247 | |||
248 | } |
||
249 |
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.