Completed
Push — master ( 2555c1...347de5 )
by Sebastian
03:02
created

Schema::typeMapReducer()   D

Complexity

Conditions 10
Paths 10

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 10

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 36
ccs 26
cts 26
cp 1
rs 4.8196
cc 10
eloc 20
nc 10
nop 2
crap 10

How to fix   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
namespace Fubhy\GraphQL;
4
5
use Fubhy\GraphQL\Type\Definition\FieldArgument;
6
use Fubhy\GraphQL\Type\Definition\Types\InterfaceType;
7
use Fubhy\GraphQL\Type\Definition\Types\ModifierInterface;
8
use Fubhy\GraphQL\Type\Definition\Types\ObjectType;
9
use Fubhy\GraphQL\Type\Definition\Types\Type;
10
use Fubhy\GraphQL\Type\Definition\Types\TypeInterface;
11
use Fubhy\GraphQL\Type\Definition\Types\UnionType;
12
use Fubhy\GraphQL\Type\Directives\Directive;
13
use Fubhy\GraphQL\Type\Introspection;
14
15
/**
16
 * Schema Definition
17
 *
18
 * A Schema is created by supplying the root types of each type of operation,
19
 * query and mutation (optional). A schema definition is then supplied to the
20
 * validator and executor.
21
 */
22
class Schema
23
{
24
    /**
25
     * @var \Fubhy\GraphQL\Type\Definition\Types\ObjectType|null
26
     */
27
    protected $mutationType;
28
29
    /**
30
     * @var \Fubhy\GraphQL\Type\Definition\Types\ObjectType
31
     */
32
    protected $queryType;
33
34
    /**
35
     * @var \Fubhy\GraphQL\Type\Directives\DirectiveInterface[]
36
     */
37
    protected $directives;
38
39
    /**
40
     * @var \Fubhy\GraphQL\Type\Definition\Types\TypeInterface[]
41
     */
42
    protected $typeMap;
43
44
    /**
45
     * Constructor.
46
     *
47
     * @param \Fubhy\GraphQL\Type\Definition\Types\TypeInterface $queryType
48
     * @param \Fubhy\GraphQL\Type\Definition\Types\TypeInterface|null $mutationType
49
     */
50 309
    public function __construct(TypeInterface $queryType, TypeInterface $mutationType = NULL)
51
    {
52 309
        $this->queryType = $queryType;
0 ignored issues
show
Documentation Bug introduced by
$queryType is of type object<Fubhy\GraphQL\Typ...on\Types\TypeInterface>, but the property $queryType was declared to be of type object<Fubhy\GraphQL\Typ...ition\Types\ObjectType>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
53 309
        $this->mutationType = $mutationType;
0 ignored issues
show
Documentation Bug introduced by
It seems like $mutationType can also be of type object<Fubhy\GraphQL\Typ...on\Types\TypeInterface>. However, the property $mutationType is declared as type object<Fubhy\GraphQL\Typ...\Types\ObjectType>|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
54 309
    }
55
56
    /**
57
     * @return \Fubhy\GraphQL\Type\Definition\Types\ObjectType
58
     */
59 297
    public function getQueryType()
60
    {
61 297
        return $this->queryType;
62
    }
63
64
    /**
65
     * @return \Fubhy\GraphQL\Type\Definition\Types\ObjectType|null
66
     */
67 162
    public function getMutationType()
68
    {
69 162
        return $this->mutationType;
70
    }
71
72
    /**
73
     * @param string $name
74
     *
75
     * @return \Fubhy\GraphQL\Type\Directives\DirectiveInterface|null
76
     */
77 3
    public function getDirective($name)
78
    {
79
        foreach ($this->getDirectives() as $directive) {
80
            if ($directive->getName() === $name) {
81 3
                return $directive;
82
            }
83
        }
84
        return NULL;
85
    }
86
87
    /**
88
     * @return \Fubhy\GraphQL\Type\Directives\DirectiveInterface[]
89
     */
90 3
    public function getDirectives()
91
    {
92 3
        if (!isset($this->directives)) {
93 3
            $include = Directive::includeDirective();
94 3
            $skip = Directive::skipDirective();
95
96 3
            $this->directives = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array($include->getName(...ip->getName() => $skip) of type array<string,object<Fubh...es\DirectiveInterface>> is incompatible with the declared type array<integer,object<Fub...es\DirectiveInterface>> of property $directives.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
97 3
                $include->getName() => $include,
98 3
                $skip->getName() => $skip,
99
            ];
100 3
        }
101
102 3
        return $this->directives;
103
    }
104
105
    /**
106
     * @return \Fubhy\GraphQL\Type\Definition\Types\TypeInterface[]
107
     */
108 153
    public function getTypeMap()
109
    {
110 153
        if (!isset($this->typeMap)) {
111 153
            $input = [$this->getQueryType(), $this->getMutationType(), Introspection::schema()];
112 153
            $this->typeMap = array_reduce($input, [$this, 'typeMapReducer'], []);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_reduce($input, arr...eMapReducer'), array()) of type * is incompatible with the declared type array<integer,object<Fub...n\Types\TypeInterface>> of property $typeMap.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
113
114 153
            $this->typeMap['Boolean'] = Type::booleanType();
115 153
            $this->typeMap['Float'] = Type::floatType();
116 153
            $this->typeMap['Id'] = Type::idType();
117 153
            $this->typeMap['Integer'] = Type::intType();
118 153
            $this->typeMap['String'] = Type::stringType();
119 153
        }
120
121 153
        return $this->typeMap;
122
    }
123
124
    /**
125
     * @param string $name
126
     *
127
     * @return \Fubhy\GraphQL\Type\Definition\Types\TypeInterface
128
     */
129 147
    public function getType($name)
130
    {
131 147
        $map = $this->getTypeMap();
132 147
        return isset($map[$name]) ? $map[$name] : NULL;
133
    }
134
135
    /**
136
     * @param \Fubhy\GraphQL\Type\Definition\Types\TypeInterface[] $map
137
     * @param mixed $type
138
     *
139
     * @return \Fubhy\GraphQL\Type\Definition\Types\TypeInterface[]
140
     */
141 153
    protected function typeMapReducer(array $map, $type)
142
    {
143 153
        if ($type instanceof ModifierInterface) {
144 153
            return $this->typeMapReducer($map, $type->getWrappedType());
145
        }
146
147 153
        if (!$type instanceof TypeInterface || !empty($map[$type->getName()])) {
148 153
            return $map;
149
        }
150
151 153
        $reducedMap = array_merge($map, [$type->getName() => $type]);
152 153
        if ($type instanceof InterfaceType || $type instanceof UnionType) {
153 48
            $reducedMap = array_reduce(
154 48
                $type->getPossibleTypes(), [$this, 'typeMapReducer'], $reducedMap
155 48
            );
156 48
        }
157
158 153
        if ($type instanceof ObjectType) {
159 153
            $reducedMap = array_reduce(
160 153
                $type->getInterfaces(), [$this, 'typeMapReducer'], $reducedMap
161 153
            );
162 153
        }
163
164 153
        if ($type instanceof ObjectType || $type instanceof InterfaceType) {
165 153
            foreach ($type->getFields() as $fieldName => $field) {
166 153
                $args = $field->getArguments();
167 153
                $reducedMap = array_reduce(array_map(function (FieldArgument $arg) {
168 153
                    return $arg->getType();
169 153
                }, $args), [$this, 'typeMapReducer'], $reducedMap);
170
171 153
                $reducedMap = $this->typeMapReducer($reducedMap, $field->getType());
172 153
            }
173 153
        }
174
175 153
        return $reducedMap;
176
    }
177
}
178