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

InputTypePluginBase   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 93
Duplicated Lines 9.68 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 9
loc 93
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 5

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createInstance() 0 9 1
A getDefinition() 9 9 1
A buildFields() 0 9 1
A buildFieldType() 0 4 2
A buildFieldDescription() 0 3 2
A buildFieldDefault() 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\InputTypes;
4
5
use Drupal\Component\Plugin\PluginBase;
6
use Drupal\graphql\Plugin\GraphQL\Traits\DescribablePluginTrait;
7
use Drupal\graphql\Plugin\GraphQL\Traits\TypedPluginTrait;
8
use Drupal\graphql\Plugin\SchemaBuilderInterface;
9
use Drupal\graphql\Plugin\TypePluginInterface;
10
use Drupal\graphql\Plugin\TypePluginManager;
11
use Drupal\graphql\Utility\StringHelper;
12
use GraphQL\Type\Definition\InputObjectType;
13
14
abstract class InputTypePluginBase extends PluginBase implements TypePluginInterface {
15
  use DescribablePluginTrait;
16
  use TypedPluginTrait;
17
18
  /**
19
   * {@inheritdoc}
20
   */
21
  public static function createInstance(SchemaBuilderInterface $builder, TypePluginManager $manager, $definition, $id) {
22
    return new InputObjectType([
23
      'name' => $definition['name'],
24
      'description' => $definition['description'],
25
      'fields' => function () use ($builder, $definition) {
26
        return $builder->processArguments($definition['fields']);
27
      },
28
    ]);
29
  }
30
31
  /**
32
   * {@inheritdoc}
33
   */
34 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...
35
    $definition = $this->getPluginDefinition();
36
37
    return [
38
      'name' => $definition['name'],
39
      'description' => $this->buildDescription($definition),
40
      'fields' => $this->buildFields($definition),
41
    ];
42
  }
43
44
  /**
45
   * Builds the fields of the type definition.
46
   *
47
   * @param $definition
48
   *   The plugin definition array.
49
   *
50
   * @return array
51
   *   The list of fields for the input type.
52
   */
53
  protected function buildFields($definition) {
54
    return array_map(function ($field) use ($definition) {
55
      return [
56
        'type' => $this->buildFieldType($field, $definition),
57
        'description' => $this->buildFieldDescription($field, $definition),
58
        'default' => $this->buildFieldDefault($field, $definition),
59
      ];
60
    }, $definition['fields']);
61
  }
62
63
  /**
64
   * Builds a field's type.
65
   *
66
   * @param array $field
67
   *   The field definition array.
68
   *
69
   * @return array
70
   *   The parsed type definition array.
71
   */
72
  protected function buildFieldType($field) {
73
    $type = is_array($field) ? $field['type'] : $field;
74
    return StringHelper::parseType($type);
75
  }
76
77
  /**
78
   * Builds a field's description.
79
   *
80
   * @param array $field
81
   *   The field definition array.
82
   * @param array $definition
83
   *   The plugin definition array.
84
   *
85
   * @return string
86
   *   The field's description.
87
   */
88
  protected function buildFieldDescription($field, $definition) {
89
    return (string) (isset($field['description']) ? $field['description'] : '');
90
  }
91
92
  /**
93
   * Builds a field's default value.
94
   *
95
   * @param array $field
96
   *   The field definition array.
97
   * @param array $definition
98
   *   The plugin definition array.
99
   *
100
   * @return mixed
101
   *   The field's default value.
102
   */
103
  protected function buildFieldDefault($field, $definition) {
104
    return isset($field['default']) ? $field['default'] : NULL;
105
  }
106
}
107