Completed
Push — 8.x-3.x ( 0334d2...6b3e90 )
by Sebastian
04:15
created

TypedPluginTrait::parseType()   D

Complexity

Conditions 9
Paths 11

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 17
nc 11
nop 2
dl 0
loc 29
rs 4.909
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\Type\ListType\ListType;
8
use Youshido\GraphQL\Type\NonNullType;
9
use Youshido\GraphQL\Type\TypeInterface;
10
11
trait TypedPluginTrait {
12
13
  /**
14
   * {@inheritdoc}
15
   */
16
  abstract public function getPluginDefinition();
17
18
  /**
19
   * Build the plugin type.
20
   *
21
   * @param \Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface $schemaBuilder
22
   *   Instance of the schema manager to resolve dependencies.
23
   *
24
   * @return \Youshido\GraphQL\Type\TypeInterface|null
25
   *   The type object.
26
   */
27
  protected function buildType(PluggableSchemaBuilderInterface $schemaBuilder) {
28
    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...
29
      $definition = $this->getPluginDefinition();
30
      return $this->parseType($definition['type'], function ($type) use ($schemaBuilder) {
31
        $type = $schemaBuilder->findByDataTypeOrName($type, [
32
          GRAPHQL_SCALAR_PLUGIN,
33
          GRAPHQL_UNION_TYPE_PLUGIN,
34
          GRAPHQL_TYPE_PLUGIN,
35
          GRAPHQL_INTERFACE_PLUGIN,
36
          GRAPHQL_ENUM_PLUGIN,
37
        ]);
38
39
        return !empty($type) ? $type->getDefinition($schemaBuilder) : NULL;
40
      });
41
    }
42
43
    return NULL;
44
  }
45
46
  /**
47
   * Parses a type definition from a string and properly decorates it.
48
   *
49
   * Converts type strings (e.g. [Foo!]) to their object representations.
50
   *
51
   * @param string $type
52
   *   The type string to parse.
53
   * @param callable $callback
54
   *   A callback for retrieving the concrete type object from the extracted
55
   *   type name.
56
   *
57
   * @return \Youshido\GraphQL\Type\TypeInterface
58
   *   The extracted type with the type decorators applied.
59
   */
60
  protected function parseType($type, callable $callback) {
61
    $decorators = [];
62
    $unwrapped = $type;
63
    $matches = NULL;
64
65
    while (preg_match('/[\[\]!]/', $unwrapped) && preg_match_all('/^(\[?)(.*?)(\]?)(!*?)$/', $unwrapped, $matches)) {
66
      if ($unwrapped === $matches[2][0] || empty($matches[1][0]) !== empty($matches[3][0])) {
67
        throw new \InvalidArgumentException(sprintf("Invalid type declaration '%s'.", $type));
68
      }
69
70
      if (!empty($matches[4][0])) {
71
        array_unshift($decorators, [$this, 'nonNullType']);
72
      }
73
74
      if (!empty($matches[1][0]) && !empty($matches[3][0])) {
75
        array_unshift($decorators, [$this, 'listType']);
76
      }
77
78
      $unwrapped = $matches[2][0];
79
    }
80
81
    if ($type = $callback($unwrapped)) {
82
      return array_reduce($decorators, function ($carry, $current) {
83
        return $current($carry);
84
      }, $type);
85
    }
86
87
    return NULL;
88
  }
89
90
  /**
91
   * Declares a type as non-nullable.
92
   *
93
   * @param \Youshido\GraphQL\Type\TypeInterface $type
94
   *   The type to wrap.
95
   *
96
   * @return \Youshido\GraphQL\Type\NonNullType
97
   *   The wrapped type.
98
   */
99
  protected function nonNullType(TypeInterface $type) {
100
    return new NonNullType($type);
0 ignored issues
show
Documentation introduced by
$type is of type object<Youshido\GraphQL\Type\TypeInterface>, but the function expects a object<Youshido\GraphQL\Type\AbstractType>|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
101
  }
102
103
  /**
104
   * Declares a type as a list.
105
   *
106
   * @param \Youshido\GraphQL\Type\TypeInterface $type
107
   *   The type to wrap.
108
   *
109
   * @return \Youshido\GraphQL\Type\ListType\ListType
110
   *   The wrapped type.
111
   */
112
  protected function listType(TypeInterface $type) {
113
    return new ListType($type);
114
  }
115
116
}
117