Completed
Pull Request — 8.x-3.x (#490)
by Sebastian
06:18 queued 41s
created

TypedPluginTrait::nonNullType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
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
/**
12
 * Methods for GraphQL plugins that have a type.
13
 */
14
trait TypedPluginTrait {
15
16
  /**
17
   * Add information about cardinality and nullability.
18
   *
19
   * @param \Youshido\GraphQL\Type\TypeInterface $type
20
   *   The initial type object.
21
   * @param bool $nullable
22
   *   Indicates if the type can be null.
23
   * @param bool $multi
24
   *   Indicates if the type is a list.
25
   *
26
   * @return \Youshido\GraphQL\Type\TypeInterface
27
   *   The decorated type
28
   *
29
   * @deprecated
30
   *   Will be removed before the 1.0 release. Use the type string syntax
31
   *   (e.g. [Foo!]) instead.
32
   */
33
  public function decorateType(TypeInterface $type, $nullable, $multi) {
34
    if (!empty($multi)) {
35
      $type = new ListType($type);
36
    }
37
38
    if (empty($nullable)) {
39
      $type = 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...
40
    }
41
42
    return $type;
43
  }
44
45
  /**
46
   * Build the plugin type.
47
   *
48
   * @param \Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface $schemaBuilder
49
   *   Instance of the schema manager to resolve dependencies.
50
   *
51
   * @return \Youshido\GraphQL\Type\TypeInterface
52
   *   The type object.
53
   */
54
  protected function buildType(PluggableSchemaBuilderInterface $schemaBuilder) {
55
    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...
56
      $definition = $this->getPluginDefinition();
57
      $type = $this->parseType($definition['type'], function ($type) use ($schemaBuilder) {
58
        return $schemaBuilder->findByDataTypeOrName($type, [
59
          GRAPHQL_SCALAR_PLUGIN,
60
          GRAPHQL_UNION_TYPE_PLUGIN,
61
          GRAPHQL_TYPE_PLUGIN,
62
          GRAPHQL_INTERFACE_PLUGIN,
63
          GRAPHQL_ENUM_PLUGIN,
64
        ])->getDefinition($schemaBuilder);
65
      });
66
67
      if ($type instanceof TypeInterface) {
68
        return $this->decorateType($type, $definition['nullable'], $definition['multi']);
69
      }
70
    }
71
72
    return NULL;
73
  }
74
75
  /**
76
   * Parses a type definition from a string and properly decorates it.
77
   *
78
   * Converts type strings (e.g. [Foo!]) to their object representations.
79
   *
80
   * @param string $type
81
   *   The type string to parse.
82
   * @param callable $callback
83
   *   A callback for retrieving the concrete type object from the extracted
84
   *   type name.
85
   *
86
   * @return \Youshido\GraphQL\Type\TypeInterface
87
   *   The extracted type with the type decorators applied.
88
   */
89
  protected function parseType($type, callable $callback) {
90
    $decorators = [];
91
    $unwrapped = $type;
92
    $matches = NULL;
93
94
    while (preg_match('/[\[\]!]/', $unwrapped) && preg_match_all('/^(\[?)(.*?)(\]?)(!*?)$/', $unwrapped, $matches)) {
95
      if ($unwrapped === $matches[2][0] || empty($matches[1][0]) !== empty($matches[3][0])) {
96
        throw new \InvalidArgumentException(sprintf("Invalid type declaration '%s'.", $type));
97
      }
98
99
      if (!empty($matches[4][0])) {
100
        array_push($decorators, [$this, 'nonNullType']);
101
      }
102
103
      if (!empty($matches[1][0]) && !empty($matches[3][0])) {
104
        array_push($decorators, [$this, 'listType']);
105
      }
106
107
      $unwrapped = $matches[2][0];
108
    }
109
110
    return array_reduce($decorators, function ($carry, $current) {
111
      return $current($carry);
112
    }, $callback($unwrapped));
113
  }
114
115
  /**
116
   * Declares a type as non-nullable.
117
   *
118
   * @param \Youshido\GraphQL\Type\TypeInterface $type
119
   *   The type to wrap.
120
   *
121
   * @return \Youshido\GraphQL\Type\NonNullType
122
   *   The wrapped type.
123
   */
124
  protected function nonNullType(TypeInterface $type) {
125
    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...
126
  }
127
128
  /**
129
   * Declares a type as a list.
130
   *
131
   * @param \Youshido\GraphQL\Type\TypeInterface $type
132
   *   The type to wrap.
133
   *
134
   * @return \Youshido\GraphQL\Type\ListType\ListType
135
   *   The wrapped type.
136
   */
137
  protected function listType(TypeInterface $type) {
138
    return new ListType($type);
139
  }
140
141
}
142