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

AbstractObjectType::resolveFields()   C

Complexity

Conditions 10
Paths 79

Size

Total Lines 51
Code Lines 30

Duplication

Lines 23
Ratio 45.1 %

Code Coverage

Tests 27
CRAP Score 10.2146

Importance

Changes 0
Metric Value
cc 10
eloc 30
nc 79
nop 0
dl 23
loc 51
ccs 27
cts 31
cp 0.871
crap 10.2146
rs 6
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\Type;
12
13
use GraphQL\Type\Definition\ObjectType;
14
use GraphQL\Type\Definition\ResolveInfo;
15
use GraphQL\Type\Definition\Type;
16
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
17
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
18
use Ynlo\GraphQLBundle\Definition\ObjectDefinition;
19
use Ynlo\GraphQLBundle\Resolver\ObjectFieldResolver;
20
21
/**
22
 * Class AbstractObjectType
23
 */
24
abstract class AbstractObjectType extends ObjectType implements
25
    ContainerAwareInterface,
26
    DefinitionManagerAwareInterface
27
{
28
    use ContainerAwareTrait;
29
    use DefinitionManagerAwareTrait;
30
31
    /**
32
     * @var ObjectDefinition
33
     */
34
    protected $definition;
35
36
    /**
37
     * @param ObjectDefinition $definition
38
     */
39 14
    public function __construct(ObjectDefinition $definition)
40
    {
41 1
        $this->definition = $definition;
42
43 1
        parent::__construct(
44
            [
45 1
                'name' => $definition->getName(),
46 1
                'description' => $definition->getDescription(),
47 1
                'fields' => function () {
48 1
                    return $this->resolveFields();
49 1
                },
50 1
                'interfaces' => function () {
51 1
                    return $this->resolveInterfaces();
52 1
                },
53 14 View Code Duplication
                'resolveField' => function ($root, array $args, $context, ResolveInfo $resolveInfo) {
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...
54 14
                    $resolver = new ObjectFieldResolver($this->container, $this->manager, $this->definition);
55
56 14
                    return $resolver($root, $args, $context, $resolveInfo);
57 1
                },
58 14
                'isTypeOf' => function ($value, $context, ResolveInfo $info) {
0 ignored issues
show
Unused Code introduced by
The parameter $info is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

58
                'isTypeOf' => function ($value, $context, /** @scrutinizer ignore-unused */ ResolveInfo $info) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

58
                'isTypeOf' => function (/** @scrutinizer ignore-unused */ $value, $context, ResolveInfo $info) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

58
                'isTypeOf' => function ($value, /** @scrutinizer ignore-unused */ $context, ResolveInfo $info) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
59
                    //TODO: implement this
60 14
                },
61
            ]
62
        );
63 1
    }
64
65
    /**
66
     * @return array
67
     */
68 1
    private function resolveFields()
69
    {
70 1
        $fields = [];
71 1
        foreach ($this->definition->getFields() as $fieldDefinition) {
72 1
            $type = Types::get($fieldDefinition->getType());
73
74 1
            if ($fieldDefinition->isList()) {
75 1
                if ($fieldDefinition->isNonNullList()) {
76
                    $type = Type::nonNull($type);
77
                }
78 1
                $type = Type::listOf($type);
79
            }
80
81 1
            if ($fieldDefinition->isNonNull()) {
82 1
                $type = Type::nonNull($type);
83
            }
84
85 1
            $args = [];
86 1 View Code Duplication
            foreach ($fieldDefinition->getArguments() as $argument) {
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
                $argumentType = Types::get($argument->getType());
88
89 1
                if ($argument->isList()) {
90 1
                    if ($argument->isNonNullList()) {
91
                        $argumentType = Type::nonNull($type);
92
                    }
93 1
                    $argumentType = Type::listOf($argumentType);
94
                }
95
96 1
                if ($argument->isNonNull()) {
97
                    $argumentType = Type::nonNull($argumentType);
98
                }
99 1
                $arg['name'] = $argument->getName();
100 1
                $arg['type'] = $argumentType;
101 1
                $arg['description'] = $argument->getDescription();
102
103 1
                if ($argument->getDefaultValue() !== null) {
104
                    $arg['defaultValue'] = $argument->getDefaultValue();
105
                }
106
107 1
                $args[] = $arg;
108
            }
109
110 1
            $fields[$fieldDefinition->getName()] = [
111 1
                'type' => $type,
112 1
                'description' => $fieldDefinition->getDescription(),
113 1
                'deprecationReason' => $fieldDefinition->getDeprecationReason(),
114 1
                'args' => $args,
115
            ];
116
        }
117
118 1
        return $fields;
119
    }
120
121
    /**
122
     * @return array
123
     */
124 1
    private function resolveInterfaces()
125
    {
126 1
        $interfaces = [];
127 1
        foreach ($this->definition->getInterfaces() as $interface) {
128 1
            $interfaces[] = Types::get($interface);
129
        }
130
131 1
        return $interfaces;
132
    }
133
}
134