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 = []) |
|
78 | |||
79 | 60 | public function addQueries($queries) |
|
85 | |||
86 | 60 | public function addMutations($mutations) |
|
92 | |||
93 | 59 | public function addQueryVariables($queryVariables) |
|
99 | |||
100 | 59 | public function addVariableReferences($variableReferences) |
|
106 | |||
107 | 59 | public function addFragmentReferences($fragmentReferences) |
|
113 | |||
114 | 60 | public function addFragments($fragments) |
|
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) |
|
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) |
|
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..