Completed
Push — master ( 186982...2975e7 )
by Rafael
05:12
created

QueryAnnotationParser::parse()   C

Complexity

Conditions 14
Paths 160

Size

Total Lines 66

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 14.4467

Importance

Changes 0
Metric Value
dl 0
loc 66
ccs 33
cts 38
cp 0.8684
rs 5.7666
c 0
b 0
f 0
cc 14
nc 160
nop 3
crap 14.4467

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