QueryAnnotationParser::parse()   F
last analyzed

Complexity

Conditions 16
Paths 400

Size

Total Lines 72
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 16.747

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 44
c 1
b 1
f 0
dl 0
loc 72
ccs 36
cts 42
cp 0.8571
rs 2.2333
cc 16
nc 400
nop 3
crap 16.747

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 48
    public function __construct(Reader $reader)
38
    {
39 48
        $this->reader = $reader;
40 48
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 43
    public function supports($annotation): bool
46
    {
47 43
        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
            if ($endpoint->hasType($type)) {
73 12
                $query->setNode($type);
74
            }
75 12
            $query->setType($type);
76
        }
77
78 13
        $query->setList(TypeUtil::isTypeList($annotation->type));
79 13
        $query->setNonNull(TypeUtil::isTypeNonNull($annotation->type));
80 13
        $query->setNonNullList(TypeUtil::isTypeNonNullList($annotation->type));
81
82 13
        if (!$query->getType()) {
83 1
            $nodeType = ClassUtils::getNodeFromClass($refClass->getName());
84 1
            $objectDefinition = null;
85 1
            if ($nodeType && $endpoint->hasType($nodeType)) {
86 1
                $objectDefinition = $endpoint->getType($nodeType);
87
            }
88 1
            if ($objectDefinition) {
89 1
                $query->setType($objectDefinition->getName());
90 1
                $query->setNode($objectDefinition->getName());
91
            } else {
92
                //avoid throw Exception when the schema is empty
93
                //when the schema is empty the NodeInterface has not been loaded
94
                //then the node(id) and nodes(ids) queries fails
95
                if ('Node' === $nodeType) {
96
                    $endpoint->removeQuery($query->getName());
97
98
                    return;
99
                }
100
101
                $error = sprintf('Does not exist any valid type for class "%s"', $refClass->getName());
102
                throw new \RuntimeException($error);
103
            }
104
        }
105
106 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...
107 1
            foreach ($annotation->arguments as $argAnnotation) {
108 1
                if ($argAnnotation instanceof Annotation\Argument) {
109 1
                    $this->resolveArgument($query, $argAnnotation);
110
                }
111
            }
112
        } else {
113 12
            $argAnnotations = $this->reader->getClassAnnotations($refClass);
114 12
            foreach ($argAnnotations as $argAnnotation) {
115 1
                if ($argAnnotation instanceof Annotation\Argument) {
116 1
                    $this->resolveArgument($query, $argAnnotation);
117
                }
118
            }
119
        }
120
121 13
        $query->setResolver($annotation->resolver ?? $refClass->getName());
122 13
        $query->setDeprecationReason($annotation->deprecationReason);
123 13
        $query->setDescription($annotation->description);
124
125 13
        foreach ($annotation->options as $option => $value) {
126 13
            $query->setMeta($option, $value);
127
        }
128 13
    }
129
130
    /**
131
     * @param ArgumentAwareInterface $argumentAware
132
     * @param Annotation\Argument    $argAnnotation
133
     */
134 3
    protected function resolveArgument(ArgumentAwareInterface $argumentAware, Annotation\Argument $argAnnotation): void
135
    {
136 3
        $arg = new ArgumentDefinition();
137 3
        $arg->setName($argAnnotation->name);
138 3
        $arg->setDescription($argAnnotation->description);
139 3
        $arg->setInternalName($argAnnotation->internalName);
140 3
        $arg->setDefaultValue($argAnnotation->defaultValue);
141 3
        $arg->setType(TypeUtil::normalize($argAnnotation->type));
142 3
        $arg->setList(TypeUtil::isTypeList($argAnnotation->type));
143 3
        $arg->setNonNullList(TypeUtil::isTypeNonNullList($argAnnotation->type));
144 3
        $arg->setNonNull(TypeUtil::isTypeNonNull($argAnnotation->type));
145 3
        foreach ($argAnnotation->options as $option => $value) {
146
            $arg->setMeta($option, $value);
147
        }
148 3
        $argumentAware->addArgument($arg);
149 3
    }
150
}
151