Schema::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
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) {
148 153
            return $map;
149
        }
150
151 153
        if (!empty($map[$type->getName()])) {
152 153
            if ($type instanceof ObjectType || $type instanceof InterfaceType) {
153 48
                $type->setFields($map[$type->getName()]->getFields());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Fubhy\GraphQL\Type\Definition\Types\TypeInterface as the method getFields() does only exist in the following implementations of said interface: Fubhy\GraphQL\Type\Defin...n\Types\InputObjectType, Fubhy\GraphQL\Type\Definition\Types\InterfaceType, Fubhy\GraphQL\Type\Definition\Types\ObjectType.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
154 48
            }
155 48
156 48
            return $map;
157
        }
158 153
159 153
        $reducedMap = array_merge($map, [$type->getName() => $type]);
160 153
        if ($type instanceof InterfaceType || $type instanceof UnionType) {
161 153
            $reducedMap = array_reduce(
162 153
                $type->getPossibleTypes(), [$this, 'typeMapReducer'], $reducedMap
163
            );
164 153
        }
165 153
166 153
        if ($type instanceof ObjectType) {
167 153
            $reducedMap = array_reduce(
168 153
                $type->getInterfaces(), [$this, 'typeMapReducer'], $reducedMap
169 153
            );
170
        }
171 153
172 153
        if ($type instanceof ObjectType || $type instanceof InterfaceType) {
173 153
            foreach ($type->getFields() as $fieldName => $field) {
174
                $args = $field->getArguments();
175 153
                $reducedMap = array_reduce(array_map(function (FieldArgument $arg) {
176
                    return $arg->getType();
177
                }, $args), [$this, 'typeMapReducer'], $reducedMap);
178
179
                $reducedMap = $this->typeMapReducer($reducedMap, $field->getType());
180
            }
181
        }
182
183
        return $reducedMap;
184
    }
185
186
    /**
187
     * Re-populate static properties when de-serializing.
188
     */
189
    public function __wakeup() {
190
        Type::intType();
191
        Type::booleanType();
192
        Type::floatType();
193
        Type::idType();
194
        Type::stringType();
195
    }
196
}
197