Completed
Pull Request — master (#7)
by Yonel Ceruto
10:45 queued 04:14
created

StandaloneFieldParser   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
wmc 9
dl 0
loc 59
ccs 26
cts 30
cp 0.8667
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C parse() 0 42 8
A supports() 0 3 1
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;
12
13
use Ynlo\GraphQLBundle\Annotation;
14
use Ynlo\GraphQLBundle\Definition\FieldDefinition;
15
use Ynlo\GraphQLBundle\Definition\ObjectDefinitionInterface;
16
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
17
use Ynlo\GraphQLBundle\Util\ClassUtils;
18
use Ynlo\GraphQLBundle\Util\TypeUtil;
19
20
/**
21
 * Resolve field of types queries using naming conventions
22
 */
23
class StandaloneFieldParser extends QueryAnnotationParser
24
{
25
    use AnnotationReaderAwareTrait;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 22
    public function supports($annotation): bool
31
    {
32 22
        return $annotation instanceof Annotation\Field;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     *
38
     * @param Annotation\Field $annotation
39
     */
40 22
    public function parse($annotation, \ReflectionClass $refClass, Endpoint $endpoint)
41
    {
42 22
        $field = new FieldDefinition();
43
44 22
        if ($annotation->name) {
45
            $field->setName($annotation->name);
46
        } else {
47 22
            $field->setName(lcfirst(ClassUtils::getDefaultName($refClass->getName())));
48
        }
49
50 22
        $objectType = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $objectType is dead and can be removed.
Loading history...
51 22
        preg_match('/(\w+)\\\\Field\\\\(\w+)$/', $refClass->getName(), $matches);
52 22
        if (!isset($matches[1]) || !$endpoint->hasType($matches[1])) {
53
            $error = sprintf('Can`t resolve a valid object type for field "%s"', $refClass->getName());
54
            throw new \RuntimeException($error);
55
        }
56
        /** @var ObjectDefinitionInterface $objectDefinition */
57 22
        $objectDefinition = $endpoint->getType($matches[1]);
58 22
        $objectDefinition->addField($field);
59
60 22
        $argAnnotations = $this->reader->getClassAnnotations($refClass);
61 22
        foreach ($argAnnotations as $argAnnotation) {
62 22
            if ($argAnnotation instanceof Annotation\Argument) {
63 22
                $this->resolveArgument($field, $argAnnotation);
64
            }
65
        }
66
67 22
        $field->setType(TypeUtil::normalize($annotation->type));
68 22
        $field->setList(TypeUtil::isTypeList($annotation->type));
69 22
        $field->setResolver($annotation->resolver ?? $refClass->getName());
0 ignored issues
show
Bug introduced by
The property resolver does not seem to exist on Ynlo\GraphQLBundle\Annotation\Field.
Loading history...
70 22
        $field->setDeprecationReason($annotation->deprecationReason);
71 22
        $field->setDescription($annotation->description);
72 22
        $field->setComplexity($annotation->complexity);
73 22
        $field->setMaxConcurrentUsage($annotation->maxConcurrentUsage);
74 22
        $field->setRoles((array) $annotation->roles);
75
76 22
        if ($annotation->roles) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $annotation->roles of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
77
            $annotation->options['roles'] = (array) $annotation->roles;
78
        }
79
80 22
        foreach ($annotation->options as $option => $value) {
81 22
            $field->setMeta($option, $value);
82
        }
83 22
    }
84
}
85