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

FieldConnectionAnnotationParser::parse()   C

Complexity

Conditions 10
Paths 51

Size

Total Lines 67
Code Lines 46

Duplication

Lines 24
Ratio 35.82 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
eloc 46
nc 51
nop 3
dl 24
loc 67
ccs 0
cts 45
cp 0
crap 110
rs 6.1506
c 0
b 0
f 0

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 Ynlo\GraphQLBundle\Annotation;
14
use Ynlo\GraphQLBundle\Definition\ArgumentDefinition;
15
use Ynlo\GraphQLBundle\Definition\ConnectionDefinitionBuilder;
16
use Ynlo\GraphQLBundle\Definition\FieldDefinition;
17
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionManager;
18
use Ynlo\GraphQLBundle\Util\TypeUtil;
19
20
/**
21
 * Resolve field of types queries using naming conventions
22
 */
23
class FieldConnectionAnnotationParser implements AnnotationParserInterface
24
{
25
    use AnnotationReaderAwareTrait;
26
27
    /**
28
     * @var ConnectionDefinitionBuilder
29
     */
30
    protected $connectionBuilder;
31
32
    /**
33
     * @var DefinitionManager
34
     */
35
    protected $definitionManager;
36
37
    /**
38
     * @param ConnectionDefinitionBuilder $connectionBuilder
39
     */
40 1
    public function __construct(ConnectionDefinitionBuilder $connectionBuilder)
41
    {
42 1
        $this->connectionBuilder = $connectionBuilder;
43 1
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 1
    public function supports($annotation): bool
49
    {
50 1
        return $annotation instanceof Annotation\Field;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function parse($annotation, \ReflectionClass $refClass, DefinitionManager $definitionManager)
57
    {
58
        $this->definitionManager = $definitionManager;
59
60
        /** @var Annotation\Field $annotation */
61
        $field = new FieldDefinition();
62
63
        if (!$refClass->hasMethod('__invoke')) {
64
            throw new \LogicException(sprintf('The class %s should have "__invoke" method to resolve field valued', $refClass->getName()));
65
        }
66
67
        if ($annotation->name) {
68
            $field->setName($annotation->name);
69
        } else {
70
            preg_match('/\w+$/', $refClass->getName(), $matches);
71
            $field->setName(lcfirst($matches[0] ?? ''));
72
        }
73
74
        $objectType = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $objectType is dead and can be removed.
Loading history...
75
        preg_match('/(\w+)\\\\Field\\\\(\w+)$/', $refClass->getName(), $matches);
76 View Code Duplication
        if (!isset($matches[1]) || !$definitionManager->hasType($matches[1])) {
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...
77
            $error = sprintf('Can`t resolve a valid object type for field "%s"', $refClass->getName());
78
            throw new \RuntimeException($error);
79
        }
80
81
        $objectDefinition = $definitionManager->getType($matches[1]);
82
        if ($objectDefinition->hasField($field->getName())) {
83
            $field = $objectDefinition->getField($field->getName());
84
        } else {
85
            $objectDefinition->addField($field);
86
        }
87
88
        $argAnnotations = $this->reader->getClassAnnotations($refClass);
89 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...
90
            if ($argAnnotation instanceof Annotation\Argument) {
91
                $arg = new ArgumentDefinition();
92
                $arg->setName($argAnnotation->name);
93
                $arg->setDescription($argAnnotation->description);
94
                $arg->setInternalName($argAnnotation->internalName);
95
                $arg->setDefaultValue($argAnnotation->defaultValue);
96
                $arg->setType(TypeUtil::normalize($argAnnotation->type));
97
                $arg->setList(TypeUtil::isTypeList($argAnnotation->type));
98
                $arg->setNonNullList(TypeUtil::isTypeNonNullList($argAnnotation->type));
99
                $arg->setNonNull(TypeUtil::isTypeNonNull($argAnnotation->type));
100
                $field->addArgument($arg);
101
            }
102
        }
103
104
        $field->setDeprecationReason($annotation->deprecationReason ?? $field->getDeprecationReason());
105
        $field->setDescription($annotation->description ?? $field->getDescription());
106
        $field->setType(TypeUtil::normalize($annotation->type) ?? $field->getType());
107
108
        if ($annotation->type) {
109
            $field->setList(TypeUtil::isTypeList($annotation->type));
110
            $field->setNonNull(TypeUtil::isTypeNonNull($annotation->type));
111
            $field->setNonNullList(TypeUtil::isTypeNonNullList($annotation->type));
112
        }
113
114
        $field->setResolver($refClass->getName());
115
116
        /** @var Annotation\Connection $connection */
117 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...
118
            $this->connectionBuilder->setEndpoint($this->definitionManager->getEndpoint());
119
            $this->connectionBuilder->setLimit($connection->limit);
120
            $this->connectionBuilder->setParentField($connection->parentField);
121
            $this->connectionBuilder->build($field, $field->getType());
122
            $field->setResolver($refClass->getName());
123
        }
124
    }
125
}
126