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

AbstractObjectType   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 108
Duplicated Lines 25.93 %

Test Coverage

Coverage 92.59%

Importance

Changes 0
Metric Value
dl 28
loc 108
ccs 50
cts 54
cp 0.9259
rs 10
c 0
b 0
f 0
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
C resolveFields() 23 51 10
A __construct() 5 20 1
A resolveInterfaces() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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