Completed
Pull Request — 8.x-3.x (#484)
by Sebastian
03:39
created

EntityCreated   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A buildArguments() 10 10 1
A resolveValues() 9 9 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_core\Plugin\GraphQL\Fields\Entity;
4
5
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
6
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface;
7
use Youshido\GraphQL\Execution\ResolveInfo;
8
use DateTime;
9
10
/**
11
 * Get the entities created date if available.
12
 *
13
 * @GraphQLField(
14
 *   id = "entity_created",
15
 *   secure = true,
16
 *   name = "entityCreated",
17
 *   type = "String",
18
 *   parents = {"Entity"}
19
 * )
20
 */
21 View Code Duplication
class EntityCreated extends FieldPluginBase {
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...
22
23
  /**
24
   * {@inheritdoc}
25
   */
26
  protected function buildArguments(PluggableSchemaBuilderInterface $schemaBuilder) {
27
    $arguments = parent::buildArguments($schemaBuilder);
28
29
    // Set the default value of the format argument programmatically.
30
    /** @var \Youshido\GraphQL\Field\AbstractInputField $format */
31
    $format = $arguments['format'];
32
    $format->getConfig()->set('defaultValue', DateTime::ISO8601);
33
34
    return $arguments;
35
  }
36
37
  /**
38
   * {@inheritdoc}
39
   */
40
  protected function resolveValues($value, array $args, ResolveInfo $info) {
41
    // `getCreatedTime` is on NodeInterface which feels weird, since there
42
    // is a generic `EntityInterface`. Checking for method existence for now.
43
    if (method_exists($value, 'getCreatedTime')) {
44
      $datetime = new DateTime();
45
      $datetime->setTimestamp($value->getCreatedTime());
46
      yield $datetime->format($args['format']);
47
    }
48
  }
49
50
}
51