StandaloneFieldParser::parse()   F
last analyzed

Complexity

Conditions 22
Paths 482

Size

Total Lines 67
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 30.1011

Importance

Changes 4
Bugs 3 Features 0
Metric Value
eloc 45
c 4
b 3
f 0
dl 0
loc 67
ccs 32
cts 43
cp 0.7442
rs 0.7194
cc 22
nc 482
nop 3
crap 30.1011

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\FieldDefinition;
15
use Ynlo\GraphQLBundle\Definition\FieldsAwareDefinitionInterface;
16
use Ynlo\GraphQLBundle\Definition\InterfaceDefinition;
17
use Ynlo\GraphQLBundle\Definition\ObjectDefinitionInterface;
18
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
19
use Ynlo\GraphQLBundle\Util\ClassUtils;
20
use Ynlo\GraphQLBundle\Util\TypeUtil;
21
22
/**
23
 * Resolve field of types queries using naming conventions
24
 */
25
class StandaloneFieldParser extends QueryAnnotationParser
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30 39
    public function supports($annotation): bool
31
    {
32 39
        return $annotation instanceof Annotation\Field;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     *
38
     * @param Annotation\Field $annotation
39
     */
40 1
    public function parse($annotation, \ReflectionClass $refClass, Endpoint $endpoint)
41
    {
42 1
        $field = new FieldDefinition();
43
44 1
        if ($annotation->name) {
45
            $field->setName($annotation->name);
46
        } else {
47 1
            $field->setName(lcfirst(ClassUtils::getDefaultName($refClass->getName())));
48
        }
49
50 1
        $objectType = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $objectType is dead and can be removed.
Loading history...
51 1
        preg_match('/(\w+)\\\\Field\\\\(\w+)$/', $refClass->getName(), $matches);
52 1
        if (!isset($matches[1]) || !$endpoint->hasType($matches[1])) {
53
            $error = sprintf('Can`t resolve a valid object type for field "%s"', $refClass->getName());
54
            throw new \RuntimeException($error);
55
        }
56
        /** @var ObjectDefinitionInterface $objectDefinition */
57 1
        $objectDefinition = $endpoint->getType($matches[1]);
58
59 1
        if ($annotation->in || $annotation->notIn) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $annotation->in of type array 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...
Bug Best Practice introduced by
The expression $annotation->notIn of type array 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...
60
            if ($annotation->in && \in_array($objectDefinition->getName(), $annotation->in, true)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $annotation->in of type array 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...
61
                $objectDefinition->addField($field);
62
            }
63
64
            if ($annotation->notIn && !\in_array($objectDefinition->getName(), $annotation->notIn, true)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $annotation->notIn of type array 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...
65
                $objectDefinition->addField($field);
66
            }
67
        } else {
68 1
            $objectDefinition->addField($field);
69
        }
70
71 1
        $argAnnotations = $this->reader->getClassAnnotations($refClass);
72 1
        foreach ($argAnnotations as $argAnnotation) {
73 1
            if ($argAnnotation instanceof Annotation\Argument) {
74 1
                $this->resolveArgument($field, $argAnnotation);
75
            }
76
        }
77
78 1
        $field->setType(TypeUtil::normalize($annotation->type));
79 1
        $field->setList(TypeUtil::isTypeList($annotation->type));
80 1
        $field->setNonNull(TypeUtil::isTypeNonNull($annotation->type));
81 1
        $field->setNonNullList(TypeUtil::isTypeNonNullList($annotation->type));
82 1
        $field->setResolver($annotation->resolver ?? $refClass->getName());
83 1
        $field->setDeprecationReason($annotation->deprecationReason);
84 1
        $field->setDescription($annotation->description);
85 1
        $field->setComplexity($annotation->complexity);
86 1
        $field->setMaxConcurrentUsage($annotation->maxConcurrentUsage);
87
88 1
        foreach ($annotation->options as $option => $value) {
89 1
            $field->setMeta($option, $value);
90
        }
91
92 1
        if ($objectDefinition instanceof InterfaceDefinition) {
93 1
            $implementors = $objectDefinition->getImplementors();
94 1
            foreach ($implementors as $implementor) {
95 1
                $childType = $endpoint->getType($implementor);
96 1
                if ($childType instanceof FieldsAwareDefinitionInterface) {
97 1
                    if ($annotation->in || $annotation->notIn) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $annotation->in of type array 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...
Bug Best Practice introduced by
The expression $annotation->notIn of type array 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...
98
                        if ($annotation->in && \in_array($childType->getName(), $annotation->in, true)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $annotation->in of type array 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...
99
                            $childType->addField($field);
100
                        }
101
102
                        if ($annotation->notIn && !\in_array($childType->getName(), $annotation->notIn, true)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $annotation->notIn of type array 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...
103
                            $childType->addField($field);
104
                        }
105
                    } else {
106 1
                        $childType->addField($field);
107
                    }
108
                }
109
            }
110
        }
111 1
    }
112
}
113