Completed
Push — master ( 120e4f...bd44b4 )
by Portey
07:04
created

ArgumentsAwareTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 75%

Importance

Changes 6
Bugs 0 Features 2
Metric Value
wmc 12
c 6
b 0
f 2
lcom 1
cbo 3
dl 0
loc 62
ccs 18
cts 24
cp 0.75
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addArgument() 0 13 3
A getArgument() 0 4 2
A hasArgument() 0 4 1
A getArguments() 0 4 1
A buildArguments() 0 7 3
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\Field\InputField;
13
use Youshido\GraphQL\Type\TypeMap;
14
use Youshido\GraphQL\Validator\Exception\ConfigurationException;
15
16
trait ArgumentsAwareTrait
17
{
18
    protected $arguments = [];
19
20 20
    public function buildArguments()
21
    {
22 20
        $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...
23 20
        foreach ($sourceArguments as $argumentName => $argumentInfo) {
24 11
            $this->addArgument($argumentName, $argumentInfo['type'], $argumentInfo);
25 20
        }
26 20
    }
27
28 12
    public function addArgument($name, $type, $config = [])
29
    {
30 12
        if (!TypeMap::isInputType($type)) {
31
            throw new ConfigurationException('Argument input type ' . $type . ' is not supported');
32
        }
33
34 12
        $config['name'] = $name;
35 12
        $config['type'] = is_string($type) ? TypeMap::getScalarTypeObject($type) : $type;
36
37 12
        $this->arguments[$name] = new InputField($config);
38
39 12
        return $this;
40
    }
41
42
    /**
43
     * @param $name
44
     *
45
     * @return InputField
46
     */
47 5
    public function getArgument($name)
48
    {
49 5
        return $this->hasArgument($name) ? $this->arguments[$name] : null;
50
    }
51
52
    /**
53
     * @param $name
54
     *
55
     * @return bool
56
     */
57 5
    public function hasArgument($name)
58
    {
59 5
        return array_key_exists($name, $this->arguments);
60
    }
61
62
    /**
63
     * @return InputField[]
64
     */
65 13
    public function getArguments()
66
    {
67 13
        return $this->arguments;
68
    }
69
70
    public function removeArgument($name)
71
    {
72
        if ($this->hasArgument($name)) {
73
            unset($this->arguments[$name]);
74
        }
75
    }
76
77
}