Completed
Pull Request — master (#45)
by Daniel
04:14
created

Query::setFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Date: 23.11.15
4
 *
5
 * @author Portey Vasil <[email protected]>
6
 */
7
8
namespace Youshido\GraphQL\Parser\Ast;
9
10
11
class Query
12
{
13
14
    /** @var string */
15
    protected $name;
16
17
    /** @var string */
18
    protected $alias;
19
20
    /** @var Argument[] */
21
    protected $arguments;
22
23
    /** @var Field[]|Query[] */
24
    protected $fields;
25
26 51
    public function __construct($name, $alias = null, $arguments = [], $fields = [])
27
    {
28 51
        $this->name      = $name;
29 51
        $this->alias     = $alias;
30 51
        $this->arguments = $arguments;
31 51
        $this->fields    = $fields;
32 51
    }
33
34 28
    public function getName()
35
    {
36 28
        return $this->name;
37
    }
38
39 1
    public function hasArguments()
40
    {
41 1
        return (bool)count($this->arguments);
42
    }
43
44
    /**
45
     * @return Argument[]
46
     */
47 29
    public function getArguments()
48
    {
49 29
        return $this->arguments;
50
    }
51
52
    /**
53
     * @param $arguments Argument[]
54
     */
55 1
    public function setArguments($arguments)
56
    {
57 1
        $this->arguments = $arguments;
58 1
    }
59
60 5
    public function addArgument(Argument $argument)
61
    {
62 5
        $this->arguments[$argument->getName()] = $argument;
63 5
    }
64
65 3
    public function getKeyValueArguments()
66
    {
67 3
        $arguments = [];
68
69 3
        foreach ($this->getArguments() as $argument) {
70 3
            $arguments[$argument->getName()] = $argument->getValue()->getValue();
71
        }
72
73 3
        return $arguments;
74
    }
75
76
    /**
77
     * @return Field[]|Query[]|FragmentInterface[]
78
     */
79 25
    public function getFields()
80
    {
81 25
        return $this->fields;
82
    }
83
84
    /**
85
     * @return bool
86
     */
87 23
    public function hasFields()
88
    {
89 23
        return (bool)count($this->fields);
90
    }
91
92
    /**
93
     * @param Field[]|Query[] $fields
94
     */
95 1
    public function setFields($fields)
96
    {
97 1
        $this->fields = $fields;
0 ignored issues
show
Documentation Bug introduced by
It seems like $fields of type array<integer,object<You...phQL\Parser\Ast\Query>> is incompatible with the declared type array<integer,object<You...phQL\Parser\Ast\Query>> of property $fields.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
98 1
    }
99
100 28
    public function getAlias()
101
    {
102 28
        return $this->alias;
103
    }
104
105
    public function hasField($name, $deep = false)
106
    {
107
        foreach ($this->getFields() as $field) {
108
            if ($field->getName() == $name) {
109
                return true;
110
            }
111
112
            if ($deep && $field instanceof Query) {
113
                if ($field->hasField($name)) {
114
                    return true;
115
                }
116
            }
117
        }
118
119
        return false;
120
    }
121
122
}
123