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

EntityById   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A create() 8 8 1
A resolveValues() 6 6 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_core\Plugin\GraphQL\Fields\Entity;
4
5
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
6
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7
use Drupal\graphql\GraphQL\Batching\Buffers\EntityBuffer;
8
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
use Youshido\GraphQL\Execution\ResolveInfo;
11
12
/**
13
 * Retrieve an entity by its id.
14
 *
15
 * @GraphQLField(
16
 *   id = "entity_by_id",
17
 *   secure = true,
18
 *   name = "entityById",
19
 *   nullable = true,
20
 *   multi = false,
21
 *   weight = -1,
22
 *   arguments = {
23
 *     "id" = "String"
24
 *   },
25
 *   deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityByIdDeriver"
26
 * )
27
 */
28 View Code Duplication
class EntityById extends FieldPluginBase implements ContainerFactoryPluginInterface {
0 ignored issues
show
Bug introduced by
There is one abstract method getPluginDefinition in this class; you could implement it, or declare this class as abstract.
Loading history...
Duplication introduced by
This class 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...
29
30
  use DependencySerializationTrait;
31
32
  /**
33
   * @var \Drupal\graphql\GraphQL\Batching\Buffers\EntityBuffer
34
   */
35
  protected $entityBuffer;
36
37
  /**
38
   * {@inheritdoc}
39
   */
40
  public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityBuffer $entityBuffer) {
41
    parent::__construct($configuration, $pluginId, $pluginDefinition);
42
    $this->entityBuffer = $entityBuffer;
43
  }
44
45
  /**
46
   * {@inheritdoc}
47
   */
48
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
49
    return new static(
50
      $configuration,
51
      $pluginId,
52
      $pluginDefinition,
53
      $container->get('graphql.buffer.entity')
54
    );
55
  }
56
57
  protected function resolveValues($value, array $args, ResolveInfo $info) {
58
    $resolver = $this->entityBuffer->add($this->getPluginDefinition()['entity_type'], $args['id']);
59
    return function ($value, array $args, ResolveInfo $info) use ($resolver) {
0 ignored issues
show
Bug Best Practice introduced by
The return type of return function ($value,...(yield $resolver()); }; (Closure) is incompatible with the return type of the parent method Drupal\graphql\Plugin\Gr...uginBase::resolveValues of type Generator.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
Unused Code introduced by
The parameter $value 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...
Unused Code introduced by
The parameter $args 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...
Unused Code introduced by
The parameter $info 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
      yield $resolver();
61
    };
62
  }
63
64
}
65