Passed
Push — master ( ffa870...12eb4f )
by Christoffer
02:22
created

FieldsTrait::buildFieldMap()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 54
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 35
nc 3
nop 1
dl 0
loc 54
rs 9.0306
c 0
b 0
f 0

How to fix   Long Method   

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 Digia\GraphQL\Type\Definition;
4
5
use Digia\GraphQL\Error\InvariantException;
6
use function Digia\GraphQL\Type\isAssocArray;
7
use function Digia\GraphQL\Type\resolveThunk;
8
use function Digia\GraphQL\Util\invariant;
9
use function Digia\GraphQL\Util\toString;
10
11
trait FieldsTrait
12
{
13
    /**
14
     * Fields can be defined either as an array or as a thunk.
15
     * Using thunks allows for cross-referencing of fields.
16
     *
17
     * @var array|callable
18
     */
19
    protected $fieldsOrThunk;
20
21
    /**
22
     * A key-value map over field names and their corresponding field instances.
23
     *
24
     * @var Field[]
25
     */
26
    protected $fieldMap;
27
28
    /**
29
     * @return null|string
30
     */
31
    abstract public function getName(): ?string;
32
33
    /**
34
     * @param string $fieldName
35
     * @return Field|null
36
     * @throws InvariantException
37
     */
38
    public function getField(string $fieldName): ?Field
39
    {
40
        return $this->getFields()[$fieldName] ?? null;
41
    }
42
43
    /**
44
     * @return Field[]
45
     * @throws InvariantException
46
     */
47
    public function getFields(): array
48
    {
49
        // Fields are built lazily to avoid concurrency issues.
50
        if (!isset($this->fieldMap)) {
51
            $this->fieldMap = $this->buildFieldMap($this->fieldsOrThunk);
52
        }
53
54
        return $this->fieldMap;
55
    }
56
57
    /**
58
     * @param array|callable $fieldsOrThunk
59
     * @return array
60
     * @throws InvariantException
61
     */
62
    protected function buildFieldMap($fieldsOrThunk): array
63
    {
64
        $fields = resolveThunk($fieldsOrThunk);
65
66
        invariant(
67
            isAssocArray($fields),
68
            \sprintf(
69
                '%s fields must be an associative array with field names as key or a callable which returns such an array.',
70
                $this->getName()
71
            )
72
        );
73
74
        $fieldMap = [];
75
76
        foreach ($fields as $fieldName => $fieldConfig) {
77
            invariant(
78
                \is_array($fieldConfig),
79
                \sprintf('%s.%s field config must be an array', $this->getName(), $fieldName)
80
            );
81
82
            invariant(
83
                !isset($fieldConfig['isDeprecated']),
84
                \sprintf(
85
                    '%s.%s should provide "deprecationReason" instead of "isDeprecated".',
86
                    $this->getName(),
87
                    $fieldName
88
                )
89
            );
90
91
            if (isset($fieldConfig['resolve'])) {
92
                invariant(
93
                    null === $fieldConfig['resolve'] || \is_callable($fieldConfig['resolve']),
94
                    \sprintf(
95
                        '%s.%s field resolver must be a function if provided, but got: %s',
96
                        $this->getName(),
97
                        $fieldName,
98
                        toString($fieldConfig['resolve'])
99
                    )
100
                );
101
            }
102
103
            $fieldMap[$fieldName] = new Field(
104
                $fieldName,
105
                $fieldConfig['description'] ?? null,
106
                $fieldConfig['type'] ?? null,
107
                $fieldConfig['args'] ?? [],
108
                $fieldConfig['resolve'] ?? null,
109
                $fieldConfig['subscribe'] ?? null,
110
                $fieldConfig['deprecationReason'] ?? null,
111
                $fieldConfig['astNode'] ?? null
112
            );
113
        }
114
115
        return $fieldMap;
116
    }
117
}
118