Completed
Push — master ( 4966ee...48a1e6 )
by Portey
10s
created

QueryType   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 14

Test Coverage

Coverage 98.08%

Importance

Changes 9
Bugs 2 Features 2
Metric Value
wmc 34
c 9
b 2
f 2
lcom 1
cbo 14
dl 0
loc 178
ccs 102
cts 104
cp 0.9808
rs 9.2

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A resolveInputFields() 0 9 2
A resolveOfType() 0 8 2
D resolveEnumValues() 0 28 10
B resolveFields() 0 19 6
A resolveInterfaces() 0 10 3
A build() 0 47 1
A isValidValue() 0 4 1
C resolvePossibleTypes() 0 30 8
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\Execution\ResolveInfo;
11
use Youshido\GraphQL\Field\Field;
12
use Youshido\GraphQL\Introspection\Traits\TypeCollectorTrait;
13
use Youshido\GraphQL\Type\AbstractType;
14
use Youshido\GraphQL\Type\CompositeTypeInterface;
15
use Youshido\GraphQL\Type\Enum\AbstractEnumType;
16
use Youshido\GraphQL\Type\InputObject\AbstractInputObjectType;
17
use Youshido\GraphQL\Type\ListType\ListType;
18
use Youshido\GraphQL\Type\Object\AbstractObjectType;
19
use Youshido\GraphQL\Type\Scalar\BooleanType;
20
use Youshido\GraphQL\Type\TypeMap;
21
use Youshido\GraphQL\Type\Union\AbstractUnionType;
22
23
class QueryType extends AbstractObjectType
24
{
25
26
    use TypeCollectorTrait;
27
28
    /**
29
     * @return String type name
30
     */
31 27
    public function getName()
32
    {
33 27
        return '__Type';
34
    }
35
36 3
    public function resolveOfType(AbstractType $value)
37
    {
38 3
        if ($value instanceof CompositeTypeInterface) {
39 2
            return $value->getTypeOf();
40
        }
41
42 3
        return null;
43
    }
44
45 3
    public function resolveInputFields($value)
46
    {
47 3
        if ($value instanceof AbstractInputObjectType) {
48
            /** @var AbstractObjectType $value */
49
            return $value->getConfig()->getFields();
0 ignored issues
show
Documentation Bug introduced by
The method getFields does not exist on object<Youshido\GraphQL\Config\AbstractConfig>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
50
        }
51
52 3
        return null;
53
    }
54
55 2
    public function resolveEnumValues($value, $args)
56
    {
57
        /** @var $value AbstractType|AbstractEnumType */
58 2
        if ($value && $value->getKind() == TypeMap::KIND_ENUM) {
59 2
            $data = [];
60 2
            foreach ($value->getValues() as $enumValue) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Youshido\GraphQL\Type\AbstractType as the method getValues() does only exist in the following sub-classes of Youshido\GraphQL\Type\AbstractType: Examples\Blog\Schema\PostStatus, Youshido\GraphQL\Type\Enum\AbstractEnumType, Youshido\GraphQL\Type\Enum\EnumType, Youshido\Tests\DataProvider\TestEnumType, Youshido\Tests\StarWars\Schema\EpisodeEnum. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
61 2
                if(!$args['includeDeprecated'] && (isset($enumValue['isDeprecated']) && $enumValue['isDeprecated'])) {
62
                    continue;
63
                }
64
65 2
                if (!array_key_exists('description', $enumValue)) {
66 2
                    $enumValue['description'] = '';
67 2
                }
68 2
                if (!array_key_exists('isDeprecated', $enumValue)) {
69 2
                    $enumValue['isDeprecated'] = false;
70 2
                }
71 2
                if (!array_key_exists('deprecationReason', $enumValue)) {
72 2
                    $enumValue['deprecationReason'] = '';
73 2
                }
74
75 2
                $data[] = $enumValue;
76 2
            }
77
78 2
            return $data;
79
        }
80
81 2
        return null;
82
    }
83
84 4
    public function resolveFields($value, $args)
85
    {
86
        /** @var AbstractType $value */
87 4
        if (!$value ||
88 4
            in_array($value->getKind(), [TypeMap::KIND_SCALAR, TypeMap::KIND_UNION, TypeMap::KIND_INPUT_OBJECT, TypeMap::KIND_ENUM])
89 4
        ) {
90 3
            return null;
91
        }
92
93
        /** @var AbstractObjectType $value */
94 4
        return array_filter($value->getConfig()->getFields(), function ($field) use ($args) {
0 ignored issues
show
Documentation Bug introduced by
The method getFields does not exist on object<Youshido\GraphQL\Config\AbstractConfig>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
95
            /** @var $field Field */
96 4
            if (in_array($field->getName(), ['__type', '__schema']) || (!$args['includeDeprecated'] && $field->isDeprecated())) {
97 4
                return false;
98
            }
99
100 4
            return true;
101 4
        });
102
    }
103
104 3
    public function resolveInterfaces($value)
105
    {
106
        /** @var $value AbstractType */
107 3
        if ($value->getKind() == TypeMap::KIND_OBJECT) {
108
            /** @var $value AbstractObjectType */
109 3
            return $value->getConfig()->getInterfaces() ?: [];
0 ignored issues
show
Documentation Bug introduced by
The method getInterfaces does not exist on object<Youshido\GraphQL\Config\AbstractConfig>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
110
        }
111
112 2
        return null;
113
    }
114
115 3
    public function resolvePossibleTypes($value, $args, ResolveInfo $info)
116
    {
117
        /** @var $value AbstractObjectType */
118 3
        if ($value->getKind() == TypeMap::KIND_INTERFACE) {
119 2
            $this->collectTypes($info->getExecutionContext()->getSchema()->getQueryType());
120
121 2
            $possibleTypes = [];
122 2
            foreach ($this->types as $type) {
123
                /** @var $type AbstractObjectType */
124 2
                if ($type->getKind() == TypeMap::KIND_OBJECT) {
125 2
                    $interfaces = $type->getConfig()->getInterfaces();
0 ignored issues
show
Documentation Bug introduced by
The method getInterfaces does not exist on object<Youshido\GraphQL\Config\AbstractConfig>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
126
127 2
                    if ($interfaces) {
128 2
                        foreach ($interfaces as $interface) {
129 2
                            if ($interface->getName() == $value->getName()) {
130 2
                                $possibleTypes[] = $type;
131 2
                            }
132 2
                        }
133 2
                    }
134 2
                }
135 2
            }
136
137 2
            return $possibleTypes;
138 3
        } elseif ($value->getKind() == TypeMap::KIND_UNION) {
139
            /** @var $value AbstractUnionType */
140 1
            return $value->getTypes();
141
        }
142
143 3
        return null;
144
    }
145
146 7
    public function build($config)
147
    {
148
        $config
149 7
            ->addField('name', TypeMap::TYPE_STRING)
150 7
            ->addField('kind', TypeMap::TYPE_STRING)
151 7
            ->addField('description', TypeMap::TYPE_STRING)
152 7
            ->addField('ofType', [
153 7
                'type'    => new QueryType(),
154 7
                'resolve' => [$this, 'resolveOfType']
155 7
            ])
156 7
            ->addField(new Field([
157 7
                'name'    => 'inputFields',
158 7
                'type'    => new ListType(new InputValueType()),
159 7
                'resolve' => [$this, 'resolveInputFields']
160 7
            ]))
161 7
            ->addField(new Field([
162 7
                'name'    => 'enumValues',
163
                'args'    => [
164
                    'includeDeprecated' => [
165 7
                        'type'    => new BooleanType(),
166
                        'default' => false
167 7
                    ]
168 7
                ],
169 7
                'type'    => new ListType(new EnumValueType()),
170 7
                'resolve' => [$this, 'resolveEnumValues']
171 7
            ]))
172 7
            ->addField(new Field([
173 7
                'name'    => 'fields',
174
                'args'    => [
175
                    'includeDeprecated' => [
176 7
                        'type'    => new BooleanType(),
177
                        'default' => false
178 7
                    ]
179 7
                ],
180 7
                'type'    => new ListType(new FieldType()),
181 7
                'resolve' => [$this, 'resolveFields']
182 7
            ]))
183 7
            ->addField(new Field([
184 7
                'name'    => 'interfaces',
185 7
                'type'    => new ListType(new QueryType()),
186 7
                'resolve' => [$this, 'resolveInterfaces']
187 7
            ]))
188 7
            ->addField('possibleTypes', [
189 7
                'type'    => new ListType(new QueryType()),
190 7
                'resolve' => [$this, 'resolvePossibleTypes']
191 7
            ]);
192 7
    }
193
194 6
    public function isValidValue($value)
195
    {
196 6
        return $value instanceof AbstractType;
197
    }
198
199
200
}
201