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 |
||
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 VariableReference[] */ |
||
34 | private $variableReferences = []; |
||
35 | |||
36 | /** @var array */ |
||
37 | private $queryVariables = []; |
||
38 | |||
39 | /** @var array */ |
||
40 | private $fragmentReferences = []; |
||
41 | |||
42 | 71 | public function __construct($data = [], $variables = []) |
|
43 | { |
||
44 | 71 | if (array_key_exists('queries', $data)) { |
|
45 | 70 | $this->addQueries($data['queries']); |
|
46 | 70 | } |
|
47 | |||
48 | 71 | if (array_key_exists('mutations', $data)) { |
|
49 | 70 | $this->addMutations($data['mutations']); |
|
50 | 70 | } |
|
51 | |||
52 | 71 | if (array_key_exists('fragments', $data)) { |
|
53 | 70 | $this->addFragments($data['fragments']); |
|
54 | 70 | } |
|
55 | |||
56 | 71 | if (array_key_exists('fragmentReferences', $data)) { |
|
57 | 69 | $this->addFragmentReferences($data['fragmentReferences']); |
|
58 | 69 | } |
|
59 | |||
60 | 71 | if (array_key_exists('variables', $data)) { |
|
61 | 69 | $this->addQueryVariables($data['variables']); |
|
62 | 69 | } |
|
63 | |||
64 | 71 | if (array_key_exists('variableReferences', $data)) { |
|
65 | 69 | foreach ($data['variableReferences'] as $ref) { |
|
66 | 9 | if (!array_key_exists($ref->getName(), $variables)) { |
|
67 | /** @var Variable $variable */ |
||
68 | $variable = $ref->getVariable(); |
||
69 | if ($variable->hasDefaultValue()) { |
||
70 | $variables[$variable->getName()] = $variable->getDefaultValue()->getValue(); |
||
71 | continue; |
||
72 | } |
||
73 | throw new InvalidRequestException(sprintf("Variable %s hasn't been submitted", $ref->getName()), $ref->getLocation()); |
||
74 | } |
||
75 | 69 | } |
|
76 | |||
77 | 69 | $this->addVariableReferences($data['variableReferences']); |
|
78 | 69 | } |
|
79 | |||
80 | 71 | $this->setVariables($variables); |
|
81 | 71 | } |
|
82 | |||
83 | 70 | public function addQueries($queries) |
|
84 | { |
||
85 | 70 | foreach ($queries as $query) { |
|
86 | 62 | $this->queries[] = $query; |
|
87 | 70 | } |
|
88 | 70 | } |
|
89 | |||
90 | 70 | public function addMutations($mutations) |
|
91 | { |
||
92 | 70 | foreach ($mutations as $mutation) { |
|
93 | 10 | $this->mutations[] = $mutation; |
|
94 | 70 | } |
|
95 | 70 | } |
|
96 | |||
97 | 69 | public function addQueryVariables($queryVariables) |
|
98 | { |
||
99 | 69 | foreach ($queryVariables as $queryVariable) { |
|
100 | 10 | $this->queryVariables[] = $queryVariable; |
|
101 | 69 | } |
|
102 | 69 | } |
|
103 | |||
104 | 69 | public function addVariableReferences($variableReferences) |
|
105 | { |
||
106 | 69 | foreach ($variableReferences as $variableReference) { |
|
107 | 9 | $this->variableReferences[] = $variableReference; |
|
108 | 69 | } |
|
109 | 69 | } |
|
110 | |||
111 | 69 | public function addFragmentReferences($fragmentReferences) |
|
112 | { |
||
113 | 69 | foreach ($fragmentReferences as $fragmentReference) { |
|
114 | 9 | $this->fragmentReferences[] = $fragmentReference; |
|
115 | 69 | } |
|
116 | 69 | } |
|
117 | |||
118 | 70 | public function addFragments($fragments) |
|
119 | { |
||
120 | 70 | foreach ($fragments as $fragment) { |
|
121 | 9 | $this->addFragment($fragment); |
|
122 | 70 | } |
|
123 | 70 | } |
|
124 | |||
125 | /** |
||
126 | * @return Query[] |
||
127 | */ |
||
128 | 68 | public function getAllOperations() |
|
132 | |||
133 | /** |
||
134 | * @return Query[] |
||
135 | */ |
||
136 | 1 | public function getQueries() |
|
140 | |||
141 | /** |
||
142 | * @return Fragment[] |
||
143 | */ |
||
144 | 72 | public function getFragments() |
|
148 | |||
149 | 9 | public function addFragment(Fragment $fragment) |
|
153 | |||
154 | /** |
||
155 | * @param $name |
||
156 | * |
||
157 | * @return null|Fragment |
||
158 | */ |
||
159 | 13 | public function getFragment($name) |
|
160 | { |
||
161 | 13 | foreach ($this->fragments as $fragment) { |
|
162 | 11 | if ($fragment->getName() == $name) { |
|
163 | 11 | return $fragment; |
|
164 | } |
||
165 | 10 | } |
|
166 | |||
167 | 4 | return null; |
|
168 | } |
||
169 | |||
170 | /** |
||
171 | * @return Mutation[] |
||
172 | */ |
||
173 | 1 | public function getMutations() |
|
177 | |||
178 | /** |
||
179 | * @return bool |
||
180 | */ |
||
181 | 1 | public function hasQueries() |
|
185 | |||
186 | /** |
||
187 | * @return bool |
||
188 | */ |
||
189 | 1 | public function hasMutations() |
|
193 | |||
194 | /** |
||
195 | * @return bool |
||
196 | */ |
||
197 | 1 | public function hasFragments() |
|
201 | |||
202 | /** |
||
203 | * @return array |
||
204 | */ |
||
205 | 2 | public function getVariables() |
|
209 | |||
210 | /** |
||
211 | * @param array|string $variables |
||
212 | * |
||
213 | * @return $this |
||
214 | */ |
||
215 | 71 | public function setVariables($variables) |
|
216 | { |
||
217 | 71 | if (!is_array($variables)) { |
|
218 | 1 | $variables = json_decode($variables, true); |
|
219 | 1 | } |
|
220 | |||
221 | 71 | $this->variables = $variables; |
|
|
|||
222 | 71 | foreach ($this->variableReferences as $reference) { |
|
223 | /** invalid request with no variable */ |
||
224 | 9 | if (!$reference->getVariable()) continue; |
|
225 | 9 | $variableName = $reference->getVariable()->getName(); |
|
226 | |||
227 | /** no variable was set at the time */ |
||
228 | 9 | if (!array_key_exists($variableName, $variables)) continue; |
|
229 | |||
230 | 9 | $reference->getVariable()->setValue($variables[$variableName]); |
|
231 | 9 | $reference->setValue($variables[$variableName]); |
|
232 | 71 | } |
|
233 | |||
234 | 71 | return $this; |
|
235 | } |
||
236 | |||
237 | 9 | public function getVariable($name) |
|
241 | |||
242 | 9 | public function hasVariable($name) |
|
246 | |||
247 | /** |
||
248 | * @return array|Variable[] |
||
249 | */ |
||
250 | 69 | public function getQueryVariables() |
|
254 | |||
255 | /** |
||
256 | * @param array $queryVariables |
||
257 | */ |
||
258 | public function setQueryVariables($queryVariables) |
||
262 | |||
263 | /** |
||
264 | * @return array|FragmentReference[] |
||
265 | */ |
||
266 | 74 | public function getFragmentReferences() |
|
270 | |||
271 | /** |
||
272 | * @param array $fragmentReferences |
||
273 | */ |
||
274 | public function setFragmentReferences($fragmentReferences) |
||
278 | |||
279 | /** |
||
280 | * @return array|VariableReference[] |
||
281 | */ |
||
282 | 70 | public function getVariableReferences() |
|
286 | } |
||
287 |
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..