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 | 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() |
|
128 | |||
129 | /** |
||
130 | * @return Query[] |
||
131 | */ |
||
132 | 1 | public function getQueries() |
|
136 | |||
137 | /** |
||
138 | * @return Fragment[] |
||
139 | */ |
||
140 | 62 | public function getFragments() |
|
144 | |||
145 | 5 | public function addFragment(Fragment $fragment) |
|
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() |
|
173 | |||
174 | /** |
||
175 | * @return bool |
||
176 | */ |
||
177 | 1 | public function hasQueries() |
|
181 | |||
182 | /** |
||
183 | * @return bool |
||
184 | */ |
||
185 | 1 | public function hasMutations() |
|
189 | |||
190 | /** |
||
191 | * @return bool |
||
192 | */ |
||
193 | 1 | public function hasFragments() |
|
197 | |||
198 | /** |
||
199 | * @return array |
||
200 | */ |
||
201 | 2 | public function getVariables() |
|
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; |
|
|
|||
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) |
|
237 | |||
238 | 8 | public function hasVariable($name) |
|
242 | |||
243 | /** |
||
244 | * @return array|Variable[] |
||
245 | */ |
||
246 | 59 | public function getQueryVariables() |
|
250 | |||
251 | /** |
||
252 | * @param array $queryVariables |
||
253 | */ |
||
254 | public function setQueryVariables($queryVariables) |
||
258 | |||
259 | /** |
||
260 | * @return array|FragmentReference[] |
||
261 | */ |
||
262 | 64 | public function getFragmentReferences() |
|
266 | |||
267 | /** |
||
268 | * @param array $fragmentReferences |
||
269 | */ |
||
270 | public function setFragmentReferences($fragmentReferences) |
||
274 | |||
275 | /** |
||
276 | * @return array|VariableReference[] |
||
277 | */ |
||
278 | 60 | public function getVariableReferences() |
|
282 | } |
||
283 |
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..