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 |
||
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 | 62 | public function __construct($data = [], $variables = []) |
|
45 | { |
||
46 | 62 | if (array_key_exists('queries', $data)) { |
|
47 | 61 | $this->addQueries($data['queries']); |
|
48 | } |
||
49 | |||
50 | 62 | if (array_key_exists('mutations', $data)) { |
|
51 | 61 | $this->addMutations($data['mutations']); |
|
52 | } |
||
53 | |||
54 | 62 | if (array_key_exists('fragments', $data)) { |
|
55 | 61 | $this->addFragments($data['fragments']); |
|
56 | } |
||
57 | |||
58 | 62 | if (array_key_exists('fragmentReferences', $data)) { |
|
59 | 60 | $this->addFragmentReferences($data['fragmentReferences']); |
|
60 | } |
||
61 | |||
62 | 62 | if (array_key_exists('variables', $data)) { |
|
63 | 60 | $this->addQueryVariables($data['variables']); |
|
64 | } |
||
65 | |||
66 | 62 | if (array_key_exists('variableReferences', $data)) { |
|
67 | 60 | foreach ($data['variableReferences'] as $ref) { |
|
68 | 9 | if (!array_key_exists($ref->getName(), $variables)) { |
|
69 | /** @var Variable $variable */ |
||
70 | $variable = $ref->getVariable(); |
||
71 | if ($variable->hasDefaultValue()) { |
||
72 | $variables[$variable->getName()] = $variable->getDefaultValue()->getValue(); |
||
73 | continue; |
||
74 | } |
||
75 | throw new InvalidRequestException(sprintf("Variable %s hasn't been submitted", $ref->getName()), $ref->getLocation()); |
||
76 | } |
||
77 | } |
||
78 | |||
79 | 60 | $this->addVariableReferences($data['variableReferences']); |
|
80 | } |
||
81 | |||
82 | 62 | $this->setVariables($variables); |
|
83 | 62 | } |
|
84 | |||
85 | 61 | public function addQueries($queries) |
|
86 | { |
||
87 | 61 | foreach ($queries as $query) { |
|
88 | 53 | $this->queries[] = $query; |
|
89 | } |
||
90 | 61 | } |
|
91 | |||
92 | 61 | public function addMutations($mutations) |
|
93 | { |
||
94 | 61 | foreach ($mutations as $mutation) { |
|
95 | 10 | $this->mutations[] = $mutation; |
|
96 | } |
||
97 | 61 | } |
|
98 | |||
99 | 60 | public function addQueryVariables($queryVariables) |
|
100 | { |
||
101 | 60 | foreach ($queryVariables as $queryVariable) { |
|
102 | 10 | $this->queryVariables[] = $queryVariable; |
|
103 | } |
||
104 | 60 | } |
|
105 | |||
106 | 60 | public function addVariableReferences($variableReferences) |
|
107 | { |
||
108 | 60 | foreach ($variableReferences as $variableReference) { |
|
109 | 9 | $this->variableReferences[] = $variableReference; |
|
110 | } |
||
111 | 60 | } |
|
112 | |||
113 | 60 | public function addFragmentReferences($fragmentReferences) |
|
114 | { |
||
115 | 60 | foreach ($fragmentReferences as $fragmentReference) { |
|
116 | 5 | $this->fragmentReferences[] = $fragmentReference; |
|
117 | } |
||
118 | 60 | } |
|
119 | |||
120 | 61 | public function addFragments($fragments) |
|
121 | { |
||
122 | 61 | foreach ($fragments as $fragment) { |
|
123 | 5 | $this->addFragment($fragment); |
|
124 | } |
||
125 | 61 | } |
|
126 | |||
127 | /** |
||
128 | * @return Query[] |
||
129 | */ |
||
130 | 59 | public function getAllOperations() |
|
134 | |||
135 | /** |
||
136 | * @return Query[] |
||
137 | */ |
||
138 | 1 | public function getQueries() |
|
142 | |||
143 | /** |
||
144 | * @return Fragment[] |
||
145 | */ |
||
146 | 63 | public function getFragments() |
|
150 | |||
151 | 5 | public function addFragment(Fragment $fragment) |
|
155 | |||
156 | /** |
||
157 | * @param $name |
||
158 | * |
||
159 | * @return null|Fragment |
||
160 | */ |
||
161 | 9 | public function getFragment($name) |
|
162 | { |
||
163 | 9 | foreach ($this->fragments as $fragment) { |
|
164 | 7 | if ($fragment->getName() == $name) { |
|
165 | 7 | return $fragment; |
|
166 | } |
||
167 | } |
||
168 | |||
169 | 4 | return null; |
|
170 | } |
||
171 | |||
172 | /** |
||
173 | * @return Mutation[] |
||
174 | */ |
||
175 | 1 | public function getMutations() |
|
179 | |||
180 | /** |
||
181 | * @return bool |
||
182 | */ |
||
183 | 1 | public function hasQueries() |
|
187 | |||
188 | /** |
||
189 | * @return bool |
||
190 | */ |
||
191 | 1 | public function hasMutations() |
|
195 | |||
196 | /** |
||
197 | * @return bool |
||
198 | */ |
||
199 | 1 | public function hasFragments() |
|
203 | |||
204 | /** |
||
205 | * @return array |
||
206 | */ |
||
207 | 2 | public function getVariables() |
|
211 | |||
212 | /** |
||
213 | * @param array $variables |
||
214 | * |
||
215 | * @return $this |
||
216 | */ |
||
217 | 62 | public function setVariables($variables) |
|
218 | { |
||
219 | 62 | if (!is_array($variables)) { |
|
220 | 1 | $variables = json_decode($variables, true); |
|
221 | } |
||
222 | |||
223 | 62 | $this->variables = $variables; |
|
|
|||
224 | 62 | foreach ($this->variableReferences as $reference) { |
|
225 | /** invalid request with no variable */ |
||
226 | 9 | if (!$reference->getVariable()) continue; |
|
227 | 9 | $variableName = $reference->getVariable()->getName(); |
|
228 | |||
229 | /** no variable was set at the time */ |
||
230 | 9 | if (!array_key_exists($variableName, $variables)) continue; |
|
231 | |||
232 | 9 | $reference->getVariable()->setValue($variables[$variableName]); |
|
233 | 9 | $reference->setValue($variables[$variableName]); |
|
234 | } |
||
235 | |||
236 | 62 | return $this; |
|
237 | } |
||
238 | |||
239 | 9 | public function getVariable($name) |
|
243 | |||
244 | 9 | public function hasVariable($name) |
|
248 | |||
249 | /** |
||
250 | * @return array|Variable[] |
||
251 | */ |
||
252 | 60 | public function getQueryVariables() |
|
256 | |||
257 | /** |
||
258 | * @param array $queryVariables |
||
259 | */ |
||
260 | public function setQueryVariables($queryVariables) |
||
264 | |||
265 | /** |
||
266 | * @return array|FragmentReference[] |
||
267 | */ |
||
268 | 65 | public function getFragmentReferences() |
|
272 | |||
273 | /** |
||
274 | * @param array $fragmentReferences |
||
275 | */ |
||
276 | public function setFragmentReferences($fragmentReferences) |
||
280 | |||
281 | /** |
||
282 | * @return array|VariableReference[] |
||
283 | */ |
||
284 | 61 | public function getVariableReferences() |
|
288 | } |
||
289 |
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..