InputValueType   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 5
dl 0
loc 51
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveType() 0 4 1
A resolveDefaultValue() 0 5 2
A getName() 0 4 1
A build() 0 17 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\Schema\AbstractSchema;
12
use Youshido\GraphQL\Type\NonNullType;
13
use Youshido\GraphQL\Type\Object\AbstractObjectType;
14
use Youshido\GraphQL\Type\TypeInterface;
15
use Youshido\GraphQL\Type\TypeMap;
16
17
class InputValueType extends AbstractObjectType
18
{
19
    /**
20
     * @param AbstractSchema|Field $value
21
     *
22
     * @return TypeInterface
23
     */
24 5
    public function resolveType($value)
25
    {
26 5
        return $value->getConfig()->getType();
0 ignored issues
show
Bug introduced by
The method getConfig does only exist in Youshido\GraphQL\Field\Field, but not in Youshido\GraphQL\Schema\AbstractSchema.

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...
27
    }
28
29
    /**
30
     * @param AbstractSchema|Field $value
31
     *
32
     * @return string|null
33
     *
34
     * //todo implement value printer
35
     */
36 6
    public function resolveDefaultValue($value)
37
    {
38 6
        $resolvedValue = $value->getConfig()->getDefaultValue();
0 ignored issues
show
Bug introduced by
The method getConfig does only exist in Youshido\GraphQL\Field\Field, but not in Youshido\GraphQL\Schema\AbstractSchema.

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 6
        return $resolvedValue === null ? $resolvedValue : str_replace('"', '', json_encode($resolvedValue));
40
    }
41
42 9
    public function build($config)
43
    {
44
        $config
45 9
            ->addField('name', new NonNullType(TypeMap::TYPE_STRING))
46 9
            ->addField('description', TypeMap::TYPE_STRING)
47 9
            ->addField('isDeprecated', new NonNullType(TypeMap::TYPE_BOOLEAN))
48 9
            ->addField('deprecationReason', TypeMap::TYPE_STRING)
49 9
            ->addField(new Field([
50 9
                'name'    => 'type',
51 9
                'type'    => new NonNullType(new QueryType()),
52 9
                'resolve' => [$this, 'resolveType']
53 9
            ]))
54 9
            ->addField('defaultValue', [
55 9
                'type' => TypeMap::TYPE_STRING,
56 9
                'resolve' => [$this, 'resolveDefaultValue']
57 9
            ]);
58 9
    }
59
60
    /**
61
     * @return string type name
62
     */
63 11
    public function getName()
64
    {
65 11
        return '__InputValue';
66
    }
67
}
68