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 |
||
44 | class SearchRequest implements ISearchRequest, JsonSerializable { |
||
45 | |||
46 | |||
47 | use TArrayTools; |
||
48 | |||
49 | |||
50 | /** @var array */ |
||
51 | private $providers; |
||
52 | |||
53 | /** @var string */ |
||
54 | private $search; |
||
55 | |||
56 | /** @var int */ |
||
57 | private $page = 1; |
||
58 | |||
59 | /** @var int */ |
||
60 | private $size = 10; |
||
61 | |||
62 | /** @var string */ |
||
63 | private $author; |
||
64 | |||
65 | /** @var array */ |
||
66 | private $tags = []; |
||
67 | |||
68 | /** @var array */ |
||
69 | public $metaTags = []; |
||
70 | |||
71 | /** @var array */ |
||
72 | public $subTags = []; |
||
73 | |||
74 | /** @var array */ |
||
75 | private $options; |
||
76 | |||
77 | /** @var array */ |
||
78 | private $parts = []; |
||
79 | |||
80 | /** @var array */ |
||
81 | private $fields = []; |
||
82 | |||
83 | /** @var array */ |
||
84 | private $limitFields = []; |
||
85 | |||
86 | /** @var array */ |
||
87 | private $wildcardFields = []; |
||
88 | |||
89 | // /** @var array */ |
||
90 | // private $wildcardQueries = []; |
||
91 | |||
92 | /** @var array */ |
||
93 | private $wildcardFilters = []; |
||
94 | |||
95 | /** @var array */ |
||
96 | private $regexFilters = []; |
||
97 | |||
98 | |||
99 | /** |
||
100 | * SearchRequest constructor. |
||
101 | */ |
||
102 | public function __construct() { |
||
104 | |||
105 | |||
106 | /** |
||
107 | * @return array |
||
108 | */ |
||
109 | public function getProviders(): array { |
||
112 | |||
113 | /** |
||
114 | * @param array $providers |
||
115 | * |
||
116 | * @return ISearchRequest |
||
|
|||
117 | */ |
||
118 | public function setProviders(array $providers): ISearchRequest { |
||
123 | |||
124 | |||
125 | /** |
||
126 | * @return string |
||
127 | */ |
||
128 | public function getAuthor(): string { |
||
131 | |||
132 | /** |
||
133 | * @param string $author |
||
134 | * |
||
135 | * @return ISearchRequest |
||
136 | */ |
||
137 | public function setAuthor(string $author): ISearchRequest { |
||
142 | |||
143 | |||
144 | /** |
||
145 | * @return string |
||
146 | */ |
||
147 | public function getSearch(): string { |
||
150 | |||
151 | /** |
||
152 | * @param string $search |
||
153 | * |
||
154 | * @return ISearchRequest |
||
155 | */ |
||
156 | public function setSearch(string $search): ISearchRequest { |
||
161 | |||
162 | |||
163 | /** |
||
164 | * |
||
165 | */ |
||
166 | public function cleanSearch(): ISearchRequest { |
||
183 | |||
184 | |||
185 | /** |
||
186 | * @param string $word |
||
187 | * |
||
188 | * @return bool |
||
189 | */ |
||
190 | private function searchQueryOptions(string $word): bool { |
||
213 | |||
214 | |||
215 | /** |
||
216 | * @return int |
||
217 | */ |
||
218 | public function getPage(): int { |
||
221 | |||
222 | /** |
||
223 | * @param int $page |
||
224 | * |
||
225 | * @return ISearchRequest |
||
226 | */ |
||
227 | public function setPage(int $page): ISearchRequest { |
||
236 | |||
237 | |||
238 | /** |
||
239 | * @return int |
||
240 | */ |
||
241 | public function getSize(): int { |
||
244 | |||
245 | /** |
||
246 | * @param int $size |
||
247 | * |
||
248 | * @return ISearchRequest |
||
249 | */ |
||
250 | public function setSize(int $size): ISearchRequest { |
||
255 | |||
256 | |||
257 | /** |
||
258 | * @return array |
||
259 | */ |
||
260 | public function getOptions(): array { |
||
263 | |||
264 | /** |
||
265 | * @param array $options |
||
266 | * |
||
267 | * @return ISearchRequest |
||
268 | */ |
||
269 | public function setOptions(array $options): ISearchRequest { |
||
274 | |||
275 | /** |
||
276 | * @param $option |
||
277 | * @param $value |
||
278 | * |
||
279 | * @return ISearchRequest |
||
280 | */ |
||
281 | public function addOption(string $option, string $value): ISearchRequest { |
||
286 | |||
287 | /** |
||
288 | * @param string $option |
||
289 | * @param array $value |
||
290 | * |
||
291 | * @return ISearchRequest |
||
292 | */ |
||
293 | public function addOptionArray(string $option, array $value): ISearchRequest { |
||
298 | |||
299 | /** |
||
300 | * @param string $option |
||
301 | * @param bool $value |
||
302 | * |
||
303 | * @return ISearchRequest |
||
304 | */ |
||
305 | public function addOptionBool(string $option, bool $value): ISearchRequest { |
||
310 | |||
311 | /** |
||
312 | * @param string $option |
||
313 | * @param string $value |
||
314 | * |
||
315 | * @return ISearchRequest |
||
316 | */ |
||
317 | public function addMultipleOption(string $option, string $value): ISearchRequest { |
||
326 | |||
327 | /** |
||
328 | * @param string $option |
||
329 | * @param string $default |
||
330 | * |
||
331 | * @return string |
||
332 | */ |
||
333 | public function getOption(string $option, string $default = ''): string { |
||
336 | |||
337 | |||
338 | /** |
||
339 | * @param string $option |
||
340 | * @param array $default |
||
341 | * |
||
342 | * @return array |
||
343 | */ |
||
344 | public function getOptionArray(string $option, array $default = []): array { |
||
347 | |||
348 | |||
349 | /** |
||
350 | * @param string $part |
||
351 | * |
||
352 | * @return ISearchRequest |
||
353 | */ |
||
354 | public function addPart(string $part): ISearchRequest { |
||
359 | |||
360 | /** |
||
361 | * @return array |
||
362 | */ |
||
363 | public function getParts(): array { |
||
366 | |||
367 | |||
368 | /** |
||
369 | * @since 15.0.0 |
||
370 | * |
||
371 | * @param array $parts |
||
372 | * |
||
373 | * @return ISearchRequest |
||
374 | */ |
||
375 | public function setParts(array $parts): ISearchRequest { |
||
380 | |||
381 | |||
382 | /** |
||
383 | * @return array |
||
384 | */ |
||
385 | public function getFields(): array { |
||
388 | |||
389 | /** |
||
390 | * @param array $fields |
||
391 | * |
||
392 | * @return ISearchRequest |
||
393 | */ |
||
394 | public function setFields(array $fields): ISearchRequest { |
||
399 | |||
400 | |||
401 | /** |
||
402 | * @param string $field |
||
403 | * |
||
404 | * @return ISearchRequest |
||
405 | */ |
||
406 | public function addLimitField(string $field): ISearchRequest { |
||
411 | |||
412 | /** |
||
413 | * @return array |
||
414 | */ |
||
415 | public function getLimitFields(): array { |
||
418 | |||
419 | |||
420 | /** |
||
421 | * @param string $field |
||
422 | * |
||
423 | * @return ISearchRequest |
||
424 | */ |
||
425 | public function addField(string $field): ISearchRequest { |
||
430 | |||
431 | |||
432 | /** |
||
433 | * @param string $tag |
||
434 | * |
||
435 | * @return ISearchRequest |
||
436 | */ |
||
437 | public function addTag(string $tag): ISearchRequest { |
||
442 | |||
443 | /** |
||
444 | * @return array |
||
445 | */ |
||
446 | public function getTags(): array { |
||
449 | |||
450 | /** |
||
451 | * @param array $tags |
||
452 | * |
||
453 | * @return ISearchRequest |
||
454 | */ |
||
455 | public function setTags(array $tags): ISearchRequest { |
||
460 | |||
461 | |||
462 | /** |
||
463 | * @param array $tags |
||
464 | * |
||
465 | * @return ISearchRequest |
||
466 | */ |
||
467 | public function setMetaTags(array $tags): ISearchRequest { |
||
472 | |||
473 | /** |
||
474 | * @return array |
||
475 | */ |
||
476 | public function getMetaTags(): array { |
||
479 | |||
480 | /** |
||
481 | * @param string $tag |
||
482 | * |
||
483 | * @return ISearchRequest |
||
484 | */ |
||
485 | public function addMetaTag(string $tag): ISearchRequest { |
||
490 | |||
491 | |||
492 | /** |
||
493 | * @param array $tags |
||
494 | * |
||
495 | * @return ISearchRequest |
||
496 | */ |
||
497 | public function setSubTags(array $tags): ISearchRequest { |
||
502 | |||
503 | /** |
||
504 | * @param bool $formatted |
||
505 | * |
||
506 | * @return array |
||
507 | */ |
||
508 | public function getSubTags(bool $formatted = false): array { |
||
524 | |||
525 | /** |
||
526 | * @param string $source |
||
527 | * @param string $tag |
||
528 | * |
||
529 | * @return ISearchRequest |
||
530 | */ |
||
531 | public function addSubTag(string $source, string $tag): ISearchRequest { |
||
540 | |||
541 | |||
542 | /** |
||
543 | * @param string $field |
||
544 | * |
||
545 | * @return ISearchRequest |
||
546 | */ |
||
547 | public function addWildcardField(string $field): ISearchRequest { |
||
552 | |||
553 | |||
554 | /** |
||
555 | * @return array |
||
556 | */ |
||
557 | public function getWildcardFields(): array { |
||
560 | |||
561 | // |
||
562 | // /** |
||
563 | // * @param array $query |
||
564 | // * |
||
565 | // * @return ISearchRequest |
||
566 | // */ |
||
567 | // public function addWildcardQuery($query) { |
||
568 | // $this->addWildcardQueries([$query]); |
||
569 | // |
||
570 | // return $this; |
||
571 | // } |
||
572 | // |
||
573 | // /** |
||
574 | // * @param array $query |
||
575 | // * |
||
576 | // * @return ISearchRequest |
||
577 | // */ |
||
578 | // public function addWildcardQueries($query) { |
||
579 | // array_push($this->wildcardQueries, $query); |
||
580 | // |
||
581 | // return $this; |
||
582 | // } |
||
583 | // |
||
584 | // /** |
||
585 | // * @return array |
||
586 | // */ |
||
587 | // public function getWildcardQueries() { |
||
588 | // return $this->wildcardQueries; |
||
589 | // } |
||
590 | |||
591 | |||
592 | /** |
||
593 | * @param array $filter |
||
594 | * |
||
595 | * @return ISearchRequest |
||
596 | */ |
||
597 | public function addWildcardFilter(array $filter): ISearchRequest { |
||
602 | |||
603 | /** |
||
604 | * @param array $filters |
||
605 | * |
||
606 | * @return ISearchRequest |
||
607 | */ |
||
608 | public function addWildcardFilters(array $filters): ISearchRequest { |
||
613 | |||
614 | /** |
||
615 | * @return array |
||
616 | */ |
||
617 | public function getWildcardFilters(): array { |
||
620 | |||
621 | |||
622 | /** |
||
623 | * @param string $filter |
||
624 | * |
||
625 | * @return ISearchRequest |
||
626 | */ |
||
627 | public function addRegexFilter(string $filter): ISearchRequest { |
||
632 | |||
633 | /** |
||
634 | * @param array $filters |
||
635 | * |
||
636 | * @return ISearchRequest |
||
637 | */ |
||
638 | public function addRegexFilters(array $filters): ISearchRequest { |
||
643 | |||
644 | /** |
||
645 | * @return array |
||
646 | */ |
||
647 | public function getRegexFilters(): array { |
||
650 | |||
651 | |||
652 | /** |
||
653 | * @return array |
||
654 | */ |
||
655 | public function jsonSerialize(): array { |
||
669 | |||
670 | |||
671 | /** |
||
672 | * @param array $arr |
||
673 | * |
||
674 | * @return SearchRequest |
||
675 | */ |
||
676 | public function importFromArray($arr): SearchRequest { |
||
695 | |||
696 | |||
697 | /** |
||
698 | * @param string $json |
||
699 | * |
||
700 | * @return SearchRequest |
||
701 | */ |
||
702 | public static function fromJSON(string $json): SearchRequest { |
||
708 | |||
709 | } |
||
710 |
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.