Completed
Pull Request — 8.x-3.x (#401)
by Sebastian
02:54
created

PluggableSchemaManagerFactory::addPluginManager()   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;
4
5
use Drupal\Component\Plugin\PluginManagerInterface;
6
7
class PluggableSchemaManagerFactory {
8
9
  /**
10
   * Static cache of created schema managers.
11
   *
12
   * @var \Drupal\graphql\Plugin\GraphQL\PluggableSchemaManagerInterface[]
13
   */
14
  protected $schemaManagers = [];
15
16
  /**
17
   * Plugin managers, used to collect GraphQL schema plugins.
18
   *
19
   * @var \Drupal\Component\Plugin\PluginManagerInterface[]
20
   */
21
  protected $pluginManagers = [];
22
23
  /**
24
   * Add a plugin manager.
25
   *
26
   * @param \Drupal\Component\Plugin\PluginManagerInterface $pluginManager
27
   *   The plugin manager to attach.
28
   */
29
  public function addPluginManager(PluginManagerInterface $pluginManager) {
30
    $this->pluginManagers[] = $pluginManager;
31
  }
32
33
  /**
34
   * Add a plugin manager.
35
   *
36
   * @param string $schema
37
   *   The name of the schema to fetch the schema manager for.
38
   *
39
   * @return \Drupal\graphql\Plugin\GraphQL\PluggableSchemaManagerInterface
40
   *   The dedicated pluggable schema manager instance for the given schema.
41
   */
42
  public function getSchemaManager($schema) {
43
    if (!isset($schemaManagers[$schema])) {
0 ignored issues
show
Bug introduced by
The variable $schemaManagers seems only to be defined at a later point. As such the call to isset() seems to always evaluate to false.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
44
      $schemaManagers[$schema] = new PluggableSchemaManager($this->pluginManagers);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$schemaManagers was never initialized. Although not strictly required by PHP, it is generally a good practice to add $schemaManagers = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
45
    }
46
47
    return $schemaManagers[$schema];
48
  }
49
50
}
51