Completed
Pull Request — 8.x-3.x (#525)
by Sebastian
05:08
created

ResolveContext::getGlobal()   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 2
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\GraphQL\Execution;
4
5
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
6
use Drupal\Core\Cache\RefinableCacheableDependencyTrait;
7
use GraphQL\Type\Definition\ResolveInfo;
8
9
class ResolveContext implements RefinableCacheableDependencyInterface {
10
  use RefinableCacheableDependencyTrait;
11
12
  /**
13
   * Read-only list of global values.
14
   *
15
   * @var array
16
   */
17
  protected $globals;
18
19
  /**
20
   * @var array
21
   */
22
  protected $contexts = [];
23
24
  /**
25
   * ResolveContext constructor.
26
   *
27
   * @param array $globals
28
   */
29
  public function __construct(array $globals = []) {
30
    $this->globals = $globals;
31
  }
32
33
  /**
34
   * @param $name
35
   * @param \GraphQL\Type\Definition\ResolveInfo $info
36
   * @param null $default
37
   *
38
   * @return mixed|null
39
   */
40
  public function getContext($name, ResolveInfo $info, $default = NULL) {
41
    // TODO: Use operation name in path.
42
    $operation = $info->operation;
0 ignored issues
show
Unused Code introduced by
$operation is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
43
    $path = $info->path;
44
45
    do {
46
      $key = implode('.', $path);
47
      if (isset($this->contexts[$key][$name])) {
48
        return $this->contexts[$key][$name];
49
      }
50
    } while (array_pop($path) && !empty($path));
51
52
    return $default;
53
  }
54
55
  /**
56
   * @param $name
57
   * @param $value
58
   * @param \GraphQL\Type\Definition\ResolveInfo $info
59
   *
60
   * @return $this
61
   */
62
  public function setContext($name, $value, ResolveInfo $info) {
63
    // TODO: Use operation name in path.
64
    $operation = $info->operation;
0 ignored issues
show
Unused Code introduced by
$operation is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
65
    $key = implode('.', $info->path);
66
    $this->contexts[$key][$name] = $value;
67
68
    return $this;
69
  }
70
71
  /**
72
   * @param $name
73
   * @param null $default
74
   *
75
   * @return mixed|null
76
   */
77
  public function getGlobal($name, $default = NULL) {
78
    if (isset($this->globals[$name])) {
79
      return $this->globals[$name];
80
    }
81
82
    return $default;
83
  }
84
85
}