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 | * @param string $search |
||
| 168 | * |
||
| 169 | * @return ISearchRequest |
||
| 170 | */ |
||
| 171 | public function addSearch(string $search): ISearchRequest { |
||
| 176 | |||
| 177 | |||
| 178 | /** |
||
| 179 | * |
||
| 180 | */ |
||
| 181 | public function cleanSearch(): ISearchRequest { |
||
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * @param string $word |
||
| 202 | * |
||
| 203 | * @return bool |
||
| 204 | */ |
||
| 205 | private function searchQueryOptions(string $word): bool { |
||
| 236 | |||
| 237 | |||
| 238 | /** |
||
| 239 | * @return int |
||
| 240 | */ |
||
| 241 | public function getPage(): int { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param int $page |
||
| 247 | * |
||
| 248 | * @return ISearchRequest |
||
| 249 | */ |
||
| 250 | public function setPage(int $page): ISearchRequest { |
||
| 259 | |||
| 260 | |||
| 261 | /** |
||
| 262 | * @return int |
||
| 263 | */ |
||
| 264 | public function getSize(): int { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param int $size |
||
| 270 | * |
||
| 271 | * @return ISearchRequest |
||
| 272 | */ |
||
| 273 | public function setSize(int $size): ISearchRequest { |
||
| 278 | |||
| 279 | |||
| 280 | /** |
||
| 281 | * @return array |
||
| 282 | */ |
||
| 283 | public function getOptions(): array { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param array $options |
||
| 289 | * |
||
| 290 | * @return ISearchRequest |
||
| 291 | */ |
||
| 292 | public function setOptions(array $options): ISearchRequest { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param $option |
||
| 300 | * @param $value |
||
| 301 | * |
||
| 302 | * @return ISearchRequest |
||
| 303 | */ |
||
| 304 | public function addOption(string $option, string $value): ISearchRequest { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param string $option |
||
| 312 | * @param array $value |
||
| 313 | * |
||
| 314 | * @return ISearchRequest |
||
| 315 | */ |
||
| 316 | public function addOptionArray(string $option, array $value): ISearchRequest { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param string $option |
||
| 324 | * @param bool $value |
||
| 325 | * |
||
| 326 | * @return ISearchRequest |
||
| 327 | */ |
||
| 328 | public function addOptionBool(string $option, bool $value): ISearchRequest { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param string $option |
||
| 336 | * @param string $value |
||
| 337 | * |
||
| 338 | * @return ISearchRequest |
||
| 339 | */ |
||
| 340 | public function addMultipleOption(string $option, string $value): ISearchRequest { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @param string $option |
||
| 352 | * @param string $default |
||
| 353 | * |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | public function getOption(string $option, string $default = ''): string { |
||
| 359 | |||
| 360 | |||
| 361 | /** |
||
| 362 | * @param string $option |
||
| 363 | * @param array $default |
||
| 364 | * |
||
| 365 | * @return array |
||
| 366 | */ |
||
| 367 | public function getOptionArray(string $option, array $default = []): array { |
||
| 370 | |||
| 371 | |||
| 372 | /** |
||
| 373 | * @param string $part |
||
| 374 | * |
||
| 375 | * @return ISearchRequest |
||
| 376 | */ |
||
| 377 | public function addPart(string $part): ISearchRequest { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @return array |
||
| 385 | */ |
||
| 386 | public function getParts(): array { |
||
| 389 | |||
| 390 | |||
| 391 | /** |
||
| 392 | * @param array $parts |
||
| 393 | * |
||
| 394 | * @return ISearchRequest |
||
| 395 | * @since 15.0.0 |
||
| 396 | * |
||
| 397 | */ |
||
| 398 | public function setParts(array $parts): ISearchRequest { |
||
| 403 | |||
| 404 | |||
| 405 | /** |
||
| 406 | * @return array |
||
| 407 | */ |
||
| 408 | public function getFields(): array { |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @param array $fields |
||
| 414 | * |
||
| 415 | * @return ISearchRequest |
||
| 416 | */ |
||
| 417 | public function setFields(array $fields): ISearchRequest { |
||
| 422 | |||
| 423 | |||
| 424 | /** |
||
| 425 | * @param string $field |
||
| 426 | * |
||
| 427 | * @return ISearchRequest |
||
| 428 | */ |
||
| 429 | public function addLimitField(string $field): ISearchRequest { |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @return array |
||
| 437 | */ |
||
| 438 | public function getLimitFields(): array { |
||
| 441 | |||
| 442 | |||
| 443 | /** |
||
| 444 | * @param string $field |
||
| 445 | * |
||
| 446 | * @return ISearchRequest |
||
| 447 | */ |
||
| 448 | public function addField(string $field): ISearchRequest { |
||
| 453 | |||
| 454 | |||
| 455 | /** |
||
| 456 | * @param string $tag |
||
| 457 | * |
||
| 458 | * @return ISearchRequest |
||
| 459 | */ |
||
| 460 | public function addTag(string $tag): ISearchRequest { |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @return array |
||
| 468 | */ |
||
| 469 | public function getTags(): array { |
||
| 472 | |||
| 473 | /** |
||
| 474 | * @param array $tags |
||
| 475 | * |
||
| 476 | * @return ISearchRequest |
||
| 477 | */ |
||
| 478 | public function setTags(array $tags): ISearchRequest { |
||
| 483 | |||
| 484 | |||
| 485 | /** |
||
| 486 | * @param array $tags |
||
| 487 | * |
||
| 488 | * @return ISearchRequest |
||
| 489 | */ |
||
| 490 | public function setMetaTags(array $tags): ISearchRequest { |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @return array |
||
| 498 | */ |
||
| 499 | public function getMetaTags(): array { |
||
| 502 | |||
| 503 | /** |
||
| 504 | * @param string $tag |
||
| 505 | * |
||
| 506 | * @return ISearchRequest |
||
| 507 | */ |
||
| 508 | public function addMetaTag(string $tag): ISearchRequest { |
||
| 513 | |||
| 514 | |||
| 515 | /** |
||
| 516 | * @param array $tags |
||
| 517 | * |
||
| 518 | * @return ISearchRequest |
||
| 519 | */ |
||
| 520 | public function setSubTags(array $tags): ISearchRequest { |
||
| 525 | |||
| 526 | /** |
||
| 527 | * @param bool $formatted |
||
| 528 | * |
||
| 529 | * @return array |
||
| 530 | */ |
||
| 531 | public function getSubTags(bool $formatted = false): array { |
||
| 547 | |||
| 548 | /** |
||
| 549 | * @param string $source |
||
| 550 | * @param string $tag |
||
| 551 | * |
||
| 552 | * @return ISearchRequest |
||
| 553 | */ |
||
| 554 | public function addSubTag(string $source, string $tag): ISearchRequest { |
||
| 563 | |||
| 564 | |||
| 565 | /** |
||
| 566 | * @param string $field |
||
| 567 | * |
||
| 568 | * @return ISearchRequest |
||
| 569 | */ |
||
| 570 | public function addWildcardField(string $field): ISearchRequest { |
||
| 575 | |||
| 576 | |||
| 577 | /** |
||
| 578 | * @return array |
||
| 579 | */ |
||
| 580 | public function getWildcardFields(): array { |
||
| 583 | |||
| 584 | // |
||
| 585 | // /** |
||
| 586 | // * @param array $query |
||
| 587 | // * |
||
| 588 | // * @return ISearchRequest |
||
| 589 | // */ |
||
| 590 | // public function addWildcardQuery($query) { |
||
| 591 | // $this->addWildcardQueries([$query]); |
||
| 592 | // |
||
| 593 | // return $this; |
||
| 594 | // } |
||
| 595 | // |
||
| 596 | // /** |
||
| 597 | // * @param array $query |
||
| 598 | // * |
||
| 599 | // * @return ISearchRequest |
||
| 600 | // */ |
||
| 601 | // public function addWildcardQueries($query) { |
||
| 602 | // array_push($this->wildcardQueries, $query); |
||
| 603 | // |
||
| 604 | // return $this; |
||
| 605 | // } |
||
| 606 | // |
||
| 607 | // /** |
||
| 608 | // * @return array |
||
| 609 | // */ |
||
| 610 | // public function getWildcardQueries() { |
||
| 611 | // return $this->wildcardQueries; |
||
| 612 | // } |
||
| 613 | |||
| 614 | |||
| 615 | /** |
||
| 616 | * @param array $filter |
||
| 617 | * |
||
| 618 | * @return ISearchRequest |
||
| 619 | */ |
||
| 620 | public function addWildcardFilter(array $filter): ISearchRequest { |
||
| 625 | |||
| 626 | /** |
||
| 627 | * @param array $filters |
||
| 628 | * |
||
| 629 | * @return ISearchRequest |
||
| 630 | */ |
||
| 631 | public function addWildcardFilters(array $filters): ISearchRequest { |
||
| 636 | |||
| 637 | /** |
||
| 638 | * @return array |
||
| 639 | */ |
||
| 640 | public function getWildcardFilters(): array { |
||
| 643 | |||
| 644 | |||
| 645 | /** |
||
| 646 | * @param string $filter |
||
| 647 | * |
||
| 648 | * @return ISearchRequest |
||
| 649 | */ |
||
| 650 | public function addRegexFilter(string $filter): ISearchRequest { |
||
| 655 | |||
| 656 | /** |
||
| 657 | * @param array $filters |
||
| 658 | * |
||
| 659 | * @return ISearchRequest |
||
| 660 | */ |
||
| 661 | public function addRegexFilters(array $filters): ISearchRequest { |
||
| 666 | |||
| 667 | /** |
||
| 668 | * @return array |
||
| 669 | */ |
||
| 670 | public function getRegexFilters(): array { |
||
| 673 | |||
| 674 | |||
| 675 | /** |
||
| 676 | * @param ISearchRequestSimpleQuery $query |
||
| 677 | * |
||
| 678 | * @return ISearchRequest |
||
| 679 | */ |
||
| 680 | public function addSimpleQuery(ISearchRequestSimpleQuery $query): ISearchRequest { |
||
| 685 | |||
| 686 | |||
| 687 | /** |
||
| 688 | * @return ISearchRequestSimpleQuery[] |
||
| 689 | */ |
||
| 690 | public function getSimpleQueries(): array { |
||
| 693 | |||
| 694 | |||
| 695 | /** |
||
| 696 | * @return array |
||
| 697 | */ |
||
| 698 | public function jsonSerialize(): array { |
||
| 713 | |||
| 714 | |||
| 715 | /** |
||
| 716 | * @param array $arr |
||
| 717 | * |
||
| 718 | * @return SearchRequest |
||
| 719 | */ |
||
| 720 | public function importFromArray($arr): SearchRequest { |
||
| 739 | |||
| 740 | |||
| 741 | /** |
||
| 742 | * @param string $json |
||
| 743 | * |
||
| 744 | * @return SearchRequest |
||
| 745 | */ |
||
| 746 | public static function fromJSON(string $json): SearchRequest { |
||
| 752 | |||
| 753 | } |
||
| 754 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.