Completed
Pull Request — 8.x-3.x (#525)
by Sebastian
06:05
created

TypePluginBase   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 89
Duplicated Lines 15.73 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 14
loc 89
rs 10
c 1
b 0
f 0
wmc 6
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A buildInterfaces() 0 3 1
A buildUnions() 0 3 1
A applies() 0 3 1
B createInstance() 14 31 2
A getDefinition() 0 11 1

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace Drupal\graphql\Plugin\GraphQL\Types;
4
5
use Drupal\Component\Plugin\PluginBase;
6
use Drupal\graphql\GraphQL\Execution\ResolveContext;
7
use Drupal\graphql\Plugin\GraphQL\Traits\DescribablePluginTrait;
8
use Drupal\graphql\Plugin\SchemaBuilderInterface;
9
use Drupal\graphql\Plugin\TypePluginInterface;
10
use Drupal\graphql\Plugin\TypePluginManager;
11
use GraphQL\Type\Definition\InterfaceType;
12
use GraphQL\Type\Definition\ObjectType;
13
use GraphQL\Type\Definition\ResolveInfo;
14
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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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) {
74
    return array_unique($definition['interfaces']);
75
  }
76
77
  /**
78
   * @param $definition
79
   *
80
   * @return array
81
   */
82
  protected function buildUnions($definition) {
83
    return array_unique($definition['unions']);
84
  }
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) {
100
    return NULL;
101
  }
102
103
}
104