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 |
||
16 | class WidgetsConfig extends FormBase { |
||
17 | |||
18 | /** |
||
19 | * Entity browser widget plugin manager. |
||
20 | * |
||
21 | * @var \Drupal\entity_browser\WidgetManager |
||
22 | */ |
||
23 | protected $widgetManager; |
||
24 | |||
25 | /** |
||
26 | * Tempstore Factory for keeping track of values in each step of the wizard. |
||
27 | * |
||
28 | * @var \Drupal\user\SharedTempStoreFactory |
||
29 | */ |
||
30 | protected $tempStore; |
||
31 | |||
32 | /** |
||
33 | * WidgetsConfig constructor. |
||
34 | * @param \Drupal\entity_browser\WidgetManager $widget_manager |
||
35 | * @param \Drupal\user\SharedTempStoreFactory $temp_store |
||
36 | * @param null $tempstore_id |
||
37 | * @param null $machine_name |
||
38 | */ |
||
39 | function __construct(WidgetManager $widget_manager, SharedTempStoreFactory $temp_store, $tempstore_id = NULL, $machine_name = NULL) { |
||
|
|||
40 | $this->widgetManager = $widget_manager; |
||
41 | $this->tempStore = $temp_store; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | */ |
||
47 | public static function create(ContainerInterface $container) { |
||
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | public function getFormId() { |
||
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | public function buildForm(array $form, FormStateInterface $form_state) { |
||
135 | |||
136 | /** |
||
137 | * @param $form |
||
138 | * @param \Drupal\Core\Form\FormStateInterface $form_state |
||
139 | * @param null $tempstore_id |
||
140 | */ |
||
141 | public static function submitAddWidget($form, FormStateInterface $form_state, $tempstore_id = NULL) { |
||
158 | |||
159 | /** |
||
160 | * @param $form |
||
161 | * @param $form_state |
||
162 | * @return mixed |
||
163 | */ |
||
164 | public static function addWidgetCallback($form, $form_state) { |
||
167 | |||
168 | /** |
||
169 | * {@inheritdoc} |
||
170 | */ |
||
171 | View Code Duplication | public function validateForm(array &$form, FormStateInterface $form_state) { |
|
179 | |||
180 | /** |
||
181 | * {@inheritdoc} |
||
182 | */ |
||
183 | View Code Duplication | public function submitForm(array &$form, FormStateInterface $form_state) { |
|
191 | |||
192 | } |
||
193 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.