Completed
Push — master ( f8702a...461e07 )
by Alexandr
04:08
created

ArgumentsAwareTrait::addArgument()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
rs 9.4285
cc 3
eloc 7
nc 3
nop 3
crap 3.0261
1
<?php
2
/*
3
* This file is a part of graphql-youshido project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 12/1/15 11:07 PM
7
*/
8
9
namespace Youshido\GraphQL\Type\Config\Traits;
10
11
12
use Youshido\GraphQL\Type\AbstractType;
13
use Youshido\GraphQL\Type\Field\InputField;
14
use Youshido\GraphQL\Type\TypeMap;
15
use Youshido\GraphQL\Validator\Exception\ConfigurationException;
16
17
trait ArgumentsAwareTrait
18
{
19
    protected $arguments = [];
20
21 26
    public function buildArguments()
22
    {
23 26
        $sourceArguments = empty($this->data['args']) ? [] : $this->data['args'];
0 ignored issues
show
Bug introduced by
The property data does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24 26
        foreach ($sourceArguments as $argumentName => $argumentInfo) {
25 12
            if ($argumentInfo instanceof InputField) {
26
                $this->arguments[$argumentName] = $argumentInfo;
27
                continue;
28
            } elseif ($argumentInfo instanceof AbstractType) {
29
                $config = [
30
                    'type' => $argumentInfo
31
                ];
32
                $this->addArgument($argumentName, $argumentInfo, $config);
33
            } else {
34 12
                $this->addArgument($argumentName, $argumentInfo['type'], $argumentInfo);
35
            }
36
37
        }
38 26
    }
39
40 14
    public function addArgument($name, $type, $config = [])
41
    {
42 14
        if (!TypeMap::isInputType($type)) {
43
            throw new ConfigurationException('Argument input type ' . $type . ' is not supported');
44
        }
45
46 14
        $config['name'] = $name;
47 14
        $config['type'] = is_string($type) ? TypeMap::getScalarTypeObject($type) : $type;
48
49 14
        $this->arguments[$name] = new InputField($config);
50
51 14
        return $this;
52
    }
53
54
    /**
55
     * @param $name
56
     *
57
     * @return InputField
58
     */
59 6
    public function getArgument($name)
60
    {
61 6
        return $this->hasArgument($name) ? $this->arguments[$name] : null;
62
    }
63
64
    /**
65
     * @param $name
66
     *
67
     * @return bool
68
     */
69 6
    public function hasArgument($name)
70
    {
71 6
        return array_key_exists($name, $this->arguments);
72
    }
73
74 2
    public function hasArguments()
75
    {
76 2
        return !empty($this->arguments);
77
    }
78
79
    /**
80
     * @return InputField[]
81
     */
82 19
    public function getArguments()
83
    {
84 19
        return $this->arguments;
85
    }
86
87
    public function removeArgument($name)
88
    {
89
        if ($this->hasArgument($name)) {
90
            unset($this->arguments[$name]);
91
        }
92
    }
93
94
}
95