Completed
Push — master ( e13501...8c19ea )
by Alexandr
05:24
created

Request   B

Complexity

Total Complexity 41

Size/Duplication

Total Lines 243
Duplicated Lines 0 %

Coupling/Cohesion

Components 6
Dependencies 1

Test Coverage

Coverage 93.88%

Importance

Changes 0
Metric Value
wmc 41
lcom 6
cbo 1
dl 0
loc 243
ccs 92
cts 98
cp 0.9388
rs 7.5774
c 0
b 0
f 0

25 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 0 28 7
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 getOperationsInOrder() 0 4 1
A getQueries() 0 4 1
A getFragments() 0 4 1
A addFragment() 0 4 1
A getFragment() 0 10 3
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 setVariables() 0 10 2
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

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
11
use Youshido\GraphQL\Parser\Ast\ArgumentValue\Variable;
12
use Youshido\GraphQL\Parser\Ast\ArgumentValue\VariableReference;
13
use Youshido\GraphQL\Parser\Ast\Fragment;
14
use Youshido\GraphQL\Parser\Ast\FragmentReference;
15
use Youshido\GraphQL\Parser\Ast\Mutation;
16
use Youshido\GraphQL\Parser\Ast\Query;
17
18
class Request
19
{
20
21
    /** @var  Query[] */
22
    private $queries = [];
23
24
    /** @var Fragment[] */
25
    private $fragments = [];
26
27
    /** @var Mutation[] */
28
    private $mutations = [];
29
30
    /** @var array */
31
    private $variables = [];
32
33
    /** @var  array */
34
    private $variableReferences = [];
35
36
    /** @var  array */
37
    private $queryVariables = [];
38
39
    /** @var array */
40
    private $fragmentReferences = [];
41
42 32
    public function __construct($data = [], $variables = [])
43
    {
44 32
        if (array_key_exists('queries', $data)) {
45 30
            $this->addQueries($data['queries']);
46 30
        }
47
48 32
        if (array_key_exists('mutations', $data)) {
49 30
            $this->addMutations($data['mutations']);
50 30
        }
51
52 32
        if (array_key_exists('fragments', $data)) {
53 30
            $this->addFragments($data['fragments']);
54 30
        }
55
56 32
        if (array_key_exists('fragmentReferences', $data)) {
57 29
            $this->addFragmentReferences($data['fragmentReferences']);
58 29
        }
59
60 32
        if (array_key_exists('variables', $data)) {
61 29
            $this->addQueryVariables($data['variables']);
62 29
        }
63
64 32
        if (array_key_exists('variableReferences', $data)) {
65 29
            $this->addVariableReferences($data['variableReferences']);
66 29
        }
67
68 32
        $this->setVariables($variables);
69 32
    }
70
71 30
    public function addQueries($queries)
72
    {
73 30
        foreach ($queries as $query) {
74 28
            $this->queries[] = $query;
75 30
        }
76 30
    }
77
78 30
    public function addMutations($mutations)
79
    {
80 30
        foreach ($mutations as $mutation) {
81 4
            $this->mutations[] = $mutation;
82 30
        }
83 30
    }
84
85 29
    public function addQueryVariables($queryVariables)
86
    {
87 29
        foreach ($queryVariables as $queryVariable) {
88 3
            $this->queryVariables[] = $queryVariable;
89 29
        }
90 29
    }
91
92 29
    public function addVariableReferences($variableReferences)
93
    {
94 29
        foreach ($variableReferences as $variableReference) {
95 3
            $this->variableReferences[] = $variableReference;
96 29
        }
97 29
    }
98
99 29
    public function addFragmentReferences($fragmentReferences)
100
    {
101 29
        foreach ($fragmentReferences as $fragmentReference) {
102 2
            $this->fragmentReferences[] = $fragmentReference;
103 29
        }
104 29
    }
105
106 30
    public function addFragments($fragments)
107
    {
108 30
        foreach ($fragments as $fragment) {
109 3
            $this->addFragment($fragment);
110 30
        }
111 30
    }
112
113 29
    public function getOperationsInOrder()
114
    {
115 29
        return array_merge($this->mutations, $this->queries);
116
    }
117
118
    /**
119
     * @return Query[]
120
     */
121 1
    public function getQueries()
122
    {
123 1
        return $this->queries;
124
    }
125
126
    /**
127
     * @return Fragment[]
128
     */
129 33
    public function getFragments()
130
    {
131 33
        return $this->fragments;
132
    }
133
134 3
    public function addFragment(Fragment $fragment)
135
    {
136 3
        $this->fragments[] = $fragment;
137 3
    }
138
139
    /**
140
     * @param $name
141
     *
142
     * @return null|Fragment
143
     */
144 6
    public function getFragment($name)
145
    {
146 6
        foreach ($this->fragments as $fragment) {
147 5
            if ($fragment->getName() == $name) {
148 5
                return $fragment;
149
            }
150 5
        }
151
152 3
        return null;
153
    }
154
155
    /**
156
     * @return Mutation[]
157
     */
158 1
    public function getMutations()
159
    {
160 1
        return $this->mutations;
161
    }
162
163
    /**
164
     * @return bool
165
     */
166 1
    public function hasQueries()
167
    {
168 1
        return (bool)count($this->queries);
169
    }
170
171
    /**
172
     * @return bool
173
     */
174 1
    public function hasMutations()
175
    {
176 1
        return (bool)count($this->mutations);
177
    }
178
179
    /**
180
     * @return bool
181
     */
182 1
    public function hasFragments()
183
    {
184 1
        return (bool)count($this->fragments);
185
    }
186
187
    /**
188
     * @return array
189
     */
190 2
    public function getVariables()
191
    {
192 2
        return $this->variables;
193
    }
194
195
    /**
196
     * @param array $variables
197
     *
198
     * @return $this
199
     */
200 32
    public function setVariables($variables)
201
    {
202 32
        if (!is_array($variables)) {
203 1
            $variables = json_decode($variables, true);
204 1
        }
205
206 32
        $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...
207
208 32
        return $this;
209
    }
210
211 4
    public function getVariable($name)
212
    {
213 4
        return $this->hasVariable($name) ? $this->variables[$name] : null;
214
    }
215
216 4
    public function hasVariable($name)
217
    {
218 4
        return array_key_exists($name, $this->variables);
219
    }
220
221
    /**
222
     * @return array|Variable[]
223
     */
224 30
    public function getQueryVariables()
225
    {
226 30
        return $this->queryVariables;
227
    }
228
229
    /**
230
     * @param array $queryVariables
231
     */
232
    public function setQueryVariables($queryVariables)
233
    {
234
        $this->queryVariables = $queryVariables;
235
    }
236
237
    /**
238
     * @return array|FragmentReference[]
239
     */
240 34
    public function getFragmentReferences()
241
    {
242 34
        return $this->fragmentReferences;
243
    }
244
245
    /**
246
     * @param array $fragmentReferences
247
     */
248
    public function setFragmentReferences($fragmentReferences)
249
    {
250
        $this->fragmentReferences = $fragmentReferences;
251
    }
252
253
    /**
254
     * @return array|VariableReference[]
255
     */
256 31
    public function getVariableReferences()
257
    {
258 31
        return $this->variableReferences;
259
    }
260
}
261