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 int */ |
||
58 | private $page = 1; |
||
59 | |||
60 | /** @var int */ |
||
61 | private $size = 10; |
||
62 | |||
63 | /** @var string */ |
||
64 | private $author; |
||
65 | |||
66 | /** @var array */ |
||
67 | private $tags = []; |
||
68 | |||
69 | /** @var array */ |
||
70 | public $metaTags = []; |
||
71 | |||
72 | /** @var array */ |
||
73 | public $subTags = []; |
||
74 | |||
75 | /** @var array */ |
||
76 | private $options; |
||
77 | |||
78 | /** @var array */ |
||
79 | private $parts = []; |
||
80 | |||
81 | /** @var array */ |
||
82 | private $fields = []; |
||
83 | |||
84 | /** @var array */ |
||
85 | private $limitFields = []; |
||
86 | |||
87 | /** @var array */ |
||
88 | private $wildcardFields = []; |
||
89 | |||
90 | // /** @var array */ |
||
91 | // private $wildcardQueries = []; |
||
92 | |||
93 | /** @var array */ |
||
94 | private $wildcardFilters = []; |
||
95 | |||
96 | /** @var array */ |
||
97 | private $regexFilters = []; |
||
98 | |||
99 | /** @var array */ |
||
100 | private $simpleQueries = []; |
||
101 | |||
102 | |||
103 | /** |
||
104 | * SearchRequest constructor. |
||
105 | */ |
||
106 | public function __construct() { |
||
108 | |||
109 | |||
110 | /** |
||
111 | * @return array |
||
112 | */ |
||
113 | public function getProviders(): array { |
||
116 | |||
117 | /** |
||
118 | * @param array $providers |
||
119 | * |
||
120 | * @return ISearchRequest |
||
|
|||
121 | */ |
||
122 | public function setProviders(array $providers): ISearchRequest { |
||
127 | |||
128 | |||
129 | /** |
||
130 | * @return string |
||
131 | */ |
||
132 | public function getAuthor(): string { |
||
135 | |||
136 | /** |
||
137 | * @param string $author |
||
138 | * |
||
139 | * @return ISearchRequest |
||
140 | */ |
||
141 | public function setAuthor(string $author): ISearchRequest { |
||
146 | |||
147 | |||
148 | /** |
||
149 | * @return string |
||
150 | */ |
||
151 | public function getSearch(): string { |
||
154 | |||
155 | /** |
||
156 | * @param string $search |
||
157 | * |
||
158 | * @return ISearchRequest |
||
159 | */ |
||
160 | public function setSearch(string $search): ISearchRequest { |
||
165 | |||
166 | |||
167 | /** |
||
168 | * |
||
169 | */ |
||
170 | public function cleanSearch(): ISearchRequest { |
||
187 | |||
188 | |||
189 | /** |
||
190 | * @param string $word |
||
191 | * |
||
192 | * @return bool |
||
193 | */ |
||
194 | private function searchQueryOptions(string $word): bool { |
||
225 | |||
226 | |||
227 | /** |
||
228 | * @return int |
||
229 | */ |
||
230 | public function getPage(): int { |
||
233 | |||
234 | /** |
||
235 | * @param int $page |
||
236 | * |
||
237 | * @return ISearchRequest |
||
238 | */ |
||
239 | public function setPage(int $page): ISearchRequest { |
||
248 | |||
249 | |||
250 | /** |
||
251 | * @return int |
||
252 | */ |
||
253 | public function getSize(): int { |
||
256 | |||
257 | /** |
||
258 | * @param int $size |
||
259 | * |
||
260 | * @return ISearchRequest |
||
261 | */ |
||
262 | public function setSize(int $size): ISearchRequest { |
||
267 | |||
268 | |||
269 | /** |
||
270 | * @return array |
||
271 | */ |
||
272 | public function getOptions(): array { |
||
275 | |||
276 | /** |
||
277 | * @param array $options |
||
278 | * |
||
279 | * @return ISearchRequest |
||
280 | */ |
||
281 | public function setOptions(array $options): ISearchRequest { |
||
286 | |||
287 | /** |
||
288 | * @param $option |
||
289 | * @param $value |
||
290 | * |
||
291 | * @return ISearchRequest |
||
292 | */ |
||
293 | public function addOption(string $option, string $value): ISearchRequest { |
||
298 | |||
299 | /** |
||
300 | * @param string $option |
||
301 | * @param array $value |
||
302 | * |
||
303 | * @return ISearchRequest |
||
304 | */ |
||
305 | public function addOptionArray(string $option, array $value): ISearchRequest { |
||
310 | |||
311 | /** |
||
312 | * @param string $option |
||
313 | * @param bool $value |
||
314 | * |
||
315 | * @return ISearchRequest |
||
316 | */ |
||
317 | public function addOptionBool(string $option, bool $value): ISearchRequest { |
||
322 | |||
323 | /** |
||
324 | * @param string $option |
||
325 | * @param string $value |
||
326 | * |
||
327 | * @return ISearchRequest |
||
328 | */ |
||
329 | public function addMultipleOption(string $option, string $value): ISearchRequest { |
||
338 | |||
339 | /** |
||
340 | * @param string $option |
||
341 | * @param string $default |
||
342 | * |
||
343 | * @return string |
||
344 | */ |
||
345 | public function getOption(string $option, string $default = ''): string { |
||
348 | |||
349 | |||
350 | /** |
||
351 | * @param string $option |
||
352 | * @param array $default |
||
353 | * |
||
354 | * @return array |
||
355 | */ |
||
356 | public function getOptionArray(string $option, array $default = []): array { |
||
359 | |||
360 | |||
361 | /** |
||
362 | * @param string $part |
||
363 | * |
||
364 | * @return ISearchRequest |
||
365 | */ |
||
366 | public function addPart(string $part): ISearchRequest { |
||
371 | |||
372 | /** |
||
373 | * @return array |
||
374 | */ |
||
375 | public function getParts(): array { |
||
378 | |||
379 | |||
380 | /** |
||
381 | * @param array $parts |
||
382 | * |
||
383 | * @return ISearchRequest |
||
384 | * @since 15.0.0 |
||
385 | * |
||
386 | */ |
||
387 | public function setParts(array $parts): ISearchRequest { |
||
392 | |||
393 | |||
394 | /** |
||
395 | * @return array |
||
396 | */ |
||
397 | public function getFields(): array { |
||
400 | |||
401 | /** |
||
402 | * @param array $fields |
||
403 | * |
||
404 | * @return ISearchRequest |
||
405 | */ |
||
406 | public function setFields(array $fields): ISearchRequest { |
||
411 | |||
412 | |||
413 | /** |
||
414 | * @param string $field |
||
415 | * |
||
416 | * @return ISearchRequest |
||
417 | */ |
||
418 | public function addLimitField(string $field): ISearchRequest { |
||
423 | |||
424 | /** |
||
425 | * @return array |
||
426 | */ |
||
427 | public function getLimitFields(): array { |
||
430 | |||
431 | |||
432 | /** |
||
433 | * @param string $field |
||
434 | * |
||
435 | * @return ISearchRequest |
||
436 | */ |
||
437 | public function addField(string $field): ISearchRequest { |
||
442 | |||
443 | |||
444 | /** |
||
445 | * @param string $tag |
||
446 | * |
||
447 | * @return ISearchRequest |
||
448 | */ |
||
449 | public function addTag(string $tag): ISearchRequest { |
||
454 | |||
455 | /** |
||
456 | * @return array |
||
457 | */ |
||
458 | public function getTags(): array { |
||
461 | |||
462 | /** |
||
463 | * @param array $tags |
||
464 | * |
||
465 | * @return ISearchRequest |
||
466 | */ |
||
467 | public function setTags(array $tags): ISearchRequest { |
||
472 | |||
473 | |||
474 | /** |
||
475 | * @param array $tags |
||
476 | * |
||
477 | * @return ISearchRequest |
||
478 | */ |
||
479 | public function setMetaTags(array $tags): ISearchRequest { |
||
484 | |||
485 | /** |
||
486 | * @return array |
||
487 | */ |
||
488 | public function getMetaTags(): array { |
||
491 | |||
492 | /** |
||
493 | * @param string $tag |
||
494 | * |
||
495 | * @return ISearchRequest |
||
496 | */ |
||
497 | public function addMetaTag(string $tag): ISearchRequest { |
||
502 | |||
503 | |||
504 | /** |
||
505 | * @param array $tags |
||
506 | * |
||
507 | * @return ISearchRequest |
||
508 | */ |
||
509 | public function setSubTags(array $tags): ISearchRequest { |
||
514 | |||
515 | /** |
||
516 | * @param bool $formatted |
||
517 | * |
||
518 | * @return array |
||
519 | */ |
||
520 | public function getSubTags(bool $formatted = false): array { |
||
536 | |||
537 | /** |
||
538 | * @param string $source |
||
539 | * @param string $tag |
||
540 | * |
||
541 | * @return ISearchRequest |
||
542 | */ |
||
543 | public function addSubTag(string $source, string $tag): ISearchRequest { |
||
552 | |||
553 | |||
554 | /** |
||
555 | * @param string $field |
||
556 | * |
||
557 | * @return ISearchRequest |
||
558 | */ |
||
559 | public function addWildcardField(string $field): ISearchRequest { |
||
564 | |||
565 | |||
566 | /** |
||
567 | * @return array |
||
568 | */ |
||
569 | public function getWildcardFields(): array { |
||
572 | |||
573 | // |
||
574 | // /** |
||
575 | // * @param array $query |
||
576 | // * |
||
577 | // * @return ISearchRequest |
||
578 | // */ |
||
579 | // public function addWildcardQuery($query) { |
||
580 | // $this->addWildcardQueries([$query]); |
||
581 | // |
||
582 | // return $this; |
||
583 | // } |
||
584 | // |
||
585 | // /** |
||
586 | // * @param array $query |
||
587 | // * |
||
588 | // * @return ISearchRequest |
||
589 | // */ |
||
590 | // public function addWildcardQueries($query) { |
||
591 | // array_push($this->wildcardQueries, $query); |
||
592 | // |
||
593 | // return $this; |
||
594 | // } |
||
595 | // |
||
596 | // /** |
||
597 | // * @return array |
||
598 | // */ |
||
599 | // public function getWildcardQueries() { |
||
600 | // return $this->wildcardQueries; |
||
601 | // } |
||
602 | |||
603 | |||
604 | /** |
||
605 | * @param array $filter |
||
606 | * |
||
607 | * @return ISearchRequest |
||
608 | */ |
||
609 | public function addWildcardFilter(array $filter): ISearchRequest { |
||
614 | |||
615 | /** |
||
616 | * @param array $filters |
||
617 | * |
||
618 | * @return ISearchRequest |
||
619 | */ |
||
620 | public function addWildcardFilters(array $filters): ISearchRequest { |
||
625 | |||
626 | /** |
||
627 | * @return array |
||
628 | */ |
||
629 | public function getWildcardFilters(): array { |
||
632 | |||
633 | |||
634 | /** |
||
635 | * @param string $filter |
||
636 | * |
||
637 | * @return ISearchRequest |
||
638 | */ |
||
639 | public function addRegexFilter(string $filter): ISearchRequest { |
||
644 | |||
645 | /** |
||
646 | * @param array $filters |
||
647 | * |
||
648 | * @return ISearchRequest |
||
649 | */ |
||
650 | public function addRegexFilters(array $filters): ISearchRequest { |
||
655 | |||
656 | /** |
||
657 | * @return array |
||
658 | */ |
||
659 | public function getRegexFilters(): array { |
||
662 | |||
663 | |||
664 | /** |
||
665 | * @param ISearchRequestSimpleQuery $query |
||
666 | * |
||
667 | * @return ISearchRequest |
||
668 | */ |
||
669 | public function addSimpleQuery(ISearchRequestSimpleQuery $query): ISearchRequest { |
||
674 | |||
675 | |||
676 | /** |
||
677 | * @return ISearchRequestSimpleQuery[] |
||
678 | */ |
||
679 | public function getSimpleQueries(): array { |
||
682 | |||
683 | |||
684 | /** |
||
685 | * @return array |
||
686 | */ |
||
687 | public function jsonSerialize(): array { |
||
702 | |||
703 | |||
704 | /** |
||
705 | * @param array $arr |
||
706 | * |
||
707 | * @return SearchRequest |
||
708 | */ |
||
709 | public function importFromArray($arr): SearchRequest { |
||
728 | |||
729 | |||
730 | /** |
||
731 | * @param string $json |
||
732 | * |
||
733 | * @return SearchRequest |
||
734 | */ |
||
735 | public static function fromJSON(string $json): SearchRequest { |
||
741 | |||
742 | } |
||
743 |
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.