Completed
Pull Request — master (#45)
by Christoffer
02:12
created

ValidationContext::getParentType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\GraphQL\Validation;
4
5
use Digia\GraphQL\Error\ValidationException;
6
use Digia\GraphQL\Language\AST\Node\DocumentNode;
7
use Digia\GraphQL\Language\AST\Node\FragmentDefinitionNode;
8
use Digia\GraphQL\Type\Definition\Argument;
9
use Digia\GraphQL\Type\Definition\Directive;
10
use Digia\GraphQL\Type\Definition\Field;
11
use Digia\GraphQL\Type\Definition\TypeInterface;
12
use Digia\GraphQL\Type\SchemaInterface;
13
use Digia\GraphQL\Util\TypeInfo;
14
15
class ValidationContext
16
{
17
    /**
18
     * @var SchemaInterface
19
     */
20
    protected $schema;
21
22
    /**
23
     * @var DocumentNode
24
     */
25
    protected $documentNode;
26
27
    /**
28
     * @var TypeInfo
29
     */
30
    protected $typeInfo;
31
32
    /**
33
     * @var array|ValidationException[]
34
     */
35
    protected $errors = [];
36
37
    /**
38
     * @var array|FragmentDefinitionNode[]
39
     */
40
    protected $fragments = [];
41
42
    /**
43
     * ValidationContext constructor.
44
     * @param SchemaInterface $schema
45
     * @param DocumentNode    $documentNode
46
     * @param TypeInfo        $typeInfo
47
     */
48
    public function __construct(SchemaInterface $schema, DocumentNode $documentNode, TypeInfo $typeInfo)
49
    {
50
        $this->schema       = $schema;
51
        $this->documentNode = $documentNode;
52
        $this->typeInfo     = $typeInfo;
53
    }
54
55
    /**
56
     * @param ValidationException $error
57
     */
58
    public function reportError(ValidationException $error): void
59
    {
60
        $this->errors[] = $error;
61
    }
62
63
    /**
64
     * @return array|ValidationException[]
65
     */
66
    public function getErrors(): array
67
    {
68
        return $this->errors;
69
    }
70
71
    /**
72
     * @return TypeInterface|null
73
     */
74
    public function getParentType(): ?TypeInterface
75
    {
76
        return $this->typeInfo->getParentType();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->typeInfo->getParentType() could return the type Digia\GraphQL\Type\Defin...\CompositeTypeInterface which is incompatible with the type-hinted return null|Digia\GraphQL\Type\Definition\TypeInterface. Consider adding an additional type-check to rule them out.
Loading history...
77
    }
78
79
    /**
80
     * @return Field|null
81
     */
82
    public function getFieldDefinition(): ?Field
83
    {
84
        return $this->typeInfo->getFieldDefinition();
85
    }
86
87
    /**
88
     * @return SchemaInterface
89
     */
90
    public function getSchema(): SchemaInterface
91
    {
92
        return $this->schema;
93
    }
94
95
    /**
96
     * @return Argument|null
97
     */
98
    public function getArgument(): ?Argument
99
    {
100
        return $this->typeInfo->getArgument();
101
    }
102
103
    /**
104
     * @return Directive|null
105
     */
106
    public function getDirective(): ?Directive
107
    {
108
        return $this->typeInfo->getDirective();
109
    }
110
111
    /**
112
     * @param string $name
113
     * @return FragmentDefinitionNode|null
114
     */
115
    public function getFragment(string $name): ?FragmentDefinitionNode
116
    {
117
        if (empty($this->fragments)) {
118
            $this->fragments = array_reduce($this->documentNode->getDefinitions(), function ($fragments, $definition) {
119
                if ($definition instanceof FragmentDefinitionNode) {
120
                    $fragments[$definition->getNameValue()] = $definition;
121
                }
122
                return $fragments;
123
            }, []);
124
        }
125
126
        return $this->fragments[$name] ?? null;
127
    }
128
}
129