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 |
||
21 | abstract class FieldFormatterEntityEmbedDisplayBase extends EntityEmbedDisplayBase { |
||
22 | use PluginDependencyTrait; |
||
23 | |||
24 | /** |
||
25 | * The field formatter plugin manager. |
||
26 | * |
||
27 | * @var \Drupal\Core\Field\FormatterPluginManager |
||
28 | */ |
||
29 | protected $formatterPluginManager; |
||
30 | |||
31 | /** |
||
32 | * The typed data manager. |
||
33 | * |
||
34 | * @var \Drupal\Core\TypedData\TypedDataManager |
||
35 | */ |
||
36 | protected $typedDataManager; |
||
37 | |||
38 | /** |
||
39 | * The field definition. |
||
40 | * |
||
41 | * @var \Drupal\Core\Field\BaseFieldDefinition |
||
42 | */ |
||
43 | protected $fieldDefinition; |
||
44 | |||
45 | /** |
||
46 | * The field formatter. |
||
47 | * |
||
48 | * @var \Drupal\Core\Field\FormatterInterface |
||
49 | */ |
||
50 | protected $fieldFormatter; |
||
51 | |||
52 | /** |
||
53 | * Constructs a FieldFormatterEntityEmbedDisplayBase object. |
||
54 | * |
||
55 | * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager |
||
56 | * The entity manager service. |
||
57 | * @param \Drupal\Core\Field\FormatterPluginManager $formatter_plugin_manager |
||
58 | * The field formatter plugin manager. |
||
59 | * @param \Drupal\Core\TypedData\TypedDataManager $typed_data_manager |
||
60 | * The typed data manager. |
||
61 | */ |
||
62 | public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, FormatterPluginManager $formatter_plugin_manager, TypedDataManager $typed_data_manager) { |
||
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | View Code Duplication | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
|
83 | |||
84 | /** |
||
85 | * Get the FieldDefinition object required to render this field's formatter. |
||
86 | * |
||
87 | * @return \Drupal\Core\Field\BaseFieldDefinition |
||
88 | * The field definition. |
||
89 | * |
||
90 | * @see \Drupal\entity_embed\FieldFormatterEntityEmbedDisplayBase::build() |
||
91 | */ |
||
92 | public function getFieldDefinition() { |
||
103 | |||
104 | /** |
||
105 | * Get the field value required to pass into the field formatter. |
||
106 | * |
||
107 | * @return mixed |
||
108 | * The field value. |
||
109 | */ |
||
110 | abstract public function getFieldValue(); |
||
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | public function access(AccountInterface $account = NULL) { |
||
118 | |||
119 | /** |
||
120 | * Checks if the field formatter is applicable. |
||
121 | * |
||
122 | * @return \Drupal\Core\Access\AccessResult |
||
123 | * Returns the access result. |
||
124 | */ |
||
125 | protected function isApplicableFieldFormatter() { |
||
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | public function build() { |
||
161 | |||
162 | /** |
||
163 | * {@inheritdoc} |
||
164 | */ |
||
165 | public function defaultConfiguration() { |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | public function buildConfigurationForm(array $form, FormStateInterface $form_state) { |
||
175 | |||
176 | /** |
||
177 | * Constructs a field formatter. |
||
178 | * |
||
179 | * @return \Drupal\Core\Field\FormatterInterface |
||
180 | * The formatter object. |
||
181 | */ |
||
182 | public function getFieldFormatter() { |
||
203 | |||
204 | /** |
||
205 | * Creates a new faux-field definition. |
||
206 | * |
||
207 | * @param string $type |
||
208 | * The type of the field. |
||
209 | * |
||
210 | * @return \Drupal\Core\Field\BaseFieldDefinition |
||
211 | * A new field definition. |
||
212 | */ |
||
213 | protected function createFieldDefinition($type) { |
||
219 | |||
220 | /** |
||
221 | * {@inheritdoc} |
||
222 | */ |
||
223 | public function calculateDependencies() { |
||
233 | |||
234 | } |
||
235 |
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.