Completed
Push — master ( 2975e7...46c3ee )
by Rafael
07:49
created

QueryAnnotationParser::parse()   D

Complexity

Conditions 15
Paths 240

Size

Total Lines 70

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 15.7045

Importance

Changes 0
Metric Value
dl 0
loc 70
ccs 35
cts 41
cp 0.8537
rs 4.5833
c 0
b 0
f 0
cc 15
nc 240
nop 3
crap 15.7045

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Doctrine\Common\Annotations\Reader;
14
use Ynlo\GraphQLBundle\Annotation;
15
use Ynlo\GraphQLBundle\Definition\ArgumentAwareInterface;
16
use Ynlo\GraphQLBundle\Definition\ArgumentDefinition;
17
use Ynlo\GraphQLBundle\Definition\QueryDefinition;
18
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
19
use Ynlo\GraphQLBundle\Util\ClassUtils;
20
use Ynlo\GraphQLBundle\Util\TypeUtil;
21
22
/**
23
 * Parse Query annotation to fetch queries
24
 */
25
class QueryAnnotationParser implements AnnotationParserInterface
26
{
27
    /**
28
     * @var Reader
29
     */
30
    protected $reader;
31
32
    /**
33
     * QueryAnnotationParser constructor.
34
     *
35
     * @param Reader $reader
36
     */
37 35
    public function __construct(Reader $reader)
38
    {
39 35
        $this->reader = $reader;
40 35
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 33
    public function supports($annotation): bool
46
    {
47 33
        return $annotation instanceof Annotation\Query;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     *
53
     * @param Annotation\Query $annotation
54
     */
55 13
    public function parse($annotation, \ReflectionClass $refClass, Endpoint $endpoint)
56
    {
57 13
        $query = new QueryDefinition();
58
59 13
        if ($annotation->name) {
60 11
            $query->setName($annotation->name);
61
        } else {
62 2
            $query->setName(lcfirst(ClassUtils::getDefaultName($refClass->getName())));
63
        }
64
65 13
        $endpoint->addQuery($query);
66
67 13
        if ($annotation->type) {
68 12
            $type = TypeUtil::normalize($annotation->type);
69 12
            if (class_exists($annotation->type)) {
70
                $type = $endpoint->getTypeForClass($annotation->type);
71
            }
72 12
            $query->setNode($type);
73 12
            $query->setType($type);
74
        }
75
76 13
        $query->setList(TypeUtil::isTypeList($annotation->type));
77 13
        $query->setNonNull(TypeUtil::isTypeNonNull($annotation->type));
78 13
        $query->setNonNullList(TypeUtil::isTypeNonNullList($annotation->type));
79
80 13
        if (!$query->getType()) {
81 1
            $nodeType = ClassUtils::getNodeFromClass($refClass->getName());
82 1
            $objectDefinition = null;
83 1
            if ($nodeType && $endpoint->hasType($nodeType)) {
84 1
                $objectDefinition = $endpoint->getType($nodeType);
85
            }
86 1
            if ($objectDefinition) {
87 1
                $query->setType($objectDefinition->getName());
88 1
                $query->setNode($objectDefinition->getName());
89
            } else {
90
                //avoid throw Exception when the schema is empty
91
                //when the schema is empty the NodeInterface has not been loaded
92
                //then the node(id) and nodes(ids) queries fails
93
                if ('Node' === $nodeType) {
94
                    $endpoint->removeQuery($query->getName());
95
96
                    return;
97
                }
98
99
                $error = sprintf('Does not exist any valid type for class "%s"', $refClass->getName());
100
                throw new \RuntimeException($error);
101
            }
102
        }
103
104 13
        if ($annotation->arguments) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $annotation->arguments of type Ynlo\GraphQLBundle\Annotation\Argument[] 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...
105 1
            foreach ($annotation->arguments as $argAnnotation) {
106 1
                if ($argAnnotation instanceof Annotation\Argument) {
107 1
                    $this->resolveArgument($query, $argAnnotation);
108
                }
109
            }
110
        } else {
111 12
            $argAnnotations = $this->reader->getClassAnnotations($refClass);
112 12
            foreach ($argAnnotations as $argAnnotation) {
113 1
                if ($argAnnotation instanceof Annotation\Argument) {
114 1
                    $this->resolveArgument($query, $argAnnotation);
115
                }
116
            }
117
        }
118
119 13
        $query->setResolver($annotation->resolver ?? $refClass->getName());
120 13
        $query->setDeprecationReason($annotation->deprecationReason);
121 13
        $query->setDescription($annotation->description);
122
123 13
        foreach ($annotation->options as $option => $value) {
124 13
            $query->setMeta($option, $value);
125
        }
126 13
    }
127
128
    /**
129
     * @param ArgumentAwareInterface $argumentAware
130
     * @param object                 $argAnnotation
131
     */
132 3
    public function resolveArgument(ArgumentAwareInterface $argumentAware, $argAnnotation)
133
    {
134 3
        $arg = new ArgumentDefinition();
135 3
        $arg->setName($argAnnotation->name);
136 3
        $arg->setDescription($argAnnotation->description);
137 3
        $arg->setInternalName($argAnnotation->internalName);
138 3
        $arg->setDefaultValue($argAnnotation->defaultValue);
139 3
        $arg->setType(TypeUtil::normalize($argAnnotation->type));
140 3
        $arg->setList(TypeUtil::isTypeList($argAnnotation->type));
141 3
        $arg->setNonNullList(TypeUtil::isTypeNonNullList($argAnnotation->type));
142 3
        $arg->setNonNull(TypeUtil::isTypeNonNull($argAnnotation->type));
143 3
        $argumentAware->addArgument($arg);
144 3
    }
145
}
146