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 |
||
22 | abstract class InterfacePluginBase extends AbstractInterfaceType implements TypeSystemPluginInterface { |
||
23 | use PluginTrait; |
||
24 | use CacheablePluginTrait; |
||
25 | use NamedPluginTrait; |
||
26 | use FieldablePluginTrait; |
||
27 | |||
28 | /** |
||
29 | * The list of types that implement this interface. |
||
30 | * |
||
31 | * @var \Youshido\GraphQL\Type\Object\AbstractObjectType[] |
||
32 | */ |
||
33 | protected $types = []; |
||
34 | |||
35 | /** |
||
36 | * {@inheritdoc} |
||
37 | */ |
||
38 | public function __construct(array $configuration, $pluginId, $pluginDefinition) { |
||
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | */ |
||
45 | View Code Duplication | public function buildConfig(SchemaBuilder $schemaManager) { |
|
|
|||
46 | $this->config = new InterfaceTypeConfig([ |
||
47 | 'name' => $this->buildName(), |
||
48 | 'description' => $this->buildDescription(), |
||
49 | 'fields' => $this->buildFields($schemaManager), |
||
50 | ]); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * {@inheritdoc} |
||
55 | */ |
||
56 | public function build($config) { |
||
59 | |||
60 | /** |
||
61 | * Builds the list of types that are contained within this union type. |
||
62 | * |
||
63 | * @param \Drupal\graphql\Plugin\GraphQL\Types\TypePluginBase $type |
||
64 | * The name of this plugin. |
||
65 | */ |
||
66 | public function addType(TypePluginBase $type) { |
||
69 | |||
70 | /** |
||
71 | * Default implementation of "resolveType". |
||
72 | * |
||
73 | * Checks all implementing types and returns the matching type with the |
||
74 | * highest weight. |
||
75 | * |
||
76 | * @param mixed $object |
||
77 | * The current response tree value. |
||
78 | * |
||
79 | * @return \Youshido\GraphQL\Type\Object\AbstractObjectType|null |
||
80 | * The type object. |
||
81 | */ |
||
82 | public function resolveType($object) { |
||
91 | |||
92 | } |
||
93 |
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.