Completed
Pull Request — 8.x-3.x (#519)
by Sebastian
03:41
created

PluginReferenceTrait::getPluginDefinition()   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 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\GraphQL;
4
5
use Drupal\Component\Plugin\ConfigurablePluginInterface;
6
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface;
7
8
trait PluginReferenceTrait {
9
10
  /**
11
   * The associated type system plugin.
12
   *
13
   * @var \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface
14
   */
15
  protected $plugin;
16
17
  /**
18
   * The schema builder object.
19
   *
20
   * @var \Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface
21
   */
22
  protected $builder;
23
24
  /**
25
   * {@inheritdoc}
26
   */
27
  public function getPlugin() {
28
    if (is_array($this->plugin)) {
29
      $this->plugin = $this->builder->getInstance(
30
        $this->plugin['type'],
31
        $this->plugin['id'],
32
        $this->plugin['configuration']
33
      );
34
    }
35
36
    return $this->plugin;
37
  }
38
39
  /**
40
   * {@inheritdoc}
41
   */
42
  public function getPluginDefinition() {
43
    if (is_array($this->plugin)) {
44
      return $this->plugin['definition'];
45
    }
46
47
    return $this->getPlugin()->getPluginDefinition();
48
  }
49
50
  /**
51
   * {@inheritdoc}
52
   */
53
  public function getPluginId() {
54
    if (is_array($this->plugin)) {
55
      return $this->plugin['id'];
56
    }
57
58
    return $this->getPlugin()->getPluginId();
59
  }
60
61
  /**
62
   * {@inheritdoc}
63
   */
64
  public function __sleep() {
65
    // Instead of serializing the referenced plugin, we just serialize the
66
    // plugin id and configuration.
67
    if ($this->plugin instanceof TypeSystemPluginInterface) {
68
      $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...
69
        'id' => $this->plugin->getPluginId(),
70
        'type' => $this->plugin->getPluginDefinition()['pluginType'],
71
        '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...
72
      ];
73
    }
74
75
    return array_keys(get_object_vars($this));
76
  }
77
78
}