Completed
Pull Request — 8.x-3.x (#519)
by Sebastian
04:42
created

MaxComplexityVisitor   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A visit() 0 11 3
A reduce() 0 3 2
A initial() 0 3 1
1
<?php
2
3
namespace Drupal\graphql\GraphQL\Execution\Visitor;
4
5
use Youshido\GraphQL\Exception\ResolveException;
6
use Youshido\GraphQL\Field\FieldInterface;
7
8
class MaxComplexityVisitor implements VisitorInterface {
9
10
  /**
11
   * @var int
12
   */
13
  protected $defaultCost;
14
15
  /**
16
   * MaxComplexityQueryVisitor constructor.
17
   *
18
   * @param int $maxComplexity
0 ignored issues
show
Bug introduced by
There is no parameter named $maxComplexity. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
19
   *   The allowed maximum complexity.
20
   */
21
  public function __construct($defaultCost = 1) {
22
    $this->defaultCost = $defaultCost;
23
  }
24
25
  /**
26
   * {@inheritdoc}
27
   */
28
  public function visit(array $args, FieldInterface $field, $children) {
29
    /** @var \Youshido\GraphQL\Config\Field\FieldConfig $config */
30
    $config = $field->getConfig();
31
    $cost = $config->get('cost', NULL);
32
33
    if (is_callable($cost)) {
34
      $cost = $cost($args, $field, $children);
35
    }
36
37
    return isset($cost) ? $cost : $this->defaultCost;
38
  }
39
40
  /**
41
   * {@inheritdoc}
42
   */
43
  public function reduce($carry, $current) {
44
    return !empty($current) ? $carry + $current : $carry;
45
  }
46
47
  /**
48
   * {@inheritdoc}
49
   */
50
  public function initial() {
51
    return 0;
52
  }
53
}