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 |
||
14 | abstract class InputTypePluginBase extends PluginBase implements TypePluginInterface { |
||
15 | use DescribablePluginTrait; |
||
16 | use TypedPluginTrait; |
||
17 | |||
18 | /** |
||
19 | * {@inheritdoc} |
||
20 | */ |
||
21 | public static function createInstance(SchemaBuilderInterface $builder, TypePluginManager $manager, $definition, $id) { |
||
30 | |||
31 | /** |
||
32 | * {@inheritdoc} |
||
33 | */ |
||
34 | View Code Duplication | public function getDefinition() { |
|
43 | |||
44 | /** |
||
45 | * Builds the fields of the type definition. |
||
46 | * |
||
47 | * @param $definition |
||
48 | * The plugin definition array. |
||
49 | * |
||
50 | * @return array |
||
51 | * The list of fields for the input type. |
||
52 | */ |
||
53 | protected function buildFields($definition) { |
||
62 | |||
63 | /** |
||
64 | * Builds a field's type. |
||
65 | * |
||
66 | * @param array $field |
||
67 | * The field definition array. |
||
68 | * |
||
69 | * @return array |
||
70 | * The parsed type definition array. |
||
71 | */ |
||
72 | protected function buildFieldType($field) { |
||
76 | |||
77 | /** |
||
78 | * Builds a field's description. |
||
79 | * |
||
80 | * @param array $field |
||
81 | * The field definition array. |
||
82 | * @param array $definition |
||
83 | * The plugin definition array. |
||
84 | * |
||
85 | * @return string |
||
86 | * The field's description. |
||
87 | */ |
||
88 | protected function buildFieldDescription($field, $definition) { |
||
91 | |||
92 | /** |
||
93 | * Builds a field's default value. |
||
94 | * |
||
95 | * @param array $field |
||
96 | * The field definition array. |
||
97 | * @param array $definition |
||
98 | * The plugin definition array. |
||
99 | * |
||
100 | * @return mixed |
||
101 | * The field's default value. |
||
102 | */ |
||
103 | protected function buildFieldDefault($field, $definition) { |
||
106 | } |
||
107 |
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.