EntityFieldBase::resolveItem()   B
last analyzed

Complexity

Conditions 10
Paths 37

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 14
c 3
b 0
f 0
dl 0
loc 27
rs 7.6666
cc 10
nc 37
nop 4

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Fields;
4
5
use Drupal\Component\Render\MarkupInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Component\Render\MarkupInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Drupal\Core\Cache\CacheableDependencyInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Cache\CacheableDependencyInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Drupal\Core\Entity\ContentEntityInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Entity\ContentEntityInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Drupal\Core\Field\FieldItemInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Field\FieldItemInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Drupal\graphql\GraphQL\Execution\ResolveContext;
10
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
11
use GraphQL\Type\Definition\ResolveInfo;
12
use GraphQL\Type\Definition\ScalarType;
13
use GraphQL\Type\Definition\WrappingType;
14
15
/**
16
 * Base class for entity field plugins.
17
 */
18
class EntityFieldBase extends FieldPluginBase {
19
20
  /**
21
   * {@inheritdoc}
22
   */
23
  protected function resolveItem($item, array $args, ResolveContext $context, ResolveInfo $info) {
24
    if ($item instanceof FieldItemInterface) {
25
      $definition = $this->getPluginDefinition();
26
      $property = $definition['property'];
27
      $itemProperty = $item->get($property);
28
      $result = $itemProperty->getValue();
29
      $result = $result instanceof MarkupInterface ? $result->__toString() : $result;
30
31
      $type = $info->returnType;
32
      $type = $type instanceof WrappingType ? $type->getWrappedType(TRUE) : $type;
33
      if ($type instanceof ScalarType) {
34
        $result = is_null($result) ? NULL : $type->serialize($result);
35
      }
36
37
      if ($result instanceof ContentEntityInterface && $result->isTranslatable() && $language = $context->getContext('language', $info)) {
38
        if ($result->hasTranslation($language)) {
39
          $result = $result->getTranslation($language);
40
        }
41
      }
42
43
      // if ($itemProperty instanceof CacheableDependencyInterface) {
44
      //   $context->addCacheTags($itemProperty->getCacheTags());
45
      //   $context->addCacheContexts($itemProperty->getCacheContexts());
46
      //   $context->mergeCacheMaxAge($itemProperty->getCacheMaxAge());
47
      // }
48
49
      return $result;
50
    }
51
  }
52
53
}
54