Passed
Push — master ( e52344...df819b )
by Rafael
09:21
created

InputObjectDefinitionType   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 7.58 %

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
wmc 7
dl 5
loc 66
c 0
b 0
f 0
ccs 26
cts 30
cp 0.8667
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B resolveFields() 0 28 6
A __construct() 4 15 1

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\Definition;
12
13
use GraphQL\Type\Definition\InputObjectType;
14
use GraphQL\Type\Definition\ObjectType;
15
use GraphQL\Type\Definition\ResolveInfo;
16
use GraphQL\Type\Definition\Type;
17
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
18
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
19
use Ynlo\GraphQLBundle\Definition\InputObjectDefinition;
20
use Ynlo\GraphQLBundle\Resolver\ObjectFieldResolver;
21
use Ynlo\GraphQLBundle\Type\Registry\TypeRegistry;
22
23
/**
24
 * Class InputObjectDefinitionType
25
 */
26
class InputObjectDefinitionType extends InputObjectType implements
27
    ContainerAwareInterface,
28
    EndpointAwareInterface
29
{
30
    use ContainerAwareTrait;
31
    use EndpointAwareTrait;
32
33
    /**
34
     * @var InputObjectDefinition
35
     */
36
    protected $definition;
37
38
    /**
39
     * @param InputObjectDefinition $definition
40
     */
41 1
    public function __construct(InputObjectDefinition $definition)
42
    {
43 1
        $this->definition = $definition;
44
45 1
        parent::__construct(
46
            [
47 1
                'name' => $definition->getName(),
48 1
                'description' => $definition->getDescription(),
49 1
                'fields' => function () {
50 1
                    return $this->resolveFields();
51 1
                },
52 1 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...
53
                    $resolver = new ObjectFieldResolver($this->container, $this->endpoint, $this->definition);
54
55
                    return $resolver($root, $args, $context, $resolveInfo);
56 1
                },
57
            ]
58
        );
59 1
    }
60
61
    /**
62
     * @return array
63
     */
64 1
    private function resolveFields()
65
    {
66 1
        $fields = [];
67 1
        foreach ($this->definition->getFields() as $fieldDefinition) {
68 1
            $type = TypeRegistry::get($fieldDefinition->getType());
69 1
            if ($type instanceof ObjectType) {
70
                $type = TypeRegistry::get($fieldDefinition->getType());
71
            }
72
73 1
            if ($fieldDefinition->isList()) {
74 1
                if ($fieldDefinition->isNonNullList()) {
75
                    $type = Type::nonNull($type);
76
                }
77 1
                $type = Type::listOf($type);
78
            }
79
80 1
            if ($fieldDefinition->isNonNull()) {
81 1
                $type = Type::nonNull($type);
82
            }
83
84 1
            $fields[$fieldDefinition->getName()] = [
85 1
                'type' => $type,
86 1
                'description' => $fieldDefinition->getDescription(),
87 1
                'deprecationReason' => $fieldDefinition->getDeprecationReason(),
88
            ];
89
        }
90
91 1
        return $fields;
92
    }
93
}
94