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 = []) |
|
84 | |||
85 | 60 | public function addQueries($queries) |
|
91 | |||
92 | 60 | public function addMutations($mutations) |
|
98 | |||
99 | 59 | public function addQueryVariables($queryVariables) |
|
105 | |||
106 | 59 | public function addVariableReferences($variableReferences) |
|
112 | |||
113 | 59 | public function addFragmentReferences($fragmentReferences) |
|
119 | |||
120 | 60 | public function addFragments($fragments) |
|
126 | |||
127 | /** |
||
128 | * @return Query[] |
||
129 | */ |
||
130 | 58 | public function getAllOperations() |
|
134 | |||
135 | /** |
||
136 | * @return Query[] |
||
137 | */ |
||
138 | 1 | public function getQueries() |
|
142 | |||
143 | /** |
||
144 | * @return Fragment[] |
||
145 | */ |
||
146 | 62 | 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) |
|
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 | 61 | public function setVariables($variables) |
|
238 | |||
239 | 8 | public function getVariable($name) |
|
243 | |||
244 | 8 | public function hasVariable($name) |
|
248 | |||
249 | /** |
||
250 | * @return array|Variable[] |
||
251 | */ |
||
252 | 59 | public function getQueryVariables() |
|
256 | |||
257 | /** |
||
258 | * @param array $queryVariables |
||
259 | */ |
||
260 | public function setQueryVariables($queryVariables) |
||
264 | |||
265 | /** |
||
266 | * @return array|FragmentReference[] |
||
267 | */ |
||
268 | 64 | public function getFragmentReferences() |
|
272 | |||
273 | /** |
||
274 | * @param array $fragmentReferences |
||
275 | */ |
||
276 | public function setFragmentReferences($fragmentReferences) |
||
280 | |||
281 | /** |
||
282 | * @return array|VariableReference[] |
||
283 | */ |
||
284 | 60 | 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..