Completed
Push — master ( 3a14e9...fe7442 )
by Portey
04:17
created

QueryType::build()   C

Complexity

Conditions 11
Paths 1

Size

Total Lines 52
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 20.0353

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 52
ccs 22
cts 38
cp 0.5789
rs 6
cc 11
eloc 31
nc 1
nop 1
crap 20.0353

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
 * Date: 03.12.15
4
 *
5
 * @author Portey Vasil <[email protected]>
6
 */
7
8
namespace Youshido\GraphQL\Introspection;
9
10
use Youshido\GraphQL\Introspection\Traits\TypeCollectorTrait;
11
use Youshido\GraphQL\Schema;
12
use Youshido\GraphQL\Type\Config\TypeConfigInterface;
13
use Youshido\GraphQL\Type\Field\Field;
14
use Youshido\GraphQL\Type\ListType\ListType;
15
use Youshido\GraphQL\Type\Object\AbstractObjectType;
16
use Youshido\GraphQL\Type\TypeInterface;
17
use Youshido\GraphQL\Type\TypeMap;
18
19
class QueryType extends AbstractObjectType
20
{
21
    use TypeCollectorTrait;
22
23 3
    public function resolve($value = null, $args = [])
24
    {
25
        /** @var Schema|Field $value */
26 3
        if ($value instanceof Schema) {
27 3
            return $value->getQueryType();
28
        }
29
30
        return $value->getConfig()->getType();
31
    }
32
33
    /**
34
     * @return String type name
35
     */
36 6
    public function getName()
37
    {
38 6
        return '__Type';
39
    }
40
41 6
    protected function build(TypeConfigInterface $config)
42
    {
43
        $config
44 6
            ->addField('name', TypeMap::TYPE_STRING)
45 6
            ->addField('kind', TypeMap::TYPE_STRING)
46 6
            ->addField('description', TypeMap::TYPE_STRING)
47 6
            ->addField('ofType', new QueryType(), [
48
                'resolve' => function ($value) {
49 2
                    if ($value->getKind() == TypeMap::KIND_LIST) {
50 1
                        return $value->getConfig()->getItem();
51
                    }
52
53 2
                    return null;
54
                }
55 6
            ])
56 6
            ->addField('inputFields', new InputValueListType())
57 6
            ->addField('enumValues', new EnumValueListType())
58 6
            ->addField('fields', new FieldListType())
59 6
            ->addField('interfaces', new InterfaceListType())
60 6
            ->addField('possibleTypes', new ListType(new QueryType()), [
61 2
                'resolve' => function ($value) {
62 2
                    if ($value) {
63 2
                        if ($value->getKind() == TypeMap::KIND_INTERFACE) {
64
                            $this->collectTypes(SchemaType::$schema->getQueryType());
65
66
                            $possibleTypes = [];
67
                            foreach ($this->types as $type) {
68
                                /** @var $type TypeInterface */
69
                                if ($type->getKind() == TypeMap::KIND_OBJECT) {
70
                                    $interfaces = $type->getConfig()->getInterfaces();
71
72
                                    if ($interfaces) {
73
                                        foreach ($interfaces as $interface) {
74
                                            if (get_class($interface) == get_class($value)) {
75
                                                $possibleTypes[] = $type;
76
                                            }
77
                                        }
78
                                    }
79
                                }
80
                            }
81
82
                            return $possibleTypes ?: [];
83 2
                        } elseif ($value->getKind() == TypeMap::KIND_UNION) {
84
                            return $value->getTypes();
85
                        }
86
87 2
                    }
88
89 2
                    return null;
90
                }
91 6
            ]);
92
    }
93
}