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

InputTypePluginBase   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 12 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 9
loc 75
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
   * @param $definition
46
   *
47
   * @return array
48
   */
49
  protected function buildFields($definition) {
50
    return array_map(function ($field) use ($definition) {
51
      return [
52
        'type' => $this->buildFieldType($field, $definition),
53
        'description' => $this->buildFieldDescription($field, $definition),
54
        'default' => $this->buildFieldDefault($field, $definition),
55
      ];
56
    }, $definition['fields']);
57
  }
58
59
  /**
60
   * @param $field
61
   *
62
   * @return array
63
   */
64
  protected function buildFieldType($field) {
65
    $type = is_array($field) ? $field['type'] : $field;
66
    return StringHelper::parseType($type);
67
  }
68
69
  /**
70
   * @param $field
71
   * @param $definition
72
   *
73
   * @return string
74
   */
75
  protected function buildFieldDescription($field, $definition) {
76
    return (string) (isset($field['description']) ? $field['description'] : '');
77
  }
78
79
  /**
80
   * @param $field
81
   * @param $definition
82
   *
83
   * @return null
84
   */
85
  protected function buildFieldDefault($field, $definition) {
86
    return isset($field['default']) ? $field['default'] : NULL;
87
  }
88
}
89