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

ArgumentAwarePluginTrait::buildArgumentType()   B

Complexity

Conditions 7
Paths 34

Size

Total Lines 19
Code Lines 13

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 34
nop 2
dl 19
loc 19
rs 8.2222
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\Plugin\GraphQL\Traits;
4
5
use Drupal\Component\Plugin\PluginInspectionInterface;
6
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface;
7
use Youshido\GraphQL\Field\InputField;
8
use Youshido\GraphQL\Type\TypeInterface;
9
10
/**
11
 * Methods for argument aware plugins.
12
 */
13
trait ArgumentAwarePluginTrait {
14
  use TypedPluginTrait;
15
16
  /**
17
   * Build the arguments list.
18
   *
19
   * @param \Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface $schemaBuilder
20
   *   Instance of the schema manager to resolve dependencies.
21
   *
22
   * @return \Youshido\GraphQL\Field\InputFieldInterface[]
23
   *   The list of arguments.
24
   */
25 View Code Duplication
  protected function buildArguments(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...
26
    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...
27
      $definition = $this->getPluginDefinition();
28
      if (!$definition['arguments']) {
29
        return [];
30
      }
31
32
      $arguments = [];
33
      foreach ($definition['arguments'] as $name => $argument) {
34
        $type = $this->buildArgumentType($schemaBuilder, $argument);
35
36
        if ($type instanceof TypeInterface) {
37
          $config = [
38
            'name' => $name,
39
            'type' => $type,
40
          ];
41
42
          if (is_array($argument) && isset($argument['default'])) {
43
            $config['defaultValue'] = $argument['default'];
44
          }
45
46
          $arguments[$name] = new InputField($config);
47
        }
48
      }
49
50
      return $arguments;
51
    }
52
53
    return [];
54
  }
55
56
  /**
57
   * Build the argument type.
58
   *
59
   * @param \Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface $schemaBuilder
60
   *   Instance of the schema manager to resolve dependencies.
61
   * @param array|string $argument
62
   *   The argument definition array or type name.
63
   *
64
   * @return \Youshido\GraphQL\Type\TypeInterface
65
   *   The type object.
66
   */
67 View Code Duplication
  protected function buildArgumentType(PluggableSchemaBuilderInterface $schemaBuilder, $argument) {
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...
68
    $type = is_array($argument) ? $argument['type'] : $argument;
69
    $type = $this->parseType($type, function ($type) use ($schemaBuilder) {
70
      return $schemaBuilder->findByName($type, [
71
        GRAPHQL_INPUT_TYPE_PLUGIN,
72
        GRAPHQL_SCALAR_PLUGIN,
73
        GRAPHQL_ENUM_PLUGIN,
74
      ])->getDefinition($schemaBuilder);
75
    });
76
77
    if ($type instanceof TypeInterface) {
78
      $nullable = is_array($argument) && array_key_exists('nullable', $argument) ? (bool) $argument['nullable'] : TRUE;
79
      $multi = is_array($argument) && array_key_exists('multi', $argument) ? (bool) $argument['multi'] : FALSE;
80
81
      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...
82
    }
83
84
    return NULL;
85
  }
86
87
}
88