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 |
||
15 | abstract class TypePluginBase extends PluginBase implements TypePluginInterface { |
||
16 | use DescribablePluginTrait; |
||
17 | |||
18 | /** |
||
19 | * {@inheritdoc} |
||
20 | */ |
||
21 | public static function createInstance(SchemaBuilderInterface $builder, TypePluginManager $manager, $definition, $id) { |
||
22 | return new ObjectType([ |
||
23 | 'name' => $definition['name'], |
||
24 | 'description' => $definition['description'], |
||
25 | View Code Duplication | 'fields' => function () use ($builder, $definition) { |
|
|
|||
26 | $fields = $builder->getFields($definition['name']); |
||
27 | |||
28 | if (!empty($definition['interfaces'])) { |
||
29 | $inherited = array_map(function ($name) use ($builder) { |
||
30 | return $builder->getFields($name); |
||
31 | }, $definition['interfaces']); |
||
32 | |||
33 | $inherited = call_user_func_array('array_merge', $inherited); |
||
34 | return array_merge($inherited, $fields); |
||
35 | } |
||
36 | |||
37 | return $fields; |
||
38 | }, |
||
39 | 'interfaces' => function () use ($builder, $definition) { |
||
40 | return array_filter(array_map(function ($name) use ($builder) { |
||
41 | return $builder->getType($name); |
||
42 | }, $definition['interfaces']), function ($type) { |
||
43 | return $type instanceof InterfaceType; |
||
44 | }); |
||
45 | }, |
||
46 | 'isTypeOf' => function ($object, $context, ResolveInfo $info) use ($manager, $id) { |
||
47 | $instance = $manager->getInstance(['id' => $id]); |
||
48 | return $instance->applies($object, $context, $info); |
||
49 | }, |
||
50 | ]); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * {@inheritdoc} |
||
55 | */ |
||
56 | public function getDefinition() { |
||
57 | $definition = $this->getPluginDefinition(); |
||
58 | |||
59 | return [ |
||
60 | 'name' => $definition['name'], |
||
61 | 'description' => $this->buildDescription($definition), |
||
62 | 'interfaces' => $this->buildInterfaces($definition), |
||
63 | 'unions' => $this->buildUnions($definition), |
||
64 | 'weight' => $definition['weight'], |
||
65 | ]; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param $definition |
||
70 | * |
||
71 | * @return array |
||
72 | */ |
||
73 | protected function buildInterfaces($definition) { |
||
76 | |||
77 | /** |
||
78 | * @param $definition |
||
79 | * |
||
80 | * @return array |
||
81 | */ |
||
82 | protected function buildUnions($definition) { |
||
85 | |||
86 | /** |
||
87 | * Checks whether this type applies to a given object. |
||
88 | * |
||
89 | * @param mixed $object |
||
90 | * The object to check against. |
||
91 | * @param \Drupal\graphql\GraphQL\Execution\ResolveContext $context |
||
92 | * The resolve context. |
||
93 | * @param \GraphQL\Type\Definition\ResolveInfo $info |
||
94 | * The resolve info object. |
||
95 | * |
||
96 | * @return null|bool |
||
97 | * TRUE if this type applies to the given object or FALSE if it doesn't. |
||
98 | */ |
||
99 | public function applies($object, ResolveContext $context, ResolveInfo $info) { |
||
102 | |||
103 | } |
||
104 |
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.