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

TypePluginBase   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 79
Duplicated Lines 11.39 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 79
rs 10
wmc 5
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
B createInstance() 0 31 2
A getDefinition() 9 9 1
A buildInterfaces() 0 3 1
A applies() 0 3 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\Plugin\SchemaBuilder;
7
use Drupal\graphql\Plugin\GraphQL\Traits\CacheablePluginTrait;
8
use Drupal\graphql\Plugin\GraphQL\Traits\DescribablePluginTrait;
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 CacheablePluginTrait;
17
  use DescribablePluginTrait;
18
19
  /**
20
   * {@inheritdoc}
21
   */
22
  public static function createInstance(SchemaBuilder $builder, TypePluginManager $manager, $definition, $id) {
23
    return new ObjectType([
24
      'name' => $definition['name'],
25
      'description' => $definition['description'],
26
      'fields' => function () use ($builder, $definition) {
27
        $fields = $builder->getFields($definition['name']);
28
29
        if (!empty($definition['interfaces'])) {
30
          $inherited = array_map(function ($name) use ($builder) {
31
            return $builder->getFields($name);
32
          }, $definition['interfaces']);
33
34
          $inherited = call_user_func_array('array_merge', $inherited);
35
          return array_merge($inherited, $fields);
36
        }
37
38
        return $fields;
39
      },
40
      'interfaces' => function () use ($builder, $definition) {
41
        return array_filter(array_map(function ($name) use ($builder) {
42
          return $builder->getType($name);
43
        }, $definition['interfaces']), function ($type) {
44
          return $type instanceof InterfaceType;
45
        });
46
      },
47
      'isTypeOf' => function ($object, $context, ResolveInfo $info) use ($manager, $id) {
48
        $instance = $manager->createInstance($id);
49
        return $instance->applies($object, $context, $info);
50
      }
51
    ]);
52
  }
53
54
  /**
55
   * {@inheritdoc}
56
   */
57 View Code Duplication
  public function getDefinition() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
58
    $definition = $this->getPluginDefinition();
59
60
    return [
61
      'name' => $definition['name'],
62
      'description' => $this->buildDescription($definition),
63
      'interfaces' => $this->buildInterfaces($definition),
64
    ];
65
  }
66
67
  /**
68
   * @param $definition
69
   *
70
   * @return array
71
   */
72
  protected function buildInterfaces($definition) {
73
    return array_unique($definition['interfaces']);
74
  }
75
76
  /**
77
   * Checks whether this type applies to a given object.
78
   *
79
   * @param mixed $object
80
   *   The object to check against.
81
   * @param mixed $context
82
   *   The execution context.
83
   * @param \GraphQL\Type\Definition\ResolveInfo $info
84
   *   The resolve info object.
85
   *
86
   * @return null|bool
87
   *   TRUE if this type applies to the given object or FALSE if it doesn't.
88
   */
89
  public function applies($object, $context, ResolveInfo $info) {
90
    return NULL;
91
  }
92
93
}
94