Completed
Pull Request — master (#155)
by
unknown
02:47
created

AbstractInputObjectType::isValidValue()   C

Complexity

Conditions 11
Paths 22

Size

Total Lines 42
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 11.0699

Importance

Changes 0
Metric Value
dl 0
loc 42
ccs 22
cts 24
cp 0.9167
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 23
nc 22
nop 1
crap 11.0699

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
* This file is a part of graphql-youshido project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 12/2/15 9:00 PM
7
*/
8
9
namespace Youshido\GraphQL\Type\InputObject;
10
11
12
use Youshido\GraphQL\Config\Object\InputObjectTypeConfig;
13
use Youshido\GraphQL\Field\FieldInterface;
14
use Youshido\GraphQL\Parser\Ast\ArgumentValue\InputObject;
15
use Youshido\GraphQL\Parser\Ast\ArgumentValue\Variable;
16
use Youshido\GraphQL\Type\AbstractType;
17
use Youshido\GraphQL\Type\Traits\AutoNameTrait;
18
use Youshido\GraphQL\Type\Traits\FieldsAwareObjectTrait;
19
use Youshido\GraphQL\Type\TypeMap;
20
21
abstract class AbstractInputObjectType extends AbstractType
22
{
23
24
    use AutoNameTrait, FieldsAwareObjectTrait;
25
26
    protected $isBuilt = false;
27
28 7
    public function getConfig()
29
    {
30 7
        if (!$this->isBuilt) {
31 7
            $this->isBuilt = true;
32 7
            $this->build($this->config);
0 ignored issues
show
Documentation introduced by
$this->config is of type object<Youshido\GraphQL\...Field\InputFieldConfig>, but the function expects a object<Youshido\GraphQL\...\InputObjectTypeConfig>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
33
        }
34
35 7
        return $this->config;
36
    }
37
38 1
    public function __construct($config = [])
39
    {
40 1
        if (empty($config)) {
41
            $config = [
42 1
                'name' => $this->getName()
43
            ];
44
        }
45 1
        $this->config = new InputObjectTypeConfig($config, $this);
46 1
    }
47
48
    /**
49
     * @param InputObjectTypeConfig $config
50
     */
51
    abstract public function build($config);
52
53 7
    public function isValidValue($value)
54
    {
55 7
        if ($value instanceof InputObject) {
56
            $value = $value->getValue();
57
        }
58
59 7
        if (empty($value)) {
60 1
            return true;
61
        }
62
63 6
        if (!is_array($value)) {
64 1
            return false;
65
        }
66
67 6
        $typeConfig     = $this->getConfig();
68 6
        $requiredFields = array_filter($typeConfig->getFields(), function (FieldInterface $field) {
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...
69 6
            return $field->getType()->getKind() == TypeMap::KIND_NON_NULL;
70 6
        });
71
72 6
        foreach ($value as $valueKey => $valueItem) {
73 6
            if (!$typeConfig->hasField($valueKey)) {
0 ignored issues
show
Documentation Bug introduced by
The method hasField 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...
74
                // Schema validation will generate the error message for us.
75
                return false;
76
            }
77
78 6
            $field = $typeConfig->getField($valueKey);
0 ignored issues
show
Documentation Bug introduced by
The method getField 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...
79 6
            if (!$field->getType()->isValidValue($valueItem)) {
80 3
                $error = $field->getType()->getLastError() ?: '(no details available)';
81 3
                $this->lastError = sprintf('Not valid type for field "%s" in input type "%s": %s', $field->getName(), $this->getName(), $error);
82 3
                return false;
83
            }
84
85 5
            if (array_key_exists($valueKey, $requiredFields)) {
86 5
                unset($requiredFields[$valueKey]);
87
            }
88
        }
89 5
        if (count($requiredFields)) {
90 1
            $this->lastError = sprintf('%s %s required on %s', implode(', ', array_keys($requiredFields)), count($requiredFields) > 1 ? 'are' : 'is', $typeConfig->getName());
91
        }
92
93 5
        return !(count($requiredFields) > 0);
94
    }
95
96 8
    public function getKind()
97
    {
98 8
        return TypeMap::KIND_INPUT_OBJECT;
99
    }
100
101 2
    public function isInputType()
102
    {
103 2
        return true;
104
    }
105
106 4
    public function parseValue($value)
107
    {
108 4
        if (is_null($value)) return null;
109 4
        if($value instanceof InputObject) {
110
            $value = $value->getValue();
111
        }
112
113 4
        $typeConfig = $this->getConfig();
114 4
        foreach ($value as $valueKey => $item) {
115 3
            if ($item instanceof Variable) {
116
                $item = $item->getValue();
117
            }
118
119 3
            if (!($inputField = $typeConfig->getField($valueKey))) {
0 ignored issues
show
Documentation Bug introduced by
The method getField 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...
120
                throw new \Exception(sprintf('Invalid field "%s" on %s', $valueKey, $typeConfig->getName()));
121
            }
122 3
            $value[$valueKey] = $inputField->getType()->parseValue($item);
123
        }
124
125 4
        return $value;
126
    }
127
128
}
129