Completed
Pull Request — master (#5)
by Yonel Ceruto
10:19 queued 23s
created

resolveFieldNonNull()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 3
cts 4
cp 0.75
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 1
crap 3.1406
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Definition\Loader\Annotation\FieldDecorator;
12
13
use Ynlo\GraphQLBundle\Annotation;
14
use Ynlo\GraphQLBundle\Definition\FieldDefinition;
15
use Ynlo\GraphQLBundle\Definition\Loader\Annotation\AnnotationReaderAwareTrait;
16
use Ynlo\GraphQLBundle\Definition\ObjectDefinitionInterface;
17
use Ynlo\GraphQLBundle\Util\TypeUtil;
18
19
/**
20
 * Decorate a field definition using common GraphQL annotations
21
 */
22
class GraphQLFieldDefinitionDecorator implements FieldDefinitionDecoratorInterface
23
{
24
    use AnnotationReaderAwareTrait;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 21
    public function decorateFieldDefinition($field, FieldDefinition $definition, ObjectDefinitionInterface $objectDefinition)
30
    {
31 21
        if (!$field instanceof \ReflectionProperty && !$field instanceof \ReflectionMethod) {
32
            throw new \InvalidArgumentException('Invalid argument, expected reflection of property or method');
33
        }
34
35 21
        if (null !== $name = $this->resolveFieldName($field)) {
36 21
            $definition->setName($name);
37
        }
38
39 21
        if (null !== $type = $this->resolveFieldType($field)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $type is dead and can be removed.
Loading history...
40 21
            $definition->setType($this->resolveFieldType($field));
41 21
            $definition->setList($this->resolveFieldIsList($field));
42 21
            $definition->setNonNull($this->resolveFieldNonNull($field));
43 21
            $definition->setNonNullList($this->resolveFieldNonNullList($field));
44
        }
45
46 21
        if (null !== $description = $this->resolveFieldDescription($field)) {
47 21
            $definition->setDescription($description);
48
        }
49
50 21
        if (null !== $deprecationReason = $this->resolveFieldDeprecationReason($field)) {
51
            $definition->setDeprecationReason($deprecationReason);
52
        }
53 21
54
        if (null !== $complexity = $this->resolveFieldComplexity($field)) {
55
            $definition->setComplexity($complexity);
56
        }
57
    }
58
59
    /**
60
     * Get field specific annotation matching given objectDefinition
61
     *
62
     * @param \ReflectionMethod|\ReflectionProperty $prop
63 21
     * @param string                                $annotationClass
64
     *
65 21
     * @return mixed
66 21
     */
67
    protected function getFieldAnnotation($prop, string $annotationClass)
68
    {
69 21
        if ($prop instanceof \ReflectionProperty) {
70
            return $this->reader->getPropertyAnnotation($prop, $annotationClass);
71
        }
72
73
        return $this->reader->getMethodAnnotation($prop, $annotationClass);
74
    }
75
76
    /**
77 21
     * @param \ReflectionMethod|\ReflectionProperty $prop
78
     *
79
     * @return string|null
80 21
     */
81 21
    protected function resolveFieldDescription($prop): ?string
82
    {
83
        /** @var Annotation\Field $annotation */
84 21
        if ($annotation = $this->getFieldAnnotation($prop, Annotation\Field::class)) {
85
            return $annotation->description;
86
        }
87
88
        return null;
89
    }
90
91
    /**
92 21
     * @param \ReflectionMethod|\ReflectionProperty $prop
93
     *
94
     * @return string|null
95 21
     */
96 21
    protected function resolveFieldDeprecationReason($prop): ?string
97
    {
98
        /** @var Annotation\Field $annotation */
99 21
        if ($annotation = $this->getFieldAnnotation($prop, Annotation\Field::class)) {
100
            return $annotation->deprecationReason;
101
        }
102
103
        return null;
104
    }
105
106
    /**
107 21
     * @param \ReflectionMethod|\ReflectionProperty $prop
108
     *
109
     * @return string|null
110 21
     */
111 21
    protected function resolveFieldComplexity($prop): ?string
112 21
    {
113
        /** @var Annotation\Field $annotation */
114
        if ($annotation = $this->getFieldAnnotation($prop, Annotation\Field::class)) {
115
            return $annotation->complexity;
116
        }
117
118
        return null;
119
    }
120
121
    /**
122
     * @param \ReflectionMethod|\ReflectionProperty $prop
123 21
     *
124
     * @return bool|null
125
     */
126 21
    protected function resolveFieldNonNull($prop): ?bool
127 21
    {
128 21
        /** @var Annotation\Field $annotationField */
129
        $annotationField = $this->getFieldAnnotation($prop, Annotation\Field::class);
130
        if ($annotationField && $annotationField->type) {
131
            return TypeUtil::isTypeNonNull($annotationField->type);
132
        }
133
134
        return null;
135
    }
136
137
    /**
138
     * @param \ReflectionMethod|\ReflectionProperty $prop
139 21
     *
140
     * @return bool|null
141
     */
142 21
    protected function resolveFieldNonNullList($prop): ?bool
143 21
    {
144 21
        /** @var Annotation\Field $annotationField */
145
        $annotationField = $this->getFieldAnnotation($prop, Annotation\Field::class);
146
        if ($annotationField && $annotationField->type) {
147
            return TypeUtil::isTypeNonNullList($annotationField->type);
148
        }
149
150
        return null;
151
    }
152
153
    /**
154
     * @param \ReflectionMethod|\ReflectionProperty $prop
155 21
     *
156
     * @return bool|null
157 21
     */
158
    protected function resolveFieldIsList($prop): ?bool
159
    {
160 21
        /** @var Annotation\Field $annotationField */
161 21
        $annotationField = $this->getFieldAnnotation($prop, Annotation\Field::class);
162 21
        if ($annotationField && $annotationField->type) {
163
            return TypeUtil::isTypeList($annotationField->type);
164
        }
165 21
166
        return null;
167
    }
168
169
    /**
170
     * @param \ReflectionMethod|\ReflectionProperty $prop
171
     *
172
     * @return string|null
173 21
     */
174
    protected function resolveFieldType($prop): ?string
175
    {
176 21
        $type = null;
177 21
178
        /** @var Annotation\Field $annotationField */
179
        $annotationField = $this->getFieldAnnotation($prop, Annotation\Field::class);
180 21
        if ($annotationField && $annotationField->type) {
181 21
            $type = TypeUtil::normalize($annotationField->type);
182
        }
183
184 21
        return $type;
185
    }
186
187
    /**
188
     * @param \ReflectionMethod|\ReflectionProperty $prop
189
     *
190
     * @return string|null
191
     */
192
    protected function resolveFieldName($prop): ?string
193
    {
194
        /** @var Annotation\Field $annotation */
195
        if (($annotation = $this->getFieldAnnotation($prop, Annotation\Field::class)) && $annotation->name) {
196
            return $annotation->name;
197
        }
198
199
        if ($prop instanceof \ReflectionMethod) {
200
            return lcfirst(preg_replace('/^(get|set)/', null, $prop->name));
201
        }
202
203
        return $prop->name;
204
    }
205
}
206