Completed
Push — master ( 911cdf...67ba27 )
by Alexandr
02:54
created

ArgumentsAwareConfigTrait::buildArguments()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 8
cp 0.875
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
crap 3.0175
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\Config\Traits;
10
11
12
use Youshido\GraphQL\Field\InputField;
13
14
trait ArgumentsAwareConfigTrait
15
{
16
    protected $arguments = [];
17
    protected $_isArgumentsBuilt;
18
19 100
    public function buildArguments()
20
    {
21 100
        if ($this->_isArgumentsBuilt) {
22
            return;
23
        }
24
25 100
        if (!empty($this->data['args'])) {
26 64
            $this->addArguments($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...
27 64
        }
28 100
        $this->_isArgumentsBuilt = true;
29 100
    }
30
31 65
    public function addArguments($argsList)
32
    {
33 65
        foreach ($argsList as $argumentName => $argumentInfo) {
34 65
            if ($argumentInfo instanceof InputField) {
35 20
                $this->arguments[$argumentInfo->getName()] = $argumentInfo;
36 20
                continue;
37
            } else {
38 59
                $this->addArgument($argumentName, $this->buildConfig($argumentName, $argumentInfo));
39
            }
40 65
        }
41
42 65
        return $this;
43
    }
44
45 73
    public function addArgument($argument, $argumentInfo = null)
46
    {
47 73
        if (!($argument instanceof InputField)) {
48 63
            $argument = new InputField($this->buildConfig($argument, $argumentInfo));
49 63
        }
50 73
        $this->arguments[$argument->getName()] = $argument;
51
52 73
        return $this;
53
    }
54
55 63 View Code Duplication
    protected function buildConfig($name, $info = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57 63
        if (!is_array($info)) {
58
            return [
59 50
                'type' => $info,
60
                'name' => $name
61 50
            ];
62
        }
63 63
        if (empty($info['name'])) {
64 25
            $info['name'] = $name;
65 25
        }
66
67 63
        return $info;
68
    }
69
70
    /**
71
     * @param $name
72
     *
73
     * @return InputField
74
     */
75 38
    public function getArgument($name)
76
    {
77 38
        return $this->hasArgument($name) ? $this->arguments[$name] : null;
78
    }
79
80
    /**
81
     * @param $name
82
     *
83
     * @return bool
84
     */
85 40
    public function hasArgument($name)
86
    {
87 40
        return array_key_exists($name, $this->arguments);
88
    }
89
90 5
    public function hasArguments()
91
    {
92 5
        return !empty($this->arguments);
93
    }
94
95
    /**
96
     * @return InputField[]
97
     */
98 64
    public function getArguments()
99
    {
100 64
        return $this->arguments;
101
    }
102
103 2
    public function removeArgument($name)
104
    {
105 2
        if ($this->hasArgument($name)) {
106 2
            unset($this->arguments[$name]);
107 2
        }
108
109 2
        return $this;
110
    }
111
112
}
113