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

TypedPluginTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 95
rs 10
c 0
b 0
f 0
wmc 12
lcom 0
cbo 4

4 Methods

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