Completed
Push — master ( eea4af...ac3565 )
by Alexandr
13:12
created

Request   B

Complexity

Total Complexity 46

Size/Duplication

Total Lines 263
Duplicated Lines 0 %

Coupling/Cohesion

Components 5
Dependencies 3

Test Coverage

Coverage 93.58%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 46
lcom 5
cbo 3
dl 0
loc 263
rs 8.3999
c 1
b 0
f 0
ccs 102
cts 109
cp 0.9358

25 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllOperations() 0 4 1
A getQueries() 0 4 1
A getFragments() 0 4 1
A addFragment() 0 4 1
A getMutations() 0 4 1
A hasQueries() 0 4 1
A hasMutations() 0 4 1
A hasFragments() 0 4 1
A getVariables() 0 4 1
A getVariable() 0 4 2
A hasVariable() 0 4 1
A getQueryVariables() 0 4 1
A setQueryVariables() 0 4 1
A getFragmentReferences() 0 4 1
A setFragmentReferences() 0 4 1
A getVariableReferences() 0 4 1
D __construct() 0 34 9
A addQueries() 0 6 2
A addMutations() 0 6 2
A addQueryVariables() 0 6 2
A addVariableReferences() 0 6 2
A addFragmentReferences() 0 6 2
A addFragments() 0 6 2
A getFragment() 0 10 3
B setVariables() 0 21 5

How to fix   Complexity   

Complex Class

Complex classes like Request often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Request, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Date: 23.11.15
4
 *
5
 * @author Portey Vasil <[email protected]>
6
 */
7
8
namespace Youshido\GraphQL\Execution;
9
10
use Youshido\GraphQL\Exception\Parser\InvalidRequestException;
11
use Youshido\GraphQL\Parser\Ast\ArgumentValue\Literal;
12
use Youshido\GraphQL\Parser\Ast\ArgumentValue\Variable;
13
use Youshido\GraphQL\Parser\Ast\ArgumentValue\VariableReference;
14
use Youshido\GraphQL\Parser\Ast\Fragment;
15
use Youshido\GraphQL\Parser\Ast\FragmentReference;
16
use Youshido\GraphQL\Parser\Ast\Mutation;
17
use Youshido\GraphQL\Parser\Ast\Query;
18
use Youshido\GraphQL\Parser\Location;
19
20
class Request
21
{
22
23
    /** @var  Query[] */
24
    private $queries = [];
25
26
    /** @var Fragment[] */
27
    private $fragments = [];
28
29
    /** @var Mutation[] */
30
    private $mutations = [];
31
32
    /** @var array */
33
    private $variables = [];
34
35
    /** @var VariableReference[] */
36
    private $variableReferences = [];
37
38
    /** @var  array */
39
    private $queryVariables = [];
40
41
    /** @var array */
42
    private $fragmentReferences = [];
43
44 61
    public function __construct($data = [], $variables = [])
45
    {
46 61
        if (array_key_exists('queries', $data)) {
47 60
            $this->addQueries($data['queries']);
48 60
        }
49
50 61
        if (array_key_exists('mutations', $data)) {
51 60
            $this->addMutations($data['mutations']);
52 60
        }
53
54 61
        if (array_key_exists('fragments', $data)) {
55 60
            $this->addFragments($data['fragments']);
56 60
        }
57
58 61
        if (array_key_exists('fragmentReferences', $data)) {
59 59
            $this->addFragmentReferences($data['fragmentReferences']);
60 59
        }
61
62 61
        if (array_key_exists('variables', $data)) {
63 59
            $this->addQueryVariables($data['variables']);
64 59
        }
65
66 61
        if (array_key_exists('variableReferences', $data)) {
67 59
            foreach ($data['variableReferences'] as $ref) {
68 8
                if (!array_key_exists($ref->getName(), $variables)) {
69
                    throw new InvalidRequestException(sprintf("Variable %s hasn't been submitted", $ref->getName()), $ref->getLocation());
70
                }
71 59
            }
72
73 59
            $this->addVariableReferences($data['variableReferences']);
74 59
        }
75
76 61
        $this->setVariables($variables);
77 61
    }
78
79 60
    public function addQueries($queries)
80
    {
81 60
        foreach ($queries as $query) {
82 53
            $this->queries[] = $query;
83 60
        }
84 60
    }
85
86 60
    public function addMutations($mutations)
87
    {
88 60
        foreach ($mutations as $mutation) {
89 9
            $this->mutations[] = $mutation;
90 60
        }
91 60
    }
92
93 59
    public function addQueryVariables($queryVariables)
94
    {
95 59
        foreach ($queryVariables as $queryVariable) {
96 9
            $this->queryVariables[] = $queryVariable;
97 59
        }
98 59
    }
99
100 59
    public function addVariableReferences($variableReferences)
101
    {
102 59
        foreach ($variableReferences as $variableReference) {
103 8
            $this->variableReferences[] = $variableReference;
104 59
        }
105 59
    }
106
107 59
    public function addFragmentReferences($fragmentReferences)
108
    {
109 59
        foreach ($fragmentReferences as $fragmentReference) {
110 5
            $this->fragmentReferences[] = $fragmentReference;
111 59
        }
112 59
    }
113
114 60
    public function addFragments($fragments)
115
    {
116 60
        foreach ($fragments as $fragment) {
117 5
            $this->addFragment($fragment);
118 60
        }
119 60
    }
120
121
    /**
122
     * @return Query[]
123
     */
124 58
    public function getAllOperations()
125
    {
126 58
        return array_merge($this->mutations, $this->queries);
127
    }
128
129
    /**
130
     * @return Query[]
131
     */
132 1
    public function getQueries()
133
    {
134 1
        return $this->queries;
135
    }
136
137
    /**
138
     * @return Fragment[]
139
     */
140 62
    public function getFragments()
141
    {
142 62
        return $this->fragments;
143
    }
144
145 5
    public function addFragment(Fragment $fragment)
146
    {
147 5
        $this->fragments[] = $fragment;
148 5
    }
149
150
    /**
151
     * @param $name
152
     *
153
     * @return null|Fragment
154
     */
155 9
    public function getFragment($name)
156
    {
157 9
        foreach ($this->fragments as $fragment) {
158 7
            if ($fragment->getName() == $name) {
159 7
                return $fragment;
160
            }
161 7
        }
162
163 4
        return null;
164
    }
165
166
    /**
167
     * @return Mutation[]
168
     */
169 1
    public function getMutations()
170
    {
171 1
        return $this->mutations;
172
    }
173
174
    /**
175
     * @return bool
176
     */
177 1
    public function hasQueries()
178
    {
179 1
        return (bool)count($this->queries);
180
    }
181
182
    /**
183
     * @return bool
184
     */
185 1
    public function hasMutations()
186
    {
187 1
        return (bool)count($this->mutations);
188
    }
189
190
    /**
191
     * @return bool
192
     */
193 1
    public function hasFragments()
194
    {
195 1
        return (bool)count($this->fragments);
196
    }
197
198
    /**
199
     * @return array
200
     */
201 2
    public function getVariables()
202
    {
203 2
        return $this->variables;
204
    }
205
206
    /**
207
     * @param array $variables
208
     *
209
     * @return $this
210
     */
211 61
    public function setVariables($variables)
212
    {
213 61
        if (!is_array($variables)) {
214 1
            $variables = json_decode($variables, true);
215 1
        }
216
217 61
        $this->variables = $variables;
0 ignored issues
show
Documentation Bug introduced by
It seems like $variables of type * is incompatible with the declared type array of property $variables.

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...
218 61
        foreach ($this->variableReferences as $reference) {
219
            /** invalid request with no variable */
220 8
            if (!$reference->getVariable()) continue;
221 8
            $variableName = $reference->getVariable()->getName();
222
223
            /** no variable was set at the time */
224 8
            if (!array_key_exists($variableName, $variables)) continue;
225
226 8
            $reference->getVariable()->setValue($variables[$variableName]);
227 8
            $reference->setValue($variables[$variableName]);
228 61
        }
229
230 61
        return $this;
231
    }
232
233 8
    public function getVariable($name)
234
    {
235 8
        return $this->hasVariable($name) ? $this->variables[$name] : null;
236
    }
237
238 8
    public function hasVariable($name)
239
    {
240 8
        return array_key_exists($name, $this->variables);
241
    }
242
243
    /**
244
     * @return array|Variable[]
245
     */
246 59
    public function getQueryVariables()
247
    {
248 59
        return $this->queryVariables;
249
    }
250
251
    /**
252
     * @param array $queryVariables
253
     */
254
    public function setQueryVariables($queryVariables)
255
    {
256
        $this->queryVariables = $queryVariables;
257
    }
258
259
    /**
260
     * @return array|FragmentReference[]
261
     */
262 64
    public function getFragmentReferences()
263
    {
264 64
        return $this->fragmentReferences;
265
    }
266
267
    /**
268
     * @param array $fragmentReferences
269
     */
270
    public function setFragmentReferences($fragmentReferences)
271
    {
272
        $this->fragmentReferences = $fragmentReferences;
273
    }
274
275
    /**
276
     * @return array|VariableReference[]
277
     */
278 60
    public function getVariableReferences()
279
    {
280 60
        return $this->variableReferences;
281
    }
282
}
283