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 |
||
19 | abstract class EntityFieldDeriverBase extends DeriverBase implements ContainerDeriverInterface { |
||
20 | use DependencySerializationTrait; |
||
21 | |||
22 | /** |
||
23 | * Provides plugin definition values from fields. |
||
24 | * |
||
25 | * @param \Drupal\Core\Field\FieldDefinitionInterface $fieldDefinition |
||
26 | * Field definition object. |
||
27 | * @param array $basePluginDefinition |
||
28 | * Base definition array. |
||
29 | * |
||
30 | * @return array |
||
31 | * The derived plugin definitions for the given field. |
||
32 | */ |
||
33 | abstract protected function getDerivativeDefinitionsFromFieldDefinition(FieldDefinitionInterface $fieldDefinition, array $basePluginDefinition); |
||
34 | |||
35 | /** |
||
36 | * The entity type manager. |
||
37 | * |
||
38 | * @var \Drupal\Core\Entity\EntityTypeManagerInterface |
||
39 | */ |
||
40 | protected $entityTypeManager; |
||
41 | |||
42 | /** |
||
43 | * The entity field manager. |
||
44 | * |
||
45 | * @var \Drupal\Core\Entity\EntityFieldManagerInterface |
||
46 | */ |
||
47 | protected $entityFieldManager; |
||
48 | |||
49 | /** |
||
50 | * The entity bundle info. |
||
51 | * |
||
52 | * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface |
||
53 | */ |
||
54 | protected $entityBundleInfo; |
||
55 | |||
56 | /** |
||
57 | * The base plugin id. |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $basePluginId; |
||
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | public static function create(ContainerInterface $container, $basePluginId) { |
||
74 | |||
75 | /** |
||
76 | * RawValueFieldItemDeriver constructor. |
||
77 | * |
||
78 | * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager |
||
79 | * The entity type manager. |
||
80 | * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager |
||
81 | * The entity field manager. |
||
82 | * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entityTypeBundleInfo |
||
83 | * The bundle info service. |
||
84 | * @param string $basePluginId |
||
85 | * The base plugin id. |
||
86 | */ |
||
87 | public function __construct( |
||
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | public function getDerivativeDefinitions($basePluginDefinition) { |
||
125 | |||
126 | } |
||
127 |
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.