Completed
Push — 8.x-3.x ( 160a7b...1d36af )
by Philipp
02:38
created

TypeSystemPluginReferenceTrait::isValidValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\Plugin\GraphQL;
4
5
use Drupal\Component\Plugin\ConfigurablePluginInterface;
6
use Drupal\graphql\GraphQL\Schema\Schema;
7
use Youshido\GraphQL\Execution\ResolveInfo;
8
9
trait TypeSystemPluginReferenceTrait {
10
11
  /**
12
   * The associated type system plugin.
13
   *
14
   * @var \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface
15
   */
16
  protected $plugin;
17
18
  /**
19
   * Retrieves the corresponding plugin instance from the resolve info.
20
   *
21
   * @param \Youshido\GraphQL\Execution\ResolveInfo $info
22
   *   The resolve info object.
23
   *
24
   * @return \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface|null
25
   *   The corresponding plugin instance for this edge.
26
   */
27
  protected function getPluginFromResolveInfo(ResolveInfo $info) {
28
    if (is_array($this->plugin)) {
29
      $schema = isset($info) ? $info->getExecutionContext()->getSchema() : NULL;
30
      if (!$schema instanceof Schema) {
31
        return NULL;
32
      }
33
34
      $schemaPlugin = $schema->getSchemaPlugin();
35
      if (!$schemaPlugin instanceof PluggableSchemaPluginInterface) {
36
        return NULL;
37
      }
38
39
      $typePlugin = $this->getPlugin($schemaPlugin->getSchemaBuilder());
40
      if (!$typePlugin instanceof TypeSystemPluginInterface) {
41
        return NULL;
42
      }
43
44
      return $typePlugin;
45
    }
46
47
    if ($this->plugin instanceof TypeSystemPluginInterface) {
48
      return $this->plugin;
49
    }
50
51
    return NULL;
52
  }
53
54
  /**
55
   * {@inheritdoc}
56
   */
57
  public function getPlugin(PluggableSchemaBuilderInterface $schemaBuilder) {
58
    if (is_array($this->plugin)) {
59
      $this->plugin = $schemaBuilder->getInstance(
60
        $this->plugin['type'],
61
        $this->plugin['id'],
62
        $this->plugin['configuration']
63
      );
64
    }
65
66
    if ($this->plugin instanceof TypeSystemPluginInterface) {
67
      return $this->plugin;
68
    }
69
70
    return NULL;
71
  }
72
73
  /**
74
   * {@inheritdoc}
75
   */
76
  public function __sleep() {
77
    // Instead of serializing the referenced plugin, we just serialize the
78
    // plugin id and configuration.
79
    $this->plugin = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('id' => $this->plu...figuration() : array()) of type array<string,?,{"id":"?"...","configuration":"?"}> is incompatible with the declared type object<Drupal\graphql\Pl...eSystemPluginInterface> of property $plugin.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
80
      'id' => $this->plugin->getPluginId(),
81
      'type' => $this->plugin->getPluginDefinition()['pluginType'],
82
      'configuration' => $this->plugin instanceof ConfigurablePluginInterface ? $this->plugin->getConfiguration() : [],
0 ignored issues
show
Bug introduced by
The class Drupal\Component\Plugin\...igurablePluginInterface 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...
83
    ];
84
85
    return array_keys(get_object_vars($this));
86
  }
87
88
  /**
89
   * {@inheritdoc}
90
   */
91
  public function isValidValue($value) {
92
    // TODO: Don't rely on a pre-initialized plugin.
93
    if ($this->plugin instanceof TypeValidationInterface) {
94
      return $this->plugin->isValidValue($value);
95
    }
96
    return parent::isValidValue($value);
97
  }
98
99
}