Completed
Pull Request — 8.x-3.x (#442)
by Sebastian
02:26
created

InterfaceType   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 17.54 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 10
loc 57
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A registerType() 0 3 1
A resolveType() 10 10 3
A build() 0 3 1

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\Type;
4
5
use Drupal\graphql\Plugin\GraphQL\Interfaces\InterfacePluginBase;
6
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginReferenceInterface;
7
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginReferenceTrait;
8
use Youshido\GraphQL\Config\Object\InterfaceTypeConfig;
9
use Youshido\GraphQL\Execution\ResolveInfo;
10
use Youshido\GraphQL\Type\InterfaceType\AbstractInterfaceType;
11
12
class InterfaceType extends AbstractInterfaceType implements TypeSystemPluginReferenceInterface {
13
  use TypeSystemPluginReferenceTrait;
14
15
  /**
16
   * List of types implementing this interface.
17
   *
18
   * @var \Drupal\graphql\GraphQL\Type\ObjectType[]
19
   */
20
  protected $types;
21
22
  /**
23
   * The associated type system plugin.
24
   *
25
   * @var \Drupal\graphql\Plugin\GraphQL\Interfaces\InterfacePluginBase
26
   */
27
  protected $plugin;
28
29
  /**
30
   * {@inheritdoc}
31
   */
32
  public function __construct(InterfacePluginBase $plugin, array $config = []) {
33
    $this->plugin = $plugin;
34
    $this->config = new InterfaceTypeConfig($config, $this, TRUE);
35
  }
36
37
  /**
38
   * Registers a type that implements this interface.
39
   *
40
   * @param \Drupal\graphql\GraphQL\Type\ObjectType $type
41
   *   The type to register on this interface.
42
   */
43
  public function registerType(ObjectType $type) {
44
    $this->types[$type->getName()] = $type;
45
  }
46
47
  /**
48
   * {@inheritdoc}
49
   */
50 View Code Duplication
  public function resolveType($object, ResolveInfo $info = NULL) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
51
    /** @var \Drupal\graphql\GraphQL\Type\ObjectType $type */
52
    foreach ($this->types as $type) {
53
      if ($type->applies($object, $info)) {
0 ignored issues
show
Unused Code introduced by
The call to ObjectType::applies() has too many arguments starting with $info.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
54
        return $type;
55
      }
56
    }
57
58
    throw new \Exception(sprintf('Could not resolve type for interface %s.', $this->getName()));
59
  }
60
61
  /**
62
   * {@inheritdoc}
63
   */
64
  public function build($config) {
65
    // Nothing to do here.
66
  }
67
68
}
69