Completed
Push — 8.x-3.x ( 58a9b7...900988 )
by Sebastian
06:57 queued 03:40
created

CacheContextsCollector   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 19.57 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 9
loc 46
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getVisitor() 9 23 5
A collectCacheContexts() 0 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Drupal\graphql\GraphQL\Visitors;
4
5
use Drupal\Core\Cache\Cache;
6
use GraphQL\Language\AST\FieldNode;
7
use GraphQL\Language\AST\NodeKind;
8
use GraphQL\Type\Definition\LeafType;
9
use GraphQL\Utils\TypeInfo;
10
11
class CacheContextsCollector {
12
13
  /**
14
   * {@inheritdoc}
15
   */
16
  public function getVisitor(TypeInfo $info, array &$contexts) {
17
    return [
18
      NodeKind::FIELD => [
19
        'leave' => function (FieldNode $field) use ($info, &$contexts) {
0 ignored issues
show
Unused Code introduced by
The parameter $field 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...
20
          $definition = $info->getFieldDef();
21 View Code Duplication
          if (!empty($definition->config['contexts'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
            $contexts = Cache::mergeContexts($contexts, $this->collectCacheContexts($definition->config['contexts']));
23
          }
24
25
          $parent = $info->getParentType();
26 View Code Duplication
          if (!empty($parent->config['contexts'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
            $contexts = Cache::mergeContexts($contexts, $this->collectCacheContexts($parent->config['contexts']));
28
          }
29
30
          $type = $info->getType();
31
          // Collect cache metadata from leaf types.
32 View Code Duplication
          if ($type instanceof LeafType && !empty($type->config['contexts'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
            $contexts = Cache::mergeContexts($contexts, $this->collectCacheContexts($type->config['contexts']));
34
          }
35
        },
36
      ],
37
    ];
38
  }
39
40
  /**
41
   * Collects the cache contexts from a type or field config.
42
   *
43
   * @param array|callable $contexts
44
   *   The cache contexts array or a callable to return cache contexts.
45
   *
46
   * @return array
47
   *   The collected cache contexts.
48
   */
49
  protected function collectCacheContexts($contexts) {
50
    if (is_callable($contexts)) {
51
      return $contexts();
52
    }
53
54
    return $contexts;
55
  }
56
}