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 |
||
26 | View Code Duplication | class Translate extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
27 | use DependencySerializationTrait; |
||
28 | |||
29 | /** |
||
30 | * The language manager service. |
||
31 | * |
||
32 | * @var \Drupal\Core\Language\LanguageManagerInterface |
||
33 | */ |
||
34 | protected $languageManager; |
||
35 | |||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | */ |
||
39 | public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
||
40 | return new static( |
||
41 | $configuration, |
||
42 | $pluginId, |
||
43 | $pluginDefinition, |
||
44 | $container->get('language_manager') |
||
45 | ); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Alias constructor. |
||
50 | * |
||
51 | * @param array $configuration |
||
52 | * The plugin configuration array. |
||
53 | * @param string $pluginId |
||
54 | * The plugin id. |
||
55 | * @param mixed $pluginDefinition |
||
56 | * The plugin definition. |
||
57 | * @param \Drupal\Core\Language\LanguageManagerInterface $languageManager |
||
58 | * The language manager service. |
||
59 | */ |
||
60 | public function __construct( |
||
61 | array $configuration, |
||
62 | $pluginId, |
||
63 | $pluginDefinition, |
||
64 | LanguageManagerInterface $languageManager |
||
65 | ) { |
||
66 | parent::__construct($configuration, $pluginId, $pluginDefinition); |
||
67 | $this->languageManager = $languageManager; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | public function resolveValues($value, array $args, ResolveInfo $info) { |
||
79 | |||
80 | } |
||
81 |