Completed
Push — master ( cbfed7...c39fb6 )
by Rafael
05:06
created

QueryAnnotationParser::parse()   B

Complexity

Conditions 7
Paths 28

Size

Total Lines 55
Code Lines 39

Duplication

Lines 25
Ratio 45.45 %

Code Coverage

Tests 34
CRAP Score 7.0261

Importance

Changes 0
Metric Value
cc 7
eloc 39
nc 28
nop 3
dl 25
loc 55
ccs 34
cts 37
cp 0.9189
crap 7.0261
rs 7.8235
c 0
b 0
f 0

How to fix   Long Method   

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 Ynlo\GraphQLBundle\Annotation;
14
use Ynlo\GraphQLBundle\Definition\ArgumentDefinition;
15
use Ynlo\GraphQLBundle\Definition\ConnectionDefinitionBuilder;
16
use Ynlo\GraphQLBundle\Definition\QueryDefinition;
17
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionManager;
18
use Ynlo\GraphQLBundle\Util\TypeUtil;
19
20
/**
21
 * Parse Query annotation to fetch queries
22
 */
23
class QueryAnnotationParser implements AnnotationParserInterface
24
{
25
    use AnnotationReaderAwareTrait;
26
    use AnnotationParserHelper;
27
28
    /**
29
     * @var ConnectionDefinitionBuilder
30
     */
31
    protected $connectionBuilder;
32
33
    /**
34
     * @var DefinitionManager
35
     */
36
    protected $definitionManager;
37
38
    /**
39
     * @param ConnectionDefinitionBuilder $connectionBuilder
40
     */
41 1
    public function __construct(ConnectionDefinitionBuilder $connectionBuilder)
42
    {
43 1
        $this->connectionBuilder = $connectionBuilder;
44 1
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 1
    public function supports($annotation): bool
50
    {
51 1
        return $annotation instanceof Annotation\Query;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 1
    public function parse($annotation, \ReflectionClass $refClass, DefinitionManager $definitionManager)
58
    {
59 1
        $this->definitionManager = $definitionManager;
60
61
        /** @var Annotation\Query $annotation */
62 1
        $query = new QueryDefinition();
63
64 1 View Code Duplication
        if ($annotation->name) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65 1
            $query->setName($annotation->name);
66
        } else {
67 1
            $query->setName(lcfirst($this->getDefaultName($refClass, $definitionManager)));
68
        }
69
70 1
        if ($definitionManager->hasQuery($query->getName())) {
71
            $query = $definitionManager->getQuery($query->getName());
72
        } else {
73 1
            $definitionManager->addQuery($query);
74
        }
75
76 1
        $objectDefinition = $this->getObjectDefinition($refClass, $definitionManager);
77 1
        if ($objectDefinition) {
78 1
            $query->setType($objectDefinition->getName());
79 1
            $query->setList($annotation->list);
80
        } else {
81
            $error = sprintf('Does not exist any valid type for class "%s"', $refClass->getName());
82
            throw new \RuntimeException($error);
83
        }
84
85 1
        $argAnnotations = $this->reader->getClassAnnotations($refClass);
86 1 View Code Duplication
        foreach ($argAnnotations as $argAnnotation) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87 1
            if ($argAnnotation instanceof Annotation\Argument) {
88 1
                $arg = new ArgumentDefinition();
89 1
                $arg->setName($argAnnotation->name);
90 1
                $arg->setDescription($argAnnotation->description);
91 1
                $arg->setInternalName($argAnnotation->internalName);
92 1
                $arg->setDefaultValue($argAnnotation->defaultValue);
93 1
                $arg->setType(TypeUtil::normalize($argAnnotation->type));
94 1
                $arg->setList(TypeUtil::isTypeList($argAnnotation->type));
95 1
                $arg->setNonNullList(TypeUtil::isTypeNonNullList($argAnnotation->type));
96 1
                $arg->setNonNull(TypeUtil::isTypeNonNull($argAnnotation->type));
97 1
                $query->addArgument($arg);
98
            }
99
        }
100
101 1
        $query->setResolver($refClass->getName());
102 1
        $query->setDeprecationReason($annotation->deprecationReason);
103 1
        $query->setDescription($annotation->description);
104
105
        /** @var Annotation\Connection $connection */
106 1 View Code Duplication
        if ($connection = $this->reader->getClassAnnotation($refClass, Annotation\Connection::class)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107 1
            $this->connectionBuilder->setEndpoint($this->definitionManager->getEndpoint());
108 1
            $this->connectionBuilder->setLimit($connection->limit);
109 1
            $this->connectionBuilder->setParentField($connection->parentField);
110 1
            $this->connectionBuilder->build($query, $query->getType());
111 1
            $query->setResolver($refClass->getName());
112
        }
113 1
    }
114
}
115