Completed
Push — master ( d52ee7...e7842c )
by Alexandr
03:54
created

QueryType   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 97.7%

Importance

Changes 8
Bugs 2 Features 0
Metric Value
wmc 25
c 8
b 2
f 0
lcom 1
cbo 10
dl 0
loc 145
rs 10
ccs 85
cts 87
cp 0.977

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
D build() 0 124 23
A isValidValue() 0 4 1
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\Field\Field;
11
use Youshido\GraphQL\Introspection\Field\SchemaField;
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\TypeMap;
20
use Youshido\GraphQL\Type\Union\AbstractUnionType;
21
22
class QueryType extends AbstractObjectType
23
{
24
25
    use TypeCollectorTrait;
26
27
    /**
28
     * @return String type name
29
     */
30 21
    public function getName()
31
    {
32 21
        return '__Type';
33
    }
34
35 7
    public function build($config)
36
    {
37
        $config
38 7
            ->addField('name', TypeMap::TYPE_STRING)
39 7
            ->addField('kind', TypeMap::TYPE_STRING)
40 7
            ->addField('description', TypeMap::TYPE_STRING)
41 7
            ->addField('ofType', [
42 7
                'type'    => new QueryType(),
43
                'resolve' => function (AbstractType $value) {
44 3
                    if ($value instanceof CompositeTypeInterface) {
45
                        return $value->getTypeOf();
46
                    }
47
48 3
                    return null;
49
                }
50 7
            ])
51 7
            ->addField(new Field([
52 7
                'name'    => 'inputFields',
53 7
                'type'    => new ListType(new InputValueType()),
54
                'resolve' => function ($value) {
55 3
                    if ($value instanceof AbstractInputObjectType) {
56
                        /** @var AbstractObjectType $value */
57
                        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...
58
                    }
59
60 3
                    return null;
61
                }
62 7
            ]))
63 7
            ->addField(new Field([
64 7
                'name'    => 'enumValues',
65 7
                'type'    => new ListType(new EnumValueType()),
66
                'resolve' => function ($value) {
67
                    /** @var $value AbstractType|AbstractEnumType */
68 2
                    if ($value && $value->getKind() == TypeMap::KIND_ENUM) {
69 2
                        $data = [];
70 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...
71 2
                            if (!array_key_exists('description', $enumValue)) {
72 2
                                $enumValue['description'] = '';
73 2
                            }
74 2
                            if (!array_key_exists('isDeprecated', $enumValue)) {
75 2
                                $enumValue['isDeprecated'] = false;
76 2
                            }
77 2
                            if (!array_key_exists('deprecationReason', $enumValue)) {
78 2
                                $enumValue['deprecationReason'] = '';
79 2
                            }
80
81 2
                            $data[] = $enumValue;
82 2
                        }
83
84 2
                        return $data;
85
                    }
86
87 2
                    return null;
88
                }
89 7
            ]))
90 7
            ->addField(new Field([
91 7
                'name'    => 'fields',
92 7
                'type'    => new ListType(new FieldType()),
93
                'resolve' => function ($value) {
94
                    /** @var AbstractType $value */
95 4
                    if (!$value ||
96 4
                        in_array($value->getKind(), [TypeMap::KIND_SCALAR, TypeMap::KIND_UNION, TypeMap::KIND_INPUT_OBJECT, TypeMap::KIND_ENUM])
97 4
                    ) {
98 3
                        return null;
99
                    }
100
101
                    /** @var AbstractObjectType $value */
102 4
                    $fields = $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...
103
104 4
                    foreach ($fields as $key => $field) {
105 4
                        if (in_array($field->getName(), ['__type', '__schema'])) {
106 4
                            unset($fields[$key]);
107 4
                        }
108 4
                    }
109
110 4
                    return $fields;
111
                }
112 7
            ]))
113 7
            ->addField(new Field([
114 7
                'name'    => 'interfaces',
115 7
                'type'    => new ListType(new QueryType()),
116
                'resolve' => function ($value) {
117
                    /** @var $value AbstractType */
118 3
                    if ($value->getKind() == TypeMap::KIND_OBJECT) {
119
                        /** @var $value AbstractObjectType */
120 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...
121
                    }
122
123 2
                    return null;
124
                }
125 7
            ]))
126 7
            ->addField('possibleTypes', [
127 7
                'type'    => new ListType(new QueryType()),
128 3
                'resolve' => function ($value) {
129
                    /** @var $value AbstractObjectType */
130 3
                    if ($value->getKind() == TypeMap::KIND_INTERFACE) {
131 2
                        $this->collectTypes(SchemaField::$schema->getQueryType());
132
133 2
                        $possibleTypes = [];
134 2
                        foreach ($this->types as $type) {
135
                            /** @var $type AbstractObjectType */
136 2
                            if ($type->getKind() == TypeMap::KIND_OBJECT) {
137 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...
138
139 2
                                if ($interfaces) {
140 2
                                    foreach ($interfaces as $interface) {
141 2
                                        if (get_class($interface) == get_class($value)) {
142 2
                                            $possibleTypes[] = $type;
143 2
                                        }
144 2
                                    }
145 2
                                }
146 2
                            }
147 2
                        }
148
149 2
                        return $possibleTypes ?: [];
150 3
                    } elseif ($value->getKind() == TypeMap::KIND_UNION) {
151
                        /** @var $value AbstractUnionType */
152 1
                        return $value->getTypes();
153
                    }
154
155 3
                    return null;
156
                }
157 7
            ]);
158 7
    }
159
160 6
    public function isValidValue($value)
161
    {
162 6
        return $value instanceof AbstractType;
163
    }
164
165
166
}
167