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 |
||
32 | View Code Duplication | class MenuByName extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
|
|||
33 | use DependencySerializationTrait; |
||
34 | |||
35 | /** |
||
36 | * The entity type manager. |
||
37 | * |
||
38 | * @var \Drupal\Core\Entity\EntityTypeManagerInterface |
||
39 | */ |
||
40 | protected $entityTypeManager; |
||
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | */ |
||
45 | public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
||
48 | |||
49 | /** |
||
50 | * MenuByName constructor. |
||
51 | * |
||
52 | * @param array $configuration |
||
53 | * The plugin configuration array. |
||
54 | * @param string $pluginId |
||
55 | * The plugin id. |
||
56 | * @param mixed $pluginDefinition |
||
57 | * The plugin definition. |
||
58 | * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager |
||
59 | * The entity type manager service. |
||
60 | */ |
||
61 | public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeManagerInterface $entityTypeManager) { |
||
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) { |
||
76 | |||
77 | } |
||
78 |
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.