Completed
Pull Request — master (#31)
by Sebastian
06:26 queued 02:32
created

SchemaType::resolveMutationType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 2
cts 2
cp 1
rs 9.4285
cc 2
eloc 2
nc 2
nop 1
crap 2
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\TypesField;
12
use Youshido\GraphQL\Schema\AbstractSchema;
13
use Youshido\GraphQL\Type\ListType\ListType;
14
use Youshido\GraphQL\Type\Object\AbstractObjectType;
15
use Youshido\GraphQL\Type\Object\ObjectType;
16
use Youshido\GraphQL\Type\TypeMap;
17
18
class SchemaType extends AbstractObjectType
19
{
20
21
    /**
22
     * @return String type name
23
     */
24 22
    public function getName()
25
    {
26 22
        return '__Schema';
27
    }
28
29 4
    public static function resolveQueryType($value)
30
    {
31
        /** @var AbstractSchema|Field $value */
32 4
        return $value->getQueryType();
0 ignored issues
show
Bug introduced by
The method getQueryType does only exist in Youshido\GraphQL\Schema\AbstractSchema, but not in Youshido\GraphQL\Field\Field.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
33
    }
34
35 2
    public static function resolveMutationType($value)
36
    {
37
        /** @var AbstractSchema|Field $value */
38 2
        return $value->getMutationType()->hasFields() ? $value->getMutationType() : null;
0 ignored issues
show
Bug introduced by
The method getMutationType does only exist in Youshido\GraphQL\Schema\AbstractSchema, but not in Youshido\GraphQL\Field\Field.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
39
    }
40
41 1
    public static function resolveSubscriptionType() {
42 1
        return null;
43
    }
44
45 8
    public function build($config)
46
    {
47
        $config
48 8
            ->addField(new Field([
49 8
                'name'    => 'queryType',
50 8
                'type'    => new QueryType(),
51 8
                'resolve' => [get_class($this), 'resolveQueryType']
52
            ]))
53 8
            ->addField(new Field([
54 8
                'name'    => 'mutationType',
55 8
                'type'    => new QueryType(),
56 8
                'resolve' => [get_class($this), 'resolveMutationType']
57
            ]))
58 8
            ->addField(new Field([
59 8
                'name'    => 'subscriptionType',
60 8
                'type'    => new ObjectType([
61 8
                    'name'   => '__Subscription',
62
                    'fields' => [
63
                        'name' => ['type' => TypeMap::TYPE_STRING]
64
                    ]
65
                ]),
66 8
                'resolve' => [get_class($this), 'resolveSubscriptionType']
67
            ]))
68 8
            ->addField(new TypesField())
69 8
            ->addField(new Field([
70 8
                'name' => 'directives',
71 8
                'type' => new ListType(new DirectiveType())
72
            ]));
73 8
    }
74
}
75