Passed
Push — master ( 5ee55a...802101 )
by Christoffer
02:39
created

FieldsTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 89
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getFields() 0 7 2
A setFields() 0 4 1
B buildFieldMap() 0 46 4
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
     * @var array|callable
15
     */
16
    protected $fieldsOrThunk;
17
18
    /**
19
     * @var Field[]
20
     */
21
    protected $fieldMap;
22
23
    /**
24
     * @return Field[]
25
     * @throws InvariantException
26
     */
27
    public function getFields(): array
28
    {
29
        // Fields are built lazily to avoid concurrency issues.
30
        if (!isset($this->fieldMap)) {
31
            $this->fieldMap = $this->buildFieldMap($this->fieldsOrThunk ?? []);
32
        }
33
        return $this->fieldMap;
34
    }
35
36
    /**
37
     * Fields are created using the `ConfigAwareTrait` constructor which will automatically
38
     * call this method when setting arguments from `$config['fields']`.
39
     *
40
     * @param array|callable $fieldsOrThunk
41
     * @return $this
42
     */
43
    protected function setFields($fieldsOrThunk)
44
    {
45
        $this->fieldsOrThunk = $fieldsOrThunk;
46
        return $this;
47
    }
48
49
    /**
50
     * @param mixed $fieldsOrThunk
51
     * @return array
52
     * @throws InvariantException
53
     */
54
    protected function buildFieldMap($fieldsOrThunk): array
55
    {
56
        $fields = resolveThunk($fieldsOrThunk);
57
58
        invariant(
59
            isAssocArray($fields),
60
            \sprintf(
61
                '%s fields must be an associative array with field names as key or a callable which returns such an array.',
62
                $this->getName()
0 ignored issues
show
Bug introduced by
It seems like getName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
                $this->/** @scrutinizer ignore-call */ 
63
                       getName()
Loading history...
63
            )
64
        );
65
66
        $fieldMap = [];
67
68
        foreach ($fields as $fieldName => $fieldConfig) {
69
            invariant(
70
                \is_array($fieldConfig),
71
                \sprintf('%s.%s field config must be an array', $this->getName(), $fieldName)
72
            );
73
74
            invariant(
75
                !isset($fieldConfig['isDeprecated']),
76
                \sprintf(
77
                    '%s.%s should provide "deprecationReason" instead of "isDeprecated".',
78
                    $this->getName(),
79
                    $fieldName
80
                )
81
            );
82
83
            if (isset($fieldConfig['resolve'])) {
84
                invariant(
85
                    null === $fieldConfig['resolve'] || \is_callable($fieldConfig['resolve']),
86
                    \sprintf(
87
                        '%s.%s field resolver must be a function if provided, but got: %s',
88
                        $this->getName(),
89
                        $fieldName,
90
                        toString($fieldConfig['resolve'])
91
                    )
92
                );
93
            }
94
95
            $fieldConfig['name'] = $fieldName;
96
            $fieldMap[$fieldName] = new Field($fieldConfig);
97
        }
98
99
        return $fieldMap;
100
    }
101
}
102