Completed
Push — 8.x-3.x ( e67c0e...2e01b0 )
by Philipp
02:27
created

InputTypePluginBase   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 111
Duplicated Lines 40.54 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 45
loc 111
rs 10
c 0
b 0
f 0
wmc 23
lcom 1
cbo 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefinition() 0 11 2
C buildFields() 30 30 7
C buildFieldType() 15 32 14

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\InputTypes;
4
5
use Drupal\Component\Plugin\PluginBase;
6
use Drupal\Component\Plugin\PluginInspectionInterface;
7
use Drupal\graphql\GraphQL\Type\InputObjectType;
8
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface;
9
use Drupal\graphql\Plugin\GraphQL\Traits\CacheablePluginTrait;
10
use Drupal\graphql\Plugin\GraphQL\Traits\NamedPluginTrait;
11
use Drupal\graphql\Plugin\GraphQL\Traits\TypedPluginTrait;
12
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface;
13
use Youshido\GraphQL\Config\Object\InputObjectTypeConfig;
14
use Youshido\GraphQL\Field\Field;
15
use Youshido\GraphQL\Type\TypeInterface;
16
17
/**
18
 * Base class for input type plugins.
19
 */
20
abstract class InputTypePluginBase extends PluginBase implements TypeSystemPluginInterface {
21
  use CacheablePluginTrait;
22
  use NamedPluginTrait;
23
  use TypedPluginTrait;
24
25
  /**
26
   * The type instance.
27
   *
28
   * @var \Drupal\graphql\GraphQL\Type\InputObjectType
29
   */
30
  protected $definition;
31
32
  /**
33
   * {@inheritdoc}
34
   */
35
  public function getDefinition(PluggableSchemaBuilderInterface $schemaBuilder) {
36
    if (!isset($this->definition)) {
37
      $this->definition = new InputObjectType($this, [
38
        'name' => $this->buildName(),
39
        'description' => $this->buildDescription(),
40
        'fields' => $this->buildFields($schemaBuilder),
41
      ]);
42
    }
43
44
    return $this->definition;
45
  }
46
47
  /**
48
   * Build the field list.
49
   *
50
   * @param \Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface $schemaBuilder
51
   *   Instance of the schema manager to resolve dependencies.
52
   *
53
   * @return \Youshido\GraphQL\Field\FieldInterface[]
54
   *   The list of fields.
55
   */
56 View Code Duplication
  protected function buildFields(PluggableSchemaBuilderInterface $schemaBuilder) {
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...
57
    if ($this instanceof PluginInspectionInterface) {
0 ignored issues
show
Bug introduced by
The class Drupal\Component\Plugin\PluginInspectionInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
58
      $definition = $this->getPluginDefinition();
59
      if (!$definition['fields']) {
60
        return [];
61
      }
62
63
      $arguments = [];
64
      foreach ($definition['fields'] as $name => $argument) {
65
        $type = $this->buildFieldType($schemaBuilder, $argument);
66
67
        if ($type instanceof TypeInterface) {
68
          $config = [
69
            'name' => $name,
70
            'type' => $type,
71
          ];
72
73
          if (is_array($argument) && isset($argument['default'])) {
74
            $config['defaultValue'] = $argument['default'];
75
          }
76
77
          $arguments[$name] = new Field($config);
78
        }
79
      }
80
81
      return $arguments;
82
    }
83
84
    return [];
85
  }
86
87
  /**
88
   * Build the field type.
89
   *
90
   * @param \Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface $schemaBuilder
91
   *   Instance of the schema manager to resolve dependencies.
92
   * @param array|string $field
93
   *   The field definition array or type name.
94
   *
95
   * @return \Youshido\GraphQL\Type\TypeInterface
96
   *   The type object.
97
   */
98
  protected function buildFieldType(PluggableSchemaBuilderInterface $schemaBuilder, $field) {
99
    if (is_array($field) && array_key_exists('data_type', $field) && $field['data_type']) {
100
      $types = array_map(function (TypeSystemPluginInterface $plugin) use ($schemaBuilder) {
101
        return $plugin->getDefinition($schemaBuilder);
102
      }, $schemaBuilder->find(function($definition) use ($field) {
103
        return array_key_exists('data_type', $definition) && $definition['data_type'] === $field['data_type'];
104
      }, [
105
        GRAPHQL_INPUT_TYPE_PLUGIN,
106
        GRAPHQL_SCALAR_PLUGIN,
107
      ]));
108
109
      $type = array_pop($types) ?: $schemaBuilder->findByName('String', [GRAPHQL_SCALAR_PLUGIN])->getDefinition($schemaBuilder);
110
    }
111 View Code Duplication
    else {
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...
112
      $typeInfo = is_array($field) ? $field['type'] : $field;
113
114
      $type = is_array($typeInfo) ? $this->buildEnumConfig($typeInfo, $field['enum_type_name']) : $schemaBuilder->findByName($typeInfo, [
115
        GRAPHQL_INPUT_TYPE_PLUGIN,
116
        GRAPHQL_SCALAR_PLUGIN,
117
        GRAPHQL_ENUM_PLUGIN,
118
      ])->getDefinition($schemaBuilder);
119
    }
120
121 View Code Duplication
    if (isset($type) && $type instanceof TypeInterface) {
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...
122
      $nullable = is_array($field) && array_key_exists('nullable', $field) && $field['nullable'];
123
      $multi = is_array($field) && array_key_exists('multi', $field) && $field['multi'];
124
125
      return $this->decorateType($type, $nullable, $multi);
126
    }
127
128
    return NULL;
129
  }
130
}
131