Completed
Push — master ( d52ee7...e7842c )
by Alexandr
03:54
created

ArgumentsAwareConfigTrait   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 97
Duplicated Lines 14.43 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 18
c 3
b 0
f 0
lcom 1
cbo 1
dl 14
loc 97
rs 10
ccs 42
cts 42
cp 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A buildArguments() 0 9 3
A addArguments() 0 13 3
A addArgument() 0 9 2
A buildConfig() 14 14 3
A getArgument() 0 4 2
A hasArgument() 0 4 1
A hasArguments() 0 4 1
A getArguments() 0 4 1
A removeArgument() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 55
    public function buildArguments()
20
    {
21 55
        if ($this->_isArgumentsBuilt) return true;
22
23 55
        if (!empty($this->data['args'])) {
24 23
            $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...
25 23
        }
26 55
        $this->_isArgumentsBuilt = true;
27 55
    }
28
29 24
    public function addArguments($argsList)
30
    {
31 24
        foreach ($argsList as $argumentName => $argumentInfo) {
32 24
            if ($argumentInfo instanceof InputField) {
33 3
                $this->arguments[$argumentInfo->getName()] = $argumentInfo;
34 3
                continue;
35
            } else {
36 22
                $this->addArgument($argumentName, $this->buildConfig($argumentName, $argumentInfo));
37
            }
38 24
        }
39
40 24
        return $this;
41
    }
42
43 27
    public function addArgument($argument, $argumentInfo = null)
44
    {
45 27
        if (!($argument instanceof InputField)) {
46 22
            $argument = new InputField($this->buildConfig($argument, $argumentInfo));
47 22
        }
48 27
        $this->arguments[$argument->getName()] = $argument;
49
50 27
        return $this;
51
    }
52
53 22 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...
54
    {
55 22
        if (!is_array($info)) {
56
            return [
57 14
                'type' => $info,
58
                'name' => $name
59 14
            ];
60
        }
61 22
        if (empty($info['name'])) {
62 17
            $info['name'] = $name;
63 17
        }
64
65 22
        return $info;
66
    }
67
68
    /**
69
     * @param $name
70
     *
71
     * @return InputField
72
     */
73 9
    public function getArgument($name)
74
    {
75 9
        return $this->hasArgument($name) ? $this->arguments[$name] : null;
76
    }
77
78
    /**
79
     * @param $name
80
     *
81
     * @return bool
82
     */
83 11
    public function hasArgument($name)
84
    {
85 11
        return array_key_exists($name, $this->arguments);
86
    }
87
88 4
    public function hasArguments()
89
    {
90 4
        return !empty($this->arguments);
91
    }
92
93
    /**
94
     * @return InputField[]
95
     */
96 24
    public function getArguments()
97
    {
98 24
        return $this->arguments;
99
    }
100
101 2
    public function removeArgument($name)
102
    {
103 2
        if ($this->hasArgument($name)) {
104 2
            unset($this->arguments[$name]);
105 2
        }
106
107 2
        return $this;
108
    }
109
110
}
111