Passed
Pull Request — master (#78)
by David
01:57
created

MutableObjectType::getFields()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 14
rs 10
c 0
b 0
f 0
cc 4
nc 3
nop 0
1
<?php
2
3
4
namespace TheCodingMachine\GraphQL\Controllers\Types;
5
6
use Exception;
7
use GraphQL\Error\InvariantViolation;
8
use GraphQL\Type\Definition\FieldDefinition;
9
use GraphQL\Type\Definition\ObjectType;
10
use TheCodingMachine\GraphQL\Controllers\Annotations\Type;
11
use TheCodingMachine\GraphQL\Controllers\Mappers\RecursiveTypeMapperInterface;
12
13
/**
14
 * An object type built from the Type annotation
15
 */
16
class MutableObjectType extends ObjectType
17
{
18
    // In pending state, we can still add fields.
19
    public const STATUS_PENDING = 'pending';
20
    public const STATUS_FROZEN = 'frozen';
21
22
    /**
23
     * @var bool
24
     */
25
    private $status;
26
27
    /**
28
     * @var array<callable>
29
     */
30
    private $fieldsCallables = [];
31
32
    /**
33
     * @var FieldDefinition[]|null
34
     */
35
    private $finalFields;
36
37
    /**
38
     * @param object $annotatedObject
39
     * @param RecursiveTypeMapperInterface $recursiveTypeMapper
40
     * @param Type $typeField
41
     */
42
    public function __construct(array $config)
43
    {
44
        $this->status = self::STATUS_PENDING;
0 ignored issues
show
Documentation Bug introduced by
The property $status was declared of type boolean, but self::STATUS_PENDING is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
45
46
        parent::__construct($config);
47
    }
48
49
    public function freeze(): void
50
    {
51
        $this->status = self::STATUS_FROZEN;
0 ignored issues
show
Documentation Bug introduced by
The property $status was declared of type boolean, but self::STATUS_FROZEN is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
52
    }
53
54
    public function getStatus(): string
55
    {
56
        return $this->status;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->status returns the type boolean which is incompatible with the type-hinted return string.
Loading history...
57
    }
58
59
    public function addFields(callable $fields): void
60
    {
61
        if ($this->status !== self::STATUS_PENDING) {
0 ignored issues
show
introduced by
The condition $this->status !== self::STATUS_PENDING is always true.
Loading history...
62
            throw new \RuntimeException('Tried to add fields to a frozen MutableObjectType.');
63
        }
64
        $this->fieldsCallables[] = $fields;
65
    }
66
67
    /**
68
     * @param string $name
69
     *
70
     * @return FieldDefinition
71
     *
72
     * @throws Exception
73
     */
74
    public function getField($name): FieldDefinition
75
    {
76
        if ($this->status === self::STATUS_PENDING) {
0 ignored issues
show
introduced by
The condition $this->status === self::STATUS_PENDING is always false.
Loading history...
77
            throw new \RuntimeException('You must freeze() a MutableObjectType before fetching its fields.');
78
        }
79
        return parent::getField($name);
80
    }
81
82
    /**
83
     * @param string $name
84
     *
85
     * @return bool
86
     */
87
    public function hasField($name): bool
88
    {
89
        if ($this->status === self::STATUS_PENDING) {
0 ignored issues
show
introduced by
The condition $this->status === self::STATUS_PENDING is always false.
Loading history...
90
            throw new \RuntimeException('You must freeze() a MutableObjectType before fetching its fields.');
91
        }
92
        return parent::hasField($name);
93
    }
94
95
    /**
96
     * @return FieldDefinition[]
97
     *
98
     * @throws InvariantViolation
99
     */
100
    public function getFields(): array
101
    {
102
        if ($this->finalFields === null) {
103
            if ($this->status === self::STATUS_PENDING) {
0 ignored issues
show
introduced by
The condition $this->status === self::STATUS_PENDING is always false.
Loading history...
104
                throw new \RuntimeException('You must freeze() a MutableObjectType before fetching its fields.');
105
            }
106
107
            $this->finalFields = parent::getFields();
108
            foreach ($this->fieldsCallables as $fieldsCallable) {
109
                $this->finalFields += $fieldsCallable();
110
            }
111
        }
112
113
        return $this->finalFields;
114
    }
115
}
116