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

CacheMetadataCollector   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isEnabled() 0 3 1
B getVisitor() 0 25 1
1
<?php
2
3
namespace Drupal\graphql\GraphQL\Visitors;
4
5
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
6
use GraphQL\Language\AST\NodeKind;
7
use GraphQL\Language\AST\SelectionSetNode;
8
use GraphQL\Language\Visitor;
9
use GraphQL\Validator\Rules\AbstractQuerySecurity;
10
use GraphQL\Validator\ValidationContext;
11
12
class CacheMetadataCollector extends AbstractQuerySecurity {
13
14
  /**
15
   * @var \Drupal\Core\Cache\RefinableCacheableDependencyInterface
16
   */
17
  protected $metadata;
18
19
  /**
20
   * @var array
21
   */
22
  protected $variables;
23
24
  /**
25
   * CacheMetadataCollector constructor.
26
   *
27
   * @param \Drupal\Core\Cache\RefinableCacheableDependencyInterface $metadata
28
   * @param array|null $variables
29
   */
30
  public function __construct(RefinableCacheableDependencyInterface $metadata, array $variables = NULL) {
31
    $this->metadata = $metadata;
32
    $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...
33
  }
34
35
  /**
36
   * {@inheritdoc}
37
   */
38
  protected function isEnabled() {
39
    return TRUE;
40
  }
41
42
  /**
43
   * {@inheritdoc}
44
   */
45
  public function getVisitor(ValidationContext $context) {
46
    $this->variables = new \ArrayObject();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \ArrayObject() of type object<ArrayObject> is incompatible with the declared type array of property $variables.

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...
47
    $this->structure = new \ArrayObject();
0 ignored issues
show
Bug introduced by
The property structure does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
48
49
    return $this->invokeIfNeeded($context, [
50
      NodeKind::SELECTION_SET => function (SelectionSetNode $selectionSet) use ($context) {
51
        $this->structure = $this->collectFieldASTsAndDefs(
52
          $context,
53
          $context->getParentType(),
54
          $selectionSet,
55
          NULL,
56
          $this->structure
57
        );
58
      },
59
      NodeKind::VARIABLE_DEFINITION => function ($definition, &$variables) {
0 ignored issues
show
Unused Code introduced by
The parameter $variables 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...
60
        array_push( $this->variables, $definition);
61
        return Visitor::skipNode();
62
      },
63
//      NodeKind::OPERATION_DEFINITION => [
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
64
//        'leave' => function () {
65
//          $foo = '';
66
//        },
67
//      ],
68
    ]);
69
  }
70
}