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 |
||
28 | class EntityRevisionById extends FieldPluginBase implements ContainerFactoryPluginInterface { |
||
29 | use DependencySerializationTrait; |
||
30 | |||
31 | /** |
||
32 | * The entity type manager service. |
||
33 | * |
||
34 | * @var \Drupal\Core\Entity\EntityTypeManagerInterface |
||
35 | */ |
||
36 | protected $entityTypeManager; |
||
37 | |||
38 | /** |
||
39 | * The entity repository service. |
||
40 | * |
||
41 | * @var \Drupal\Core\Entity\EntityRepositoryInterface |
||
42 | */ |
||
43 | protected $entityRepository; |
||
44 | |||
45 | /** |
||
46 | * {@inheritdoc} |
||
47 | */ |
||
48 | public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
||
57 | |||
58 | /** |
||
59 | * EntityRevisionById constructor. |
||
60 | * |
||
61 | * @param array $configuration |
||
62 | * The plugin configuration array. |
||
63 | * @param string $pluginId |
||
64 | * The plugin id. |
||
65 | * @param mixed $pluginDefinition |
||
66 | * The plugin definition. |
||
67 | * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager |
||
68 | * The entity type manager service. |
||
69 | * @param \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository |
||
70 | * The entity repository service. |
||
71 | */ |
||
72 | public function __construct( |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) { |
||
116 | |||
117 | } |
||
118 |
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.