Completed
Pull Request — 8.x-3.x (#525)
by Philipp
02:33
created

CacheMetadataCollector::isEnabled()   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 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\GraphQL\Visitors;
4
5
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
6
use GraphQL\Language\AST\DocumentNode;
7
use GraphQL\Language\AST\NodeKind;
8
use GraphQL\Language\AST\OperationDefinitionNode;
9
use GraphQL\Language\AST\SelectionSetNode;
10
use GraphQL\Language\Visitor;
11
use GraphQL\Validator\Rules\AbstractQuerySecurity;
12
use GraphQL\Validator\ValidationContext;
13
14
class CacheMetadataCollector extends AbstractQuerySecurity {
15
16
  /**
17
   * @var \Drupal\Core\Cache\RefinableCacheableDependencyInterface
18
   */
19
  protected $metadata;
20
21
  /**
22
   * @var array
23
   */
24
  protected $variables;
25
26
  /**
27
   * CacheMetadataCollector constructor.
28
   *
29
   * @param \Drupal\Core\Cache\RefinableCacheableDependencyInterface $metadata
30
   * @param array|null $variables
31
   */
32
  public function __construct(RefinableCacheableDependencyInterface $metadata, array $variables = NULL) {
33
    $this->metadata = $metadata;
34
    $this->variables = $variables;
0 ignored issues
show
Documentation Bug introduced by
It seems like $variables can be null. However, the property $variables is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

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

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
35
  }
36
37
  /**
38
   * {@inheritdoc}
39
   */
40
  protected function isEnabled() {
41
    return TRUE;
42
  }
43
44
  /**
45
   * {@inheritdoc}
46
   */
47
  public function getVisitor(ValidationContext $context) {
48
    $variableDefs = new \ArrayObject();
49
    $fieldNodeAndDefs = new \ArrayObject();
50
51
    return $this->invokeIfNeeded($context, [
52
      NodeKind::SELECTION_SET => function (SelectionSetNode $selectionSet) use ($context, &$fieldNodeAndDefs) {
53
      $foo = '';
0 ignored issues
show
Unused Code introduced by
$foo 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...
54
        $fieldNodeAndDefs->exchangeArray($this->collectFieldASTsAndDefs(
55
          $context,
56
          $context->getParentType(),
57
          $selectionSet,
58
          null,
59
          $fieldNodeAndDefs
60
        ));
61
      },
62
      NodeKind::VARIABLE_DEFINITION => function ($def) use (&$variableDefs) {
63
        $variableDefs->append($def);
64
        return Visitor::skipNode();
65
      },
66
      NodeKind::DOCUMENT => [
67
        'leave' => function (DocumentNode $document) use ($context, $fieldNodeAndDefs, &$variableDefs) {
0 ignored issues
show
Unused Code introduced by
The parameter $document is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
68
          $errors = $context->getErrors();
69
70
          if (empty($errors)) {
71
            $foo = '';
0 ignored issues
show
Unused Code introduced by
$foo 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...
72
          }
73
        },
74
      ],
75
    ]);
76
  }
77
}