Completed
Pull Request — 8.x-3.x (#684)
by Sebastian
02:17
created

getDerivativeDefinitionsFromFieldDefinition()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 3
dl 0
loc 37
rs 8.7057
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\Deriver\Fields;
4
5
use Drupal\Core\Field\FieldDefinitionInterface;
6
use Drupal\Core\TypedData\ComplexDataDefinitionInterface;
7
use Drupal\Core\TypedData\DataDefinitionInterface;
8
use Drupal\Core\TypedData\DataReferenceDefinitionInterface;
9
use Drupal\graphql\Utility\StringHelper;
10
use Drupal\graphql_core\Plugin\Deriver\EntityFieldDeriverBase;
11
12
class EntityFieldItemDeriver extends EntityFieldDeriverBase {
13
14
  /**
15
   * {@inheritdoc}
16
   */
17
  protected function getDerivativeDefinitionsFromFieldDefinition(FieldDefinitionInterface $fieldDefinition, array $basePluginDefinition, $bundleId = NULL) {
18
    $itemDefinition = $fieldDefinition->getItemDefinition();
19
    if (!($itemDefinition instanceof ComplexDataDefinitionInterface) || !$propertyDefinitions = $itemDefinition->getPropertyDefinitions()) {
0 ignored issues
show
Bug introduced by
The class Drupal\Core\TypedData\Co...DataDefinitionInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
20
      return [];
21
    }
22
23
    if (count($propertyDefinitions) <= 1) {
24
      return [];
25
    }
26
27
    $tags = array_merge($fieldDefinition->getCacheTags(), ['entity_field_info']);
28
    $contexts = $fieldDefinition->getCacheContexts();
29
    $maxAge = $fieldDefinition->getCacheMaxAge();
30
31
    $entityTypeId = $fieldDefinition->getTargetEntityTypeId();
32
    $fieldName = $fieldDefinition->getName();
33
    $fieldBundle = $fieldDefinition->getTargetBundle() ?: '';
34
35
    $commonDefinition = [
36
      'parents' => [StringHelper::camelCase('field', $entityTypeId, $fieldBundle, $fieldName)],
37
      'schema_cache_tags' => $tags,
38
      'schema_cache_contexts' => $contexts,
39
      'schema_cache_max_age' => $maxAge,
40
    ] + $basePluginDefinition;
41
42
    $derivatives = [];
43
    foreach ($propertyDefinitions as $property => $propertyDefinition) {
44
      $derivatives["$entityTypeId-$fieldName-$fieldBundle-$property"] = [
45
        'name' => StringHelper::propCase($property),
46
        'description' => $propertyDefinition->getDescription(),
47
        'property' => $property,
48
        'type' => $this->extractDataType($propertyDefinition),
49
      ] + $commonDefinition;
50
    }
51
52
    return $derivatives;
53
  }
54
55
  /**
56
   * Extracts the data type of a property's data definition.
57
   *
58
   * @param \Drupal\Core\TypedData\DataDefinitionInterface $propertyDefinition
59
   *   The property's data definition.
60
   *
61
   * @return string
62
   *   The property's data type.
63
   */
64
  protected function extractDataType(DataDefinitionInterface $propertyDefinition) {
65
    if ($propertyDefinition instanceof DataReferenceDefinitionInterface) {
0 ignored issues
show
Bug introduced by
The class Drupal\Core\TypedData\Da...enceDefinitionInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
66
      return $propertyDefinition->getTargetDefinition()->getDataType();
67
    }
68
69
    return $propertyDefinition->getDataType();
70
  }
71
72
}
73