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

ArgumentsAwareTrait   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 66.67%

Importance

Changes 8
Bugs 1 Features 3
Metric Value
wmc 15
c 8
b 1
f 3
lcom 1
cbo 3
dl 0
loc 78
rs 10
ccs 20
cts 30
cp 0.6667

7 Methods

Rating   Name   Duplication   Size   Complexity  
B buildArguments() 0 18 5
A addArgument() 0 13 3
A getArgument() 0 4 2
A hasArgument() 0 4 1
A hasArguments() 0 4 1
A getArguments() 0 4 1
A removeArgument() 0 6 2
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