Completed
Pull Request — master (#44)
by Daniel
06:29
created

InputValueType::resolveDefaultValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 3
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\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 2
    public function resolveType($value)
25
    {
26 2
        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 3
    public function resolveDefaultValue($value)
35
    {
36 3
        $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...
37
38 3
        return $resolvedValue === null ? $resolvedValue : json_encode($resolvedValue);
39
    }
40
41 5 View Code Duplication
    public function build($config)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43
        $config
44 5
            ->addField('name', new NonNullType(TypeMap::TYPE_STRING))
45 5
            ->addField('description', TypeMap::TYPE_STRING)
46 5
            ->addField(new Field([
47 5
                'name'    => 'type',
48 5
                'type'    => new NonNullType(new QueryType()),
49 5
                'resolve' => [$this, 'resolveType']
50
            ]))
51 5
            ->addField('defaultValue', [
52 5
                'type' => TypeMap::TYPE_STRING,
53 5
                'resolve' => [$this, 'resolveDefaultValue']
54
            ]);
55 5
    }
56
57
    /**
58
     * @return string type name
59
     */
60 7
    public function getName()
61
    {
62 7
        return '__InputValue';
63
    }
64
}
65