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

InputTypePluginBase::getDefinition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
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\Field\Field;
14
use Youshido\GraphQL\Type\TypeInterface;
15
16
abstract class InputTypePluginBase extends PluginBase implements TypeSystemPluginInterface {
17
  use CacheablePluginTrait;
18
  use NamedPluginTrait;
19
  use TypedPluginTrait;
20
21
  /**
22
   * The type instance.
23
   *
24
   * @var \Drupal\graphql\GraphQL\Type\InputObjectType
25
   */
26
  protected $definition;
27
28
  /**
29
   * {@inheritdoc}
30
   */
31
  public function getDefinition(PluggableSchemaBuilderInterface $schemaBuilder) {
32
    if (!isset($this->definition)) {
33
      $this->definition = new InputObjectType($this, $schemaBuilder, [
34
        'name' => $this->buildName(),
35
        'description' => $this->buildDescription(),
36
        'fields' => $this->buildFields($schemaBuilder),
37
      ]);
38
    }
39
40
    return $this->definition;
41
  }
42
43
  /**
44
   * Build the field list.
45
   *
46
   * @param \Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface $schemaBuilder
47
   *   Instance of the schema manager to resolve dependencies.
48
   *
49
   * @return \Youshido\GraphQL\Field\FieldInterface[]
50
   *   The list of fields.
51
   */
52
  protected function buildFields(PluggableSchemaBuilderInterface $schemaBuilder) {
53
    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...
54
      $definition = $this->getPluginDefinition();
55
      if (!$definition['fields']) {
56
        return [];
57
      }
58
59
      $arguments = [];
60
      foreach ($definition['fields'] as $name => $argument) {
61
        $type = $this->buildFieldType($schemaBuilder, $argument);
62
63
        if ($type instanceof TypeInterface) {
64
          $config = [
65
            'name' => $name,
66
            'type' => $type,
67
          ];
68
69
          if (is_array($argument) && isset($argument['default'])) {
70
            $config['defaultValue'] = $argument['default'];
71
          }
72
73
          $arguments[$name] = new Field($config);
74
        }
75
      }
76
77
      return $arguments;
78
    }
79
80
    return [];
81
  }
82
83
  /**
84
   * Build the field type.
85
   *
86
   * @param \Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface $schemaBuilder
87
   *   Instance of the schema manager to resolve dependencies.
88
   * @param array|string $field
89
   *   The field definition array or type name.
90
   *
91
   * @return \Youshido\GraphQL\Type\TypeInterface
92
   *   The type object.
93
   */
94 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...
95
    $type = is_array($field) ? $field['type'] : $field;
96
    return $this->parseType($type, function ($type) use ($schemaBuilder) {
97
      return $schemaBuilder->findByDataTypeOrName($type, [
98
        GRAPHQL_INPUT_TYPE_PLUGIN,
99
        GRAPHQL_SCALAR_PLUGIN,
100
        GRAPHQL_ENUM_PLUGIN,
101
      ])->getDefinition($schemaBuilder);
102
    });
103
  }
104
}
105