Completed
Push — master ( 020154...e8c337 )
by Alexandr
02:45
created

ArgumentsAwareTrait   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 89.47%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 14
c 5
b 0
f 1
lcom 1
cbo 3
dl 0
loc 56
ccs 17
cts 19
cp 0.8947
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A buildArguments() 0 7 3
A addArgument() 0 13 3
A getArgument() 0 4 2
A hasArgument() 0 4 1
A getArguments() 0 4 1
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 13
    public function buildArguments()
21
    {
22 13
        $sourceArguments = empty($this->data['arguments']) ? [] : $this->data['arguments'];
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 13
        foreach ($sourceArguments as $argumentName => $argumentInfo) {
24
            $this->addArgument($argumentName, $argumentInfo['type'], $argumentInfo);
25 13
        }
26 13
    }
27
28 4
    public function addArgument($name, $type, $config = [])
29
    {
30 4
        if (!TypeMap::isInputType($type)) {
31
            throw new ConfigurationException('Argument input type ' . $type . ' is not supported');
32
        }
33
34 4
        $config['name'] = $name;
35 4
        $config['type'] = is_string($type) ? TypeMap::getScalarTypeObject($type) : $type;
36
37 4
        $this->arguments[$name] = new InputField($config);
38
39 4
        return $this;
40
    }
41
42
    /**
43
     * @param $name
44
     *
45
     * @return InputField
46
     */
47 2
    public function getArgument($name)
48
    {
49 2
        return $this->hasArgument($name) ? $this->arguments[$name] : null;
50
    }
51
52
    /**
53
     * @param $name
54
     *
55
     * @return bool
56
     */
57 2
    public function hasArgument($name)
58
    {
59 2
        return array_key_exists($name, $this->arguments);
60
    }
61
62
    /**
63
     * @return InputField[]
64
     */
65 7
    public function getArguments()
66
    {
67 7
        return $this->arguments;
68
    }
69
70
71
}