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

InterfaceDefinitionType   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 36.23 %

Test Coverage

Coverage 93.55%

Importance

Changes 0
Metric Value
wmc 8
dl 25
loc 69
c 0
b 0
f 0
ccs 29
cts 31
cp 0.9355
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B resolveFields() 25 25 5
A __construct() 0 21 3

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 Doctrine\Common\Util\ClassUtils;
14
use GraphQL\Type\Definition\InterfaceType;
15
use GraphQL\Type\Definition\Type;
16
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
17
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
18
use Ynlo\GraphQLBundle\Definition\InterfaceDefinition;
19
use Ynlo\GraphQLBundle\Type\Registry\TypeRegistry;
20
21
/**
22
 * Class InterfaceDefinitionType
23
 */
24
class InterfaceDefinitionType extends InterfaceType implements EndpointAwareInterface, ContainerAwareInterface
25
{
26
    use ContainerAwareTrait;
27
    use EndpointAwareTrait;
28
29
    /**
30
     * @var InterfaceDefinition
31
     */
32
    protected $definition;
33
34
    /**
35
     * InterfaceDefinitionType constructor.
36
     *
37
     * @param InterfaceDefinition $definition
38
     */
39 6
    public function __construct(InterfaceDefinition $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 6
                'resolveType' => function ($value) {
51 6
                    foreach ($this->definition->getImplementors() as $implementor) {
52 6
                        $implementorDef = $this->endpoint->getType($implementor);
53
                        //ClassUtils::getClass is required to avoid proxies
54 6
                        if ($implementorDef->getClass() === ClassUtils::getClass($value)) {
0 ignored issues
show
Bug introduced by
The method getClass() does not exist on Ynlo\GraphQLBundle\Definition\DefinitionInterface. It seems like you code against a sub-type of Ynlo\GraphQLBundle\Definition\DefinitionInterface such as Ynlo\GraphQLBundle\Defin...jectDefinitionInterface or Ynlo\GraphQLBundle\Definition\EnumDefinition or Ynlo\GraphQLBundle\Definition\ObjectDefinition or Ynlo\GraphQLBundle\Defin...jectDefinitionInterface or Ynlo\GraphQLBundle\Definition\ObjectDefinition. ( Ignorable by Annotation )

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

54
                        if ($implementorDef->/** @scrutinizer ignore-call */ getClass() === ClassUtils::getClass($value)) {
Loading history...
55 6
                            return TypeRegistry::get($implementorDef->getName());
56
                        }
57
                    }
58
59
                    return null;
60 1
                },
61
            ]
62
        );
63 1
    }
64
65
    /**
66
     * @return array
67
     */
68 1 View Code Duplication
    private function resolveFields()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
69
    {
70 1
        $fields = [];
71 1
        foreach ($this->definition->getFields() as $fieldDefinition) {
72 1
            $type = TypeRegistry::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
            $fields[$fieldDefinition->getName()] = [
86 1
                'type' => $type,
87 1
                'description' => $fieldDefinition->getDescription(),
88 1
                'deprecationReason' => $fieldDefinition->getDeprecationReason(),
89
            ];
90
        }
91
92 1
        return $fields;
93
    }
94
}
95