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

InputTypePluginBase   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 98
Duplicated Lines 50 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 49
loc 98
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefinition() 0 11 2
C buildFields() 30 30 7
B buildFieldType() 19 19 7

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, $schemaBuilder, [
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 View Code Duplication
  protected function buildFieldType(PluggableSchemaBuilderInterface $schemaBuilder, $field) {
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...
99
    $type = is_array($field) ? $field['type'] : $field;
100
    $type = $this->parseType($type, function ($type) use ($schemaBuilder) {
101
      return $schemaBuilder->findByName($type, [
102
        GRAPHQL_INPUT_TYPE_PLUGIN,
103
        GRAPHQL_SCALAR_PLUGIN,
104
        GRAPHQL_ENUM_PLUGIN,
105
      ])->getDefinition($schemaBuilder);
106
    });
107
108
    if ($type instanceof TypeInterface) {
109
      $nullable = is_array($field) && array_key_exists('nullable', $field) ? (bool) $field['nullable'] : TRUE;
110
      $multi = is_array($field) && array_key_exists('multi', $field) ? (bool) $field['multi'] : FALSE;
111
112
      return $this->decorateType($type, $nullable, $multi);
0 ignored issues
show
Deprecated Code introduced by
The method Drupal\graphql\Plugin\Gr...inTrait::decorateType() has been deprecated with message: Will be removed before the 1.0 release. Use the type string syntax (e.g. [Foo!]) instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
113
    }
114
115
    return NULL;
116
  }
117
}
118