AbstractInputField::build()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Date: 13.05.16
4
 *
5
 * @author Portey Vasil <[email protected]>
6
 */
7
8
namespace Youshido\GraphQL\Field;
9
10
11
use Youshido\GraphQL\Config\Field\InputFieldConfig;
12
use Youshido\GraphQL\Type\InputTypeInterface;
13
use Youshido\GraphQL\Type\Traits\AutoNameTrait;
14
use Youshido\GraphQL\Type\Traits\FieldsArgumentsAwareObjectTrait;
15
use Youshido\GraphQL\Type\TypeFactory;
16
use Youshido\GraphQL\Type\TypeService;
17
18
abstract class AbstractInputField implements InputFieldInterface
19
{
20
21
    use FieldsArgumentsAwareObjectTrait, AutoNameTrait;
22
23
    protected $isFinal = false;
24
25 84
    public function __construct(array $config = [])
26
    {
27 84
        if (empty($config['type'])) {
28 1
            $config['type'] = $this->getType();
29 1
            $config['name'] = $this->getName();
30 1
        }
31
32 84
        if (TypeService::isScalarType($config['type'])) {
33 48
            $config['type'] = TypeFactory::getScalarType($config['type']);
34 48
        }
35
36 84
        $this->config = new InputFieldConfig($config, $this, $this->isFinal);
37 84
        $this->build($this->config);
38 84
    }
39
40 84
    public function build(InputFieldConfig $config)
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
    {
42
43 84
    }
44
45
    /**
46
     * @return InputTypeInterface
47
     */
48
    abstract public function getType();
49
50 1
    public function getDefaultValue()
51
    {
52 1
        return $this->config->getDefaultValue();
0 ignored issues
show
Bug introduced by
The method getDefaultValue does only exist in Youshido\GraphQL\Config\Field\InputFieldConfig, but not in Youshido\GraphQL\Config\...Object\ObjectTypeConfig.

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...
53
    }
54
55
    //todo: think about serialize, parseValue methods
56
57
}
58