Completed
Pull Request — 8.x-3.x (#525)
by Philipp
02:10
created

EnumPluginBase   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 15.25 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createInstance() 0 3 1
A getDefinition() 9 9 1
A buildEnumValues() 0 8 1
A buildEnumValue() 0 3 2
A buildEnumDescription() 0 3 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\Plugin\GraphQL\Enums;
4
5
use Drupal\Component\Plugin\PluginBase;
6
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilder;
7
use Drupal\graphql\Plugin\GraphQL\Traits\CacheablePluginTrait;
8
use Drupal\graphql\Plugin\GraphQL\Traits\DescribablePluginTrait;
9
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface;
10
use GraphQL\Type\Definition\EnumType;
11
12
abstract class EnumPluginBase extends PluginBase implements TypeSystemPluginInterface {
13
  use DescribablePluginTrait;
14
  use CacheablePluginTrait;
15
16
  /**
17
   * {@inheritdoc}
18
   */
19
  public static function createInstance(PluggableSchemaBuilder $builder, $definition, $id) {
20
    return new EnumType($definition);
21
  }
22
23
  /**
24
   * {@inheritdoc}
25
   */
26 View Code Duplication
  public function getDefinition() {
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...
27
    $definition = $this->getPluginDefinition();
28
29
    return [
30
      'name' => $definition['name'],
31
      'description' => $this->buildDescription($definition),
32
      'values' => $this->buildEnumValues($definition),
33
    ];
34
  }
35
36
  /**
37
   * @param $definition
38
   *
39
   * @return array
40
   */
41
  protected function buildEnumValues($definition) {
42
    return array_map(function ($value) use ($definition) {
43
      return [
44
        'value' => $this->buildEnumValue($value, $definition),
45
        'description' => $this->buildEnumDescription($value, $definition),
46
      ];
47
    }, $definition['values']);
48
  }
49
50
  /**
51
   * @param $value
52
   * @param $definition
53
   *
54
   * @return mixed
55
   */
56
  protected function buildEnumValue($value, $definition) {
57
    return is_array($value) ? $value['value'] : $value;
58
  }
59
60
  /**
61
   * @param $value
62
   * @param $definition
63
   *
64
   * @return string
65
   */
66
  protected function buildEnumDescription($value, $definition) {
67
    return (string) (is_array($value) ? $value['description'] : '');
68
  }
69
70
}
71