Complex classes like QueryResult 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 QueryResult, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class QueryResult |
||
10 | { |
||
11 | |||
12 | /** |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | private $definitionSource; |
||
17 | |||
18 | /** |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | private $heading; |
||
23 | |||
24 | /** |
||
25 | * |
||
26 | * @var int |
||
27 | */ |
||
28 | private $imageWidth; |
||
29 | |||
30 | /** |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | private $relatedTopics; |
||
35 | |||
36 | /** |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | private $entity; |
||
41 | |||
42 | /** |
||
43 | * |
||
44 | * @var Metadata |
||
45 | */ |
||
46 | private $meta; |
||
47 | |||
48 | /** |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | private $type; |
||
53 | |||
54 | /** |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | private $redirect; |
||
59 | |||
60 | /** |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | private $definitionURL; |
||
65 | |||
66 | /** |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | private $abstractURL; |
||
71 | |||
72 | /** |
||
73 | * |
||
74 | * @var string |
||
75 | */ |
||
76 | private $definition; |
||
77 | |||
78 | /** |
||
79 | * |
||
80 | * @var string |
||
81 | */ |
||
82 | private $abstractSource; |
||
83 | |||
84 | /** |
||
85 | * |
||
86 | * @var string |
||
87 | */ |
||
88 | private $infobox; |
||
89 | |||
90 | /** |
||
91 | * |
||
92 | * @var string |
||
93 | */ |
||
94 | private $image; |
||
95 | |||
96 | /** |
||
97 | * |
||
98 | * @var bool |
||
99 | */ |
||
100 | private $imageIsLogo; |
||
101 | |||
102 | /** |
||
103 | * |
||
104 | * @var string |
||
105 | */ |
||
106 | private $abstract; |
||
107 | |||
108 | /** |
||
109 | * |
||
110 | * @var string |
||
111 | */ |
||
112 | private $abstractText; |
||
113 | |||
114 | /** |
||
115 | * |
||
116 | * @var string |
||
117 | */ |
||
118 | private $answerType; |
||
119 | |||
120 | /** |
||
121 | * |
||
122 | * @var int |
||
123 | */ |
||
124 | private $imageHeight; |
||
125 | |||
126 | /** |
||
127 | * |
||
128 | * @var string |
||
129 | */ |
||
130 | private $answer; |
||
131 | |||
132 | /** |
||
133 | * |
||
134 | * @var array |
||
135 | */ |
||
136 | private $results; |
||
137 | |||
138 | /** |
||
139 | * |
||
140 | * @return string |
||
141 | */ |
||
142 | 1 | public function getDefinitionSource(): string |
|
143 | { |
||
144 | 1 | return $this->definitionSource; |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * |
||
149 | * @param string $definitionSource |
||
150 | * @return QueryResult |
||
151 | */ |
||
152 | 3 | public function setDefinitionSource(string $definitionSource): QueryResult |
|
157 | |||
158 | /** |
||
159 | * |
||
160 | * @return string |
||
161 | */ |
||
162 | 1 | public function getHeading(): string |
|
163 | { |
||
164 | 1 | return $this->heading; |
|
165 | } |
||
166 | |||
167 | /** |
||
168 | * |
||
169 | * @param string $heading |
||
170 | * @return QueryResult |
||
171 | */ |
||
172 | 3 | public function setHeading(string $heading): QueryResult |
|
177 | |||
178 | /** |
||
179 | * |
||
180 | * @return int |
||
181 | */ |
||
182 | 1 | public function getImageWidth(): int |
|
183 | { |
||
184 | 1 | return $this->imageWidth; |
|
185 | } |
||
186 | |||
187 | /** |
||
188 | * |
||
189 | * @param int $imageWidth |
||
190 | * @return QueryResult |
||
191 | */ |
||
192 | 3 | public function setImageWidth(int $imageWidth): QueryResult |
|
197 | |||
198 | /** |
||
199 | * |
||
200 | * @return mixed |
||
201 | */ |
||
202 | 1 | public function getEntity(): string |
|
203 | { |
||
204 | 1 | return $this->entity; |
|
205 | } |
||
206 | |||
207 | /** |
||
208 | * |
||
209 | * @param string $entity |
||
210 | * @return QueryResult |
||
211 | */ |
||
212 | 3 | public function setEntity(string $entity): QueryResult |
|
217 | |||
218 | /** |
||
219 | * |
||
220 | * @return Metadata |
||
221 | */ |
||
222 | 1 | public function getMeta(): Metadata |
|
223 | { |
||
224 | 1 | return $this->meta == null ? new Metadata() : $this->meta; |
|
225 | } |
||
226 | |||
227 | /** |
||
228 | * |
||
229 | * @param Metadata $meta |
||
230 | * @return QueryResult |
||
231 | */ |
||
232 | public function setMeta(Metadata $meta): QueryResult |
||
237 | |||
238 | /** |
||
239 | * |
||
240 | * @return string |
||
241 | */ |
||
242 | 1 | public function getType(): string |
|
243 | { |
||
244 | 1 | return $this->type; |
|
245 | } |
||
246 | |||
247 | /** |
||
248 | * |
||
249 | * @param string $type |
||
250 | * @return QueryResult |
||
251 | */ |
||
252 | 3 | public function setType(string $type): QueryResult |
|
257 | |||
258 | /** |
||
259 | * |
||
260 | * @return string |
||
261 | */ |
||
262 | 1 | public function getRedirect(): string |
|
263 | { |
||
264 | 1 | return $this->redirect; |
|
265 | } |
||
266 | |||
267 | /** |
||
268 | * |
||
269 | * @param string $redirect |
||
270 | * @return QueryResult |
||
271 | */ |
||
272 | 3 | public function setRedirect(string $redirect): QueryResult |
|
277 | |||
278 | /** |
||
279 | * |
||
280 | * @return string |
||
281 | */ |
||
282 | 1 | public function getDefinitionURL(): string |
|
283 | { |
||
284 | 1 | return $this->definitionURL; |
|
285 | } |
||
286 | |||
287 | /** |
||
288 | * |
||
289 | * @param string $definitionURL |
||
290 | * @return QueryResult |
||
291 | */ |
||
292 | 3 | public function setDefinitionURL(string $definitionURL): QueryResult |
|
297 | |||
298 | /** |
||
299 | * |
||
300 | * @return string |
||
301 | */ |
||
302 | 1 | public function getAbstractURL(): string |
|
303 | { |
||
304 | 1 | return $this->abstractURL; |
|
305 | } |
||
306 | |||
307 | /** |
||
308 | * |
||
309 | * @param string $abstractURL |
||
310 | * @return QueryResult |
||
311 | */ |
||
312 | 3 | public function setAbstractURL(string $abstractURL): QueryResult |
|
317 | |||
318 | /** |
||
319 | * |
||
320 | * @return string |
||
321 | */ |
||
322 | 1 | public function getDefinition(): string |
|
323 | { |
||
324 | 1 | return $this->definition; |
|
325 | } |
||
326 | |||
327 | /** |
||
328 | * |
||
329 | * @param string $definition |
||
330 | * @return QueryResult |
||
331 | */ |
||
332 | 3 | public function setDefinition(string $definition): QueryResult |
|
337 | |||
338 | /** |
||
339 | * |
||
340 | * @return string |
||
341 | */ |
||
342 | 1 | public function getAbstractSource(): string |
|
343 | { |
||
344 | 1 | return $this->abstractSource; |
|
345 | } |
||
346 | |||
347 | /** |
||
348 | * |
||
349 | * @param string $abstractSource |
||
350 | * @return QueryResult |
||
351 | */ |
||
352 | 3 | public function setAbstractSource(string $abstractSource): QueryResult |
|
357 | |||
358 | /** |
||
359 | * |
||
360 | * @return string |
||
361 | */ |
||
362 | 1 | public function getInfobox(): string |
|
363 | { |
||
364 | 1 | return $this->infobox; |
|
365 | } |
||
366 | |||
367 | /** |
||
368 | * |
||
369 | * @param string $infobox |
||
370 | * @return QueryResult |
||
371 | */ |
||
372 | 3 | public function setInfobox(string $infobox): QueryResult |
|
377 | |||
378 | /** |
||
379 | * |
||
380 | * @return string |
||
381 | */ |
||
382 | 1 | public function getImage(): string |
|
383 | { |
||
384 | 1 | return $this->image; |
|
385 | } |
||
386 | |||
387 | /** |
||
388 | * |
||
389 | * @param string $image |
||
390 | * @return QueryResult |
||
391 | */ |
||
392 | 3 | public function setImage(string $image): QueryResult |
|
397 | |||
398 | /** |
||
399 | * |
||
400 | * @return bool |
||
401 | */ |
||
402 | 1 | public function isImageIsLogo(): bool |
|
403 | { |
||
404 | 1 | return $this->imageIsLogo; |
|
405 | } |
||
406 | |||
407 | /** |
||
408 | * |
||
409 | * @param bool $imageIsLogo |
||
410 | * @return QueryResult |
||
411 | */ |
||
412 | 3 | public function setImageIsLogo(bool $imageIsLogo): QueryResult |
|
417 | |||
418 | /** |
||
419 | * |
||
420 | * @return string |
||
421 | */ |
||
422 | 1 | public function getAbstract(): string |
|
423 | { |
||
424 | 1 | return $this->abstract; |
|
425 | } |
||
426 | |||
427 | /** |
||
428 | * |
||
429 | * @param string $abstract |
||
430 | * @return QueryResult |
||
431 | */ |
||
432 | 3 | public function setAbstract(string $abstract): QueryResult |
|
437 | |||
438 | /** |
||
439 | * |
||
440 | * @return string |
||
441 | */ |
||
442 | 1 | public function getAbstractText(): string |
|
443 | { |
||
444 | 1 | return $this->abstractText; |
|
445 | } |
||
446 | |||
447 | /** |
||
448 | * |
||
449 | * @param string $abstractText |
||
450 | * @return QueryResult |
||
451 | */ |
||
452 | 3 | public function setAbstractText(string $abstractText): QueryResult |
|
457 | |||
458 | /** |
||
459 | * |
||
460 | * @return string |
||
461 | */ |
||
462 | 1 | public function getAnswerType(): string |
|
463 | { |
||
464 | 1 | return $this->answerType; |
|
465 | } |
||
466 | |||
467 | /** |
||
468 | * |
||
469 | * @param string $answerType |
||
470 | * @return QueryResult |
||
471 | */ |
||
472 | 3 | public function setAnswerType(string $answerType): QueryResult |
|
477 | |||
478 | /** |
||
479 | * |
||
480 | * @return int |
||
481 | */ |
||
482 | 1 | public function getImageHeight(): int |
|
483 | { |
||
484 | 1 | return $this->imageHeight; |
|
485 | } |
||
486 | |||
487 | /** |
||
488 | * |
||
489 | * @param int $imageHeight |
||
490 | * @return QueryResult |
||
491 | */ |
||
492 | 3 | public function setImageHeight(int $imageHeight): QueryResult |
|
497 | |||
498 | /** |
||
499 | * |
||
500 | * @return string |
||
501 | */ |
||
502 | 1 | public function getAnswer(): string |
|
503 | { |
||
504 | 1 | return $this->answer; |
|
505 | } |
||
506 | |||
507 | /** |
||
508 | * |
||
509 | * @param string $answer |
||
510 | * @return QueryResult |
||
511 | */ |
||
512 | 3 | public function setAnswer($answer): QueryResult |
|
517 | |||
518 | /** |
||
519 | * |
||
520 | * @return array |
||
521 | */ |
||
522 | 2 | public function getRelatedTopics(): array |
|
526 | |||
527 | /** |
||
528 | * |
||
529 | * @return array |
||
530 | */ |
||
531 | 2 | public function getResults(): array |
|
532 | { |
||
533 | 2 | return $this->results == null ? array() : $this->results; |
|
534 | } |
||
535 | |||
536 | /** |
||
537 | * |
||
538 | * @param Result $result |
||
539 | * @return QueryResult |
||
540 | */ |
||
541 | 1 | public function addResult(Result $result): QueryResult |
|
542 | { |
||
543 | 1 | $this->results[] = $result; |
|
544 | 1 | return $this; |
|
545 | } |
||
546 | |||
547 | /** |
||
548 | * |
||
549 | * @param Result $topic |
||
550 | * @return QueryResult |
||
551 | */ |
||
552 | 3 | public function addRelatedTopic(Result $topic): QueryResult |
|
557 | } |