Complex classes like SearchRequest 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 SearchRequest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
45 | class SearchRequest implements ISearchRequest, JsonSerializable { |
||
46 | |||
47 | |||
48 | use TArrayTools; |
||
49 | |||
50 | |||
51 | /** @var array */ |
||
52 | private $providers; |
||
53 | |||
54 | /** @var string */ |
||
55 | private $search; |
||
56 | |||
57 | /** @var bool */ |
||
58 | private $emptySearch = false; |
||
59 | |||
60 | /** @var int */ |
||
61 | private $page = 1; |
||
62 | |||
63 | /** @var int */ |
||
64 | private $size = 10; |
||
65 | |||
66 | /** @var string */ |
||
67 | private $author; |
||
68 | |||
69 | /** @var array */ |
||
70 | private $tags = []; |
||
71 | |||
72 | /** @var array */ |
||
73 | public $metaTags = []; |
||
74 | |||
75 | /** @var array */ |
||
76 | public $subTags = []; |
||
77 | |||
78 | /** @var array */ |
||
79 | private $options; |
||
80 | |||
81 | /** @var array */ |
||
82 | private $parts = []; |
||
83 | |||
84 | /** @var array */ |
||
85 | private $fields = []; |
||
86 | |||
87 | /** @var array */ |
||
88 | private $limitFields = []; |
||
89 | |||
90 | /** @var array */ |
||
91 | private $wildcardFields = []; |
||
92 | |||
93 | // /** @var array */ |
||
94 | // private $wildcardQueries = []; |
||
95 | |||
96 | /** @var array */ |
||
97 | private $wildcardFilters = []; |
||
98 | |||
99 | /** @var array */ |
||
100 | private $regexFilters = []; |
||
101 | |||
102 | /** @var array */ |
||
103 | private $simpleQueries = []; |
||
104 | |||
105 | |||
106 | /** |
||
107 | * SearchRequest constructor. |
||
108 | */ |
||
109 | public function __construct() { |
||
111 | |||
112 | |||
113 | /** |
||
114 | * @return array |
||
115 | */ |
||
116 | public function getProviders(): array { |
||
119 | |||
120 | /** |
||
121 | * @param array $providers |
||
122 | * |
||
123 | * @return ISearchRequest |
||
|
|||
124 | */ |
||
125 | public function setProviders(array $providers): ISearchRequest { |
||
130 | |||
131 | |||
132 | /** |
||
133 | * @return string |
||
134 | */ |
||
135 | public function getAuthor(): string { |
||
138 | |||
139 | /** |
||
140 | * @param string $author |
||
141 | * |
||
142 | * @return ISearchRequest |
||
143 | */ |
||
144 | public function setAuthor(string $author): ISearchRequest { |
||
149 | |||
150 | |||
151 | /** |
||
152 | * @return string |
||
153 | */ |
||
154 | public function getSearch(): string { |
||
157 | |||
158 | /** |
||
159 | * @param string $search |
||
160 | * |
||
161 | * @return ISearchRequest |
||
162 | */ |
||
163 | public function setSearch(string $search): ISearchRequest { |
||
168 | |||
169 | /** |
||
170 | * @param string $search |
||
171 | * |
||
172 | * @return ISearchRequest |
||
173 | */ |
||
174 | public function addSearch(string $search): ISearchRequest { |
||
179 | |||
180 | |||
181 | /** |
||
182 | * @return bool |
||
183 | */ |
||
184 | public function isEmptySearch(): bool { |
||
187 | |||
188 | /** |
||
189 | * @param bool $emptySearch |
||
190 | * |
||
191 | * @return ISearchRequest |
||
192 | */ |
||
193 | public function setEmptySearch(bool $emptySearch): ISearchRequest { |
||
198 | |||
199 | |||
200 | /** |
||
201 | * |
||
202 | */ |
||
203 | public function cleanSearch(): ISearchRequest { |
||
220 | |||
221 | |||
222 | /** |
||
223 | * @param string $word |
||
224 | * |
||
225 | * @return bool |
||
226 | */ |
||
227 | private function searchQueryOptions(string $word): bool { |
||
258 | |||
259 | |||
260 | /** |
||
261 | * @return int |
||
262 | */ |
||
263 | public function getPage(): int { |
||
266 | |||
267 | /** |
||
268 | * @param int $page |
||
269 | * |
||
270 | * @return ISearchRequest |
||
271 | */ |
||
272 | public function setPage(int $page): ISearchRequest { |
||
281 | |||
282 | |||
283 | /** |
||
284 | * @return int |
||
285 | */ |
||
286 | public function getSize(): int { |
||
289 | |||
290 | /** |
||
291 | * @param int $size |
||
292 | * |
||
293 | * @return ISearchRequest |
||
294 | */ |
||
295 | public function setSize(int $size): ISearchRequest { |
||
300 | |||
301 | |||
302 | /** |
||
303 | * @return array |
||
304 | */ |
||
305 | public function getOptions(): array { |
||
308 | |||
309 | /** |
||
310 | * @param array $options |
||
311 | * |
||
312 | * @return ISearchRequest |
||
313 | */ |
||
314 | public function setOptions(array $options): ISearchRequest { |
||
319 | |||
320 | /** |
||
321 | * @param $option |
||
322 | * @param $value |
||
323 | * |
||
324 | * @return ISearchRequest |
||
325 | */ |
||
326 | public function addOption(string $option, string $value): ISearchRequest { |
||
331 | |||
332 | /** |
||
333 | * @param string $option |
||
334 | * @param array $value |
||
335 | * |
||
336 | * @return ISearchRequest |
||
337 | */ |
||
338 | public function addOptionArray(string $option, array $value): ISearchRequest { |
||
343 | |||
344 | /** |
||
345 | * @param string $option |
||
346 | * @param bool $value |
||
347 | * |
||
348 | * @return ISearchRequest |
||
349 | */ |
||
350 | public function addOptionBool(string $option, bool $value): ISearchRequest { |
||
355 | |||
356 | /** |
||
357 | * @param string $option |
||
358 | * @param string $value |
||
359 | * |
||
360 | * @return ISearchRequest |
||
361 | */ |
||
362 | public function addMultipleOption(string $option, string $value): ISearchRequest { |
||
371 | |||
372 | /** |
||
373 | * @param string $option |
||
374 | * @param string $default |
||
375 | * |
||
376 | * @return string |
||
377 | */ |
||
378 | public function getOption(string $option, string $default = ''): string { |
||
381 | |||
382 | |||
383 | /** |
||
384 | * @param string $option |
||
385 | * @param array $default |
||
386 | * |
||
387 | * @return array |
||
388 | */ |
||
389 | public function getOptionArray(string $option, array $default = []): array { |
||
392 | |||
393 | |||
394 | /** |
||
395 | * @param string $part |
||
396 | * |
||
397 | * @return ISearchRequest |
||
398 | */ |
||
399 | public function addPart(string $part): ISearchRequest { |
||
404 | |||
405 | /** |
||
406 | * @return array |
||
407 | */ |
||
408 | public function getParts(): array { |
||
411 | |||
412 | |||
413 | /** |
||
414 | * @param array $parts |
||
415 | * |
||
416 | * @return ISearchRequest |
||
417 | * @since 15.0.0 |
||
418 | * |
||
419 | */ |
||
420 | public function setParts(array $parts): ISearchRequest { |
||
425 | |||
426 | |||
427 | /** |
||
428 | * @return array |
||
429 | */ |
||
430 | public function getFields(): array { |
||
433 | |||
434 | /** |
||
435 | * @param array $fields |
||
436 | * |
||
437 | * @return ISearchRequest |
||
438 | */ |
||
439 | public function setFields(array $fields): ISearchRequest { |
||
444 | |||
445 | |||
446 | /** |
||
447 | * @param string $field |
||
448 | * |
||
449 | * @return ISearchRequest |
||
450 | */ |
||
451 | public function addLimitField(string $field): ISearchRequest { |
||
456 | |||
457 | /** |
||
458 | * @return array |
||
459 | */ |
||
460 | public function getLimitFields(): array { |
||
463 | |||
464 | |||
465 | /** |
||
466 | * @param string $field |
||
467 | * |
||
468 | * @return ISearchRequest |
||
469 | */ |
||
470 | public function addField(string $field): ISearchRequest { |
||
475 | |||
476 | |||
477 | /** |
||
478 | * @param string $tag |
||
479 | * |
||
480 | * @return ISearchRequest |
||
481 | */ |
||
482 | public function addTag(string $tag): ISearchRequest { |
||
487 | |||
488 | /** |
||
489 | * @return array |
||
490 | */ |
||
491 | public function getTags(): array { |
||
494 | |||
495 | /** |
||
496 | * @param array $tags |
||
497 | * |
||
498 | * @return ISearchRequest |
||
499 | */ |
||
500 | public function setTags(array $tags): ISearchRequest { |
||
505 | |||
506 | |||
507 | /** |
||
508 | * @param array $tags |
||
509 | * |
||
510 | * @return ISearchRequest |
||
511 | */ |
||
512 | public function setMetaTags(array $tags): ISearchRequest { |
||
517 | |||
518 | /** |
||
519 | * @return array |
||
520 | */ |
||
521 | public function getMetaTags(): array { |
||
524 | |||
525 | /** |
||
526 | * @param string $tag |
||
527 | * |
||
528 | * @return ISearchRequest |
||
529 | */ |
||
530 | public function addMetaTag(string $tag): ISearchRequest { |
||
535 | |||
536 | |||
537 | /** |
||
538 | * @param array $tags |
||
539 | * |
||
540 | * @return ISearchRequest |
||
541 | */ |
||
542 | public function setSubTags(array $tags): ISearchRequest { |
||
547 | |||
548 | /** |
||
549 | * @param bool $formatted |
||
550 | * |
||
551 | * @return array |
||
552 | */ |
||
553 | public function getSubTags(bool $formatted = false): array { |
||
569 | |||
570 | /** |
||
571 | * @param string $source |
||
572 | * @param string $tag |
||
573 | * |
||
574 | * @return ISearchRequest |
||
575 | */ |
||
576 | public function addSubTag(string $source, string $tag): ISearchRequest { |
||
585 | |||
586 | |||
587 | /** |
||
588 | * @param string $field |
||
589 | * |
||
590 | * @return ISearchRequest |
||
591 | */ |
||
592 | public function addWildcardField(string $field): ISearchRequest { |
||
597 | |||
598 | |||
599 | /** |
||
600 | * @return array |
||
601 | */ |
||
602 | public function getWildcardFields(): array { |
||
605 | |||
606 | // |
||
607 | // /** |
||
608 | // * @param array $query |
||
609 | // * |
||
610 | // * @return ISearchRequest |
||
611 | // */ |
||
612 | // public function addWildcardQuery($query) { |
||
613 | // $this->addWildcardQueries([$query]); |
||
614 | // |
||
615 | // return $this; |
||
616 | // } |
||
617 | // |
||
618 | // /** |
||
619 | // * @param array $query |
||
620 | // * |
||
621 | // * @return ISearchRequest |
||
622 | // */ |
||
623 | // public function addWildcardQueries($query) { |
||
624 | // array_push($this->wildcardQueries, $query); |
||
625 | // |
||
626 | // return $this; |
||
627 | // } |
||
628 | // |
||
629 | // /** |
||
630 | // * @return array |
||
631 | // */ |
||
632 | // public function getWildcardQueries() { |
||
633 | // return $this->wildcardQueries; |
||
634 | // } |
||
635 | |||
636 | |||
637 | /** |
||
638 | * @param array $filter |
||
639 | * |
||
640 | * @return ISearchRequest |
||
641 | */ |
||
642 | public function addWildcardFilter(array $filter): ISearchRequest { |
||
647 | |||
648 | /** |
||
649 | * @param array $filters |
||
650 | * |
||
651 | * @return ISearchRequest |
||
652 | */ |
||
653 | public function addWildcardFilters(array $filters): ISearchRequest { |
||
658 | |||
659 | /** |
||
660 | * @return array |
||
661 | */ |
||
662 | public function getWildcardFilters(): array { |
||
665 | |||
666 | |||
667 | /** |
||
668 | * @param string $filter |
||
669 | * |
||
670 | * @return ISearchRequest |
||
671 | */ |
||
672 | public function addRegexFilter(string $filter): ISearchRequest { |
||
677 | |||
678 | /** |
||
679 | * @param array $filters |
||
680 | * |
||
681 | * @return ISearchRequest |
||
682 | */ |
||
683 | public function addRegexFilters(array $filters): ISearchRequest { |
||
688 | |||
689 | /** |
||
690 | * @return array |
||
691 | */ |
||
692 | public function getRegexFilters(): array { |
||
695 | |||
696 | |||
697 | /** |
||
698 | * @param ISearchRequestSimpleQuery $query |
||
699 | * |
||
700 | * @return ISearchRequest |
||
701 | */ |
||
702 | public function addSimpleQuery(ISearchRequestSimpleQuery $query): ISearchRequest { |
||
707 | |||
708 | |||
709 | /** |
||
710 | * @return ISearchRequestSimpleQuery[] |
||
711 | */ |
||
712 | public function getSimpleQueries(): array { |
||
715 | |||
716 | |||
717 | /** |
||
718 | * @return array |
||
719 | */ |
||
720 | public function jsonSerialize(): array { |
||
736 | |||
737 | |||
738 | /** |
||
739 | * @param array $arr |
||
740 | * |
||
741 | * @return SearchRequest |
||
742 | */ |
||
743 | public function importFromArray($arr): SearchRequest { |
||
772 | |||
773 | |||
774 | /** |
||
775 | * @param string $json |
||
776 | * |
||
777 | * @return SearchRequest |
||
778 | */ |
||
779 | public static function fromJSON(string $json): SearchRequest { |
||
785 | |||
786 | } |
||
787 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.