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 |
||
20 | abstract class UnionTypePluginBase extends AbstractUnionType implements TypeSystemPluginInterface { |
||
21 | use PluginTrait; |
||
22 | use CacheablePluginTrait; |
||
23 | use NamedPluginTrait; |
||
24 | |||
25 | /** |
||
26 | * {@inheritdoc} |
||
27 | */ |
||
28 | public function __construct(array $configuration, $pluginId, $pluginDefinition) { |
||
31 | |||
32 | /** |
||
33 | * {@inheritdoc} |
||
34 | */ |
||
35 | View Code Duplication | public function buildConfig(SchemaBuilder $schemaManager) { |
|
44 | |||
45 | /** |
||
46 | * {@inheritdoc} |
||
47 | */ |
||
48 | public function build($config) { |
||
51 | |||
52 | /** |
||
53 | * Builds the list of types that are contained within this union type. |
||
54 | * |
||
55 | * @param \Drupal\graphql\Plugin\GraphQL\SchemaBuilder $schemaManager |
||
56 | * The schema manager. |
||
57 | * @param $name |
||
58 | * The name of this plugin. |
||
59 | * |
||
60 | * @return \Drupal\graphql\Plugin\GraphQL\Types\TypePluginBase[] |
||
61 | * An array of types to add to this union type. |
||
62 | */ |
||
63 | protected function buildTypes(SchemaBuilder $schemaManager, $name) { |
||
74 | |||
75 | /** |
||
76 | * Default implementation of "resolveType". |
||
77 | * |
||
78 | * Checks all implementing types and returns the matching type with the |
||
79 | * highest weight. |
||
80 | * |
||
81 | * @param mixed $object |
||
82 | * The current response tree value. |
||
83 | * |
||
84 | * @return \Drupal\graphql\Plugin\GraphQL\Types\TypePluginBase |
||
85 | * The type object. |
||
86 | */ |
||
87 | public function resolveType($object) { |
||
97 | |||
98 | /** |
||
99 | * Default implementation of "getTypes". |
||
100 | * |
||
101 | * @return \Drupal\graphql\Plugin\GraphQL\Types\TypePluginBase[] |
||
102 | * The types contained within this union type. |
||
103 | */ |
||
104 | public function getTypes() { |
||
107 | |||
108 | } |
||
109 |
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.