Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Query 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 Query, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class Query |
||
| 41 | { |
||
| 42 | /** @var string */ |
||
| 43 | private $query; |
||
| 44 | /** @var int */ |
||
| 45 | private $skip = 0; |
||
| 46 | /** @var int */ |
||
| 47 | private $pageSize = 10; |
||
| 48 | /** @var string */ |
||
| 49 | private $collection; |
||
| 50 | /** @var string */ |
||
| 51 | private $area; |
||
| 52 | /** @var string */ |
||
| 53 | private $biasingProfile; |
||
| 54 | /** @var string */ |
||
| 55 | private $language; |
||
| 56 | /** @var MSort[] */ |
||
| 57 | private $sort; |
||
| 58 | /** @var CustomUrlParam[] */ |
||
| 59 | private $customUrlParams = array(); |
||
| 60 | /** @var Navigation[] */ |
||
| 61 | private $navigations = array(); |
||
| 62 | /** @var string[] */ |
||
| 63 | private $includedNavigations = array(); |
||
| 64 | /** @var string[] */ |
||
| 65 | private $excludedNavigations = array(); |
||
| 66 | /** @var string[] */ |
||
| 67 | private $fields = array(); |
||
| 68 | /** @var string[] */ |
||
| 69 | private $orFields = array(); |
||
| 70 | /** @var bool */ |
||
| 71 | private $pruneRefinements = true; |
||
| 72 | /** @var bool */ |
||
| 73 | private $disableAutocorrection = false; |
||
| 74 | /** @var bool */ |
||
| 75 | private $wildcardSearchEnabled = false; |
||
| 76 | // Removed until CBOR support for serialization / de-serialization improves |
||
| 77 | // /** @var bool */ |
||
| 78 | // private $returnBinary = false; |
||
| 79 | /** @var RestrictNavigation */ |
||
| 80 | private $restrictNavigation; |
||
| 81 | |||
| 82 | /** @var Serializer */ |
||
| 83 | private $serializer; |
||
| 84 | |||
| 85 | const TILDE_REGEX = "/~((?=[\\w]*[=:]))/"; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param mixed $request |
||
| 89 | * |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | private function requestToJson($request) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param string $clientKey Your client key. |
||
| 106 | * |
||
| 107 | * @return string JSON representation of request to Bridge. |
||
| 108 | */ |
||
| 109 | public function getBridgeJson($clientKey) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param string $clientKey Your client key. |
||
| 117 | * @param string $navigationName Name of the navigation to get refinements for |
||
| 118 | * |
||
| 119 | * @return string JSON representation of request to Bridge. |
||
| 120 | */ |
||
| 121 | public function getBridgeRefinementsJson($clientKey, $navigationName) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param string $clientKey |
||
| 131 | * |
||
| 132 | * @return Request |
||
| 133 | */ |
||
| 134 | private function populateRequest($clientKey) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param Navigation[] $navigations |
||
| 184 | * |
||
| 185 | * @return Refinement[] |
||
| 186 | */ |
||
| 187 | private function generateSelectedRefinements($navigations) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param string $clientKey Your client key. |
||
| 226 | * |
||
| 227 | * @return string JSON representation of request to Bridge. |
||
| 228 | */ |
||
| 229 | public function getBridgeJsonRefinementSearch($clientKey) |
||
| 244 | |||
| 245 | public function __construct() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @return string The current search string. |
||
| 252 | */ |
||
| 253 | public function getQuery() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param string $query The search string. |
||
| 260 | */ |
||
| 261 | public function setQuery($query) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @return string The data sub-collection. |
||
| 268 | */ |
||
| 269 | public function getCollection() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param string $collection The string representation of a collection query. |
||
| 276 | */ |
||
| 277 | public function setCollection($collection) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @return string The area name. |
||
| 284 | */ |
||
| 285 | public function getArea() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param string $area The area name. |
||
| 292 | */ |
||
| 293 | public function setArea($area) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @return string[] A list of metadata fields that will be returned by the search engine. |
||
| 300 | */ |
||
| 301 | public function getFields() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @return string[] A list of the fields that the search service will treat as OR'able. |
||
| 308 | */ |
||
| 309 | public function getOrFields() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param string[] $fields A list of case-sensitive names of the attributes to return. |
||
| 316 | */ |
||
| 317 | public function addFields($fields) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @return string[] A list of which navigations to return from the bridge. |
||
| 324 | */ |
||
| 325 | public function getIncludedNavigations() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param string[] $navigations A list of which navigations to return from the bridge. |
||
| 332 | */ |
||
| 333 | public function addIncludedNavigations($navigations) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @return string[] A list of which navigations to not return from the bridge. |
||
| 340 | */ |
||
| 341 | public function getExcludedNavigations() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @param string[] $navigations A list of which navigations to not return from the bridge. |
||
| 348 | */ |
||
| 349 | public function addExcludedNavigations($navigations) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @return Navigation[] |
||
| 356 | */ |
||
| 357 | public function &getNavigations() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @param Navigation[] $navigations |
||
| 364 | */ |
||
| 365 | public function setNavigations($navigations) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param string $name The case-sensitive name of the attribute to return. |
||
| 372 | */ |
||
| 373 | public function addField($name) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @param string $name Field that should be treated as OR. |
||
| 380 | */ |
||
| 381 | public function addOrField($name) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @param string[] $fields A list of fields that should be treated as OR. |
||
| 388 | */ |
||
| 389 | public function addOrFields($fields) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param string $name The parameter name. |
||
| 396 | * @param string $value The parameter value. |
||
| 397 | */ |
||
| 398 | public function addCustomUrlParamByName($name, $value) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @param CustomUrlParam $param Set an additional parameter that can be used to trigger rules. |
||
| 406 | */ |
||
| 407 | public function addCustomUrlParam($param) |
||
| 411 | |||
| 412 | public function splitRefinements($refinementString) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @param string $refinementString A tilde separated list of refinements. |
||
| 422 | */ |
||
| 423 | public function addRefinementsByString($refinementString) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @param string $navigationName The name of the Navigation. |
||
| 469 | * @param Refinement $refinement A RefinementRange or RefinementValue object. |
||
| 470 | */ |
||
| 471 | public function addRefinement($navigationName, $refinement) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @param string $navigationName The name of the refinement. |
||
| 488 | * @param mixed $low The low value. |
||
| 489 | * @param mixed $high The high value. |
||
| 490 | * @param bool $exclude True if the results should exclude this range refinement, false otherwise. |
||
| 491 | */ |
||
| 492 | public function addRangeRefinement($navigationName, $low, $high, $exclude = false) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @param string $navigationName The name of the refinement. |
||
| 500 | * @param mixed $value The refinement value. |
||
| 501 | * @param bool $exclude True if the results should exclude this value refinement, false otherwise. |
||
| 502 | */ |
||
| 503 | public function addValueRefinement($navigationName, $value, $exclude = false) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * @return bool Are refinements with zero counts being removed. |
||
| 511 | */ |
||
| 512 | public function isPruneRefinements() |
||
| 516 | |||
| 517 | /** |
||
| 518 | * @param bool $pruneRefinements Specifies whether refinements should be pruned. |
||
| 519 | */ |
||
| 520 | public function setPruneRefinements($pruneRefinements) |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @return MSort[] The current list of sort parameters. |
||
| 527 | */ |
||
| 528 | public function &getSort() |
||
| 532 | |||
| 533 | /** |
||
| 534 | * @param MSort[] $sort Any number of sort criteria. |
||
| 535 | */ |
||
| 536 | public function setSort($sort) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @return int The number of documents to skip. |
||
| 543 | */ |
||
| 544 | public function getSkip() |
||
| 548 | |||
| 549 | /** |
||
| 550 | * @param int $skip The number of documents to skip. |
||
| 551 | */ |
||
| 552 | public function setSkip($skip) |
||
| 556 | |||
| 557 | /** |
||
| 558 | * @return CustomUrlParam[] A list of custom url params. |
||
| 559 | */ |
||
| 560 | public function getCustomUrlParams() |
||
| 564 | |||
| 565 | /** |
||
| 566 | * @param CustomUrlParam[] $customUrlParams Set the custom url params. |
||
| 567 | */ |
||
| 568 | public function setCustomUrlParams($customUrlParams) |
||
| 572 | |||
| 573 | // /** |
||
| 574 | // * @return bool Is return JSON set to true. |
||
| 575 | // */ |
||
| 576 | // public function isReturnBinary() |
||
| 577 | // { |
||
| 578 | // return $this->returnBinary; |
||
| 579 | // } |
||
| 580 | // |
||
| 581 | // /** |
||
| 582 | // * @param bool $returnBinary Whether to tell the bridge to return binary data rather than JSON. |
||
| 583 | // */ |
||
| 584 | // public function setReturnBinary($returnBinary) |
||
| 585 | // { |
||
| 586 | // $this->returnBinary = $returnBinary; |
||
| 587 | // } |
||
| 588 | |||
| 589 | /** |
||
| 590 | * @return string The current language restrict value. |
||
| 591 | */ |
||
| 592 | public function getLanguage() |
||
| 596 | |||
| 597 | /** |
||
| 598 | * @param string $language The value for language restrict. |
||
| 599 | */ |
||
| 600 | public function setLanguage($language) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * @return string The current biasing profile name. |
||
| 607 | */ |
||
| 608 | public function getBiasingProfile() |
||
| 612 | |||
| 613 | /** |
||
| 614 | * @param string $biasingProfile Override the biasing profile used for this query. |
||
| 615 | */ |
||
| 616 | public function setBiasingProfile($biasingProfile) |
||
| 620 | |||
| 621 | /** |
||
| 622 | * @return int The current page size. |
||
| 623 | */ |
||
| 624 | public function getPageSize() |
||
| 628 | |||
| 629 | /** |
||
| 630 | * @param int $pageSize The number of records to return with the query. |
||
| 631 | */ |
||
| 632 | public function setPageSize($pageSize) |
||
| 636 | |||
| 637 | /** |
||
| 638 | * @return boolean |
||
| 639 | */ |
||
| 640 | public function isDisableAutocorrection() |
||
| 644 | |||
| 645 | /** |
||
| 646 | * @param boolean $disableAutocorrection Specifies whether the auto-correction behavior should be disabled. |
||
| 647 | * By default, when no results are returned for the given query (and there is |
||
| 648 | * a did-you-mean available), the first did-you-mean is automatically queried |
||
| 649 | * instead. |
||
| 650 | */ |
||
| 651 | public function setDisableAutocorrection($disableAutocorrection) |
||
| 655 | |||
| 656 | /** |
||
| 657 | * @return boolean |
||
| 658 | */ |
||
| 659 | public function isWildcardSearchEnabled() |
||
| 663 | |||
| 664 | /** |
||
| 665 | * @param boolean $wildcardSearchEnabled Indicate if the *(star) character in the search string should be treated |
||
| 666 | * as a wildcard prefix search. For example, `sta*` will match `star` and |
||
| 667 | * `start`. |
||
| 668 | */ |
||
| 669 | public function setWildcardSearchEnabled($wildcardSearchEnabled) |
||
| 673 | |||
| 674 | /** |
||
| 675 | * <b>Warning</b> This will count as two queries against your search index. |
||
| 676 | * |
||
| 677 | * Typically, this feature is used when you have a large number of navigation items that will overwhelm the end |
||
| 678 | * user. It works by using one of the existing navigation items to decide what the query is about and fires a second |
||
| 679 | * query to restrict the navigation to the most relevant set of navigation items for this search term. |
||
| 680 | * |
||
| 681 | * For example, if you pass in a search of `paper` and a restrict navigation of `category:2` |
||
| 682 | * |
||
| 683 | * The bridge will find the category navigation refinements in the first query and fire a second query for the top 2 |
||
| 684 | * most populous categories. Therefore, a search for something generic like "paper" will bring back top category |
||
| 685 | * matches like copy paper (1,030), paper pads (567). The bridge will fire off the second query with the search |
||
| 686 | * term, plus an OR refinement with the most likely categories. The navigation items in the first query are |
||
| 687 | * entirely replaced with the navigation items in the second query, except for the navigation that was used for the |
||
| 688 | * restriction so that users still have the ability to navigate by all category types. |
||
| 689 | * |
||
| 690 | * @param RestrictNavigation $restrictNavigation Restriction criteria |
||
| 691 | */ |
||
| 692 | public function setRestrictNavigation($restrictNavigation) |
||
| 696 | |||
| 697 | /** @return RestrictNavigation */ |
||
| 698 | public function getRestrictNavigation() |
||
| 702 | |||
| 703 | /** |
||
| 704 | * @return string A string representation of all of the currently set refinements. |
||
| 705 | */ |
||
| 706 | View Code Duplication | public function getRefinementString() |
|
| 721 | |||
| 722 | /** |
||
| 723 | * @return string A string representation of all of the currently set custom url parameters. |
||
| 724 | */ |
||
| 725 | View Code Duplication | public function getCustomUrlParamsString() |
|
| 738 | |||
| 739 | /** |
||
| 740 | * @param MSort $sort |
||
| 741 | * |
||
| 742 | * @return RSort |
||
| 743 | */ |
||
| 744 | protected static function convertSort($sort) |
||
| 762 | |||
| 763 | /** |
||
| 764 | * @param MMatchStrategy $strategy |
||
| 765 | * |
||
| 766 | * @return RMatchStrategy |
||
| 767 | */ |
||
| 768 | protected static function convertPartialMatchStrategy($strategy) |
||
| 785 | |||
| 786 | /** |
||
| 787 | * @param MPartialMatchRule $rule |
||
| 788 | * |
||
| 789 | * @return RPartialMatchRule |
||
| 790 | */ |
||
| 791 | protected static function convertPartialMatchRule($rule) |
||
| 804 | |||
| 805 | } |
||
| 806 |