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 |
||
47 | class Query |
||
|
|||
48 | { |
||
49 | /** @var string */ |
||
50 | private $userId; |
||
51 | /** @var string */ |
||
52 | private $query; |
||
53 | /** @var int */ |
||
54 | private $skip = 0; |
||
55 | /** @var int */ |
||
56 | private $pageSize = 10; |
||
57 | /** @var string */ |
||
58 | private $collection; |
||
59 | /** @var string */ |
||
60 | private $area; |
||
61 | /** @var string */ |
||
62 | private $biasingProfile; |
||
63 | /** @var string */ |
||
64 | private $language; |
||
65 | /** @var MSort[] */ |
||
66 | private $sort; |
||
67 | /** @var CustomUrlParam[] */ |
||
68 | private $customUrlParams = array(); |
||
69 | /** @var Navigation[] */ |
||
70 | private $navigations = array(); |
||
71 | /** @var string[] */ |
||
72 | private $includedNavigations = array(); |
||
73 | /** @var string[] */ |
||
74 | private $excludedNavigations = array(); |
||
75 | /** @var string[] */ |
||
76 | private $fields = array(); |
||
77 | /** @var string[] */ |
||
78 | private $orFields = array(); |
||
79 | /** @var bool */ |
||
80 | private $pruneRefinements = true; |
||
81 | /** @var bool */ |
||
82 | private $disableAutocorrection = false; |
||
83 | /** @var bool */ |
||
84 | private $wildcardSearchEnabled = false; |
||
85 | // Removed until CBOR support for serialization / de-serialization improves |
||
86 | // /** @var bool */ |
||
87 | // private $returnBinary = false; |
||
88 | /** @var RestrictNavigation */ |
||
89 | private $restrictNavigation; |
||
90 | /** @var MBiasing */ |
||
91 | private $biasing; |
||
92 | /** @var MMatchStrategy */ |
||
93 | private $matchStrategy; |
||
94 | |||
95 | /** @var Serializer */ |
||
96 | private $serializer; |
||
97 | |||
98 | const TILDE_REGEX = "/~((?=[\\w]*[=:]))/"; |
||
99 | |||
100 | /** |
||
101 | * @param mixed $request |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | private function requestToJson($request) |
||
116 | |||
117 | /** |
||
118 | * @param string $clientKey Your client key. |
||
119 | * |
||
120 | * @return string JSON representation of request to Bridge. |
||
121 | */ |
||
122 | public function getBridgeJson($clientKey) |
||
127 | |||
128 | /** |
||
129 | * @param string $clientKey Your client key. |
||
130 | * @param string $navigationName Name of the navigation to get refinements for |
||
131 | * |
||
132 | * @return string JSON representation of request to Bridge. |
||
133 | */ |
||
134 | public function getBridgeRefinementsJson($clientKey, $navigationName) |
||
141 | |||
142 | /** |
||
143 | * @param string $clientKey |
||
144 | * |
||
145 | * @return Request |
||
146 | */ |
||
147 | private function populateRequest($clientKey) |
||
209 | |||
210 | /** |
||
211 | * @param Navigation[] $navigations |
||
212 | * |
||
213 | * @return Refinement[] |
||
214 | */ |
||
215 | private function generateSelectedRefinements($navigations) |
||
251 | |||
252 | /** |
||
253 | * @param string $clientKey Your client key. |
||
254 | * |
||
255 | * @return string JSON representation of request to Bridge. |
||
256 | */ |
||
257 | public function getBridgeJsonRefinementSearch($clientKey) |
||
272 | |||
273 | public function __construct() |
||
277 | |||
278 | /** |
||
279 | * @return string The current search string. |
||
280 | */ |
||
281 | public function getQuery() |
||
285 | |||
286 | /** |
||
287 | * @param string $query The search string. |
||
288 | */ |
||
289 | public function setQuery($query) |
||
293 | |||
294 | /** |
||
295 | * @return string The data sub-collection. |
||
296 | */ |
||
297 | public function getCollection() |
||
301 | |||
302 | /** |
||
303 | * @param string $collection The string representation of a collection query. |
||
304 | */ |
||
305 | public function setCollection($collection) |
||
309 | |||
310 | /** |
||
311 | * @return string The area name. |
||
312 | */ |
||
313 | public function getArea() |
||
317 | |||
318 | /** |
||
319 | * @param string $area The area name. |
||
320 | */ |
||
321 | public function setArea($area) |
||
325 | |||
326 | /** |
||
327 | * @return string[] A list of metadata fields that will be returned by the search engine. |
||
328 | */ |
||
329 | public function getFields() |
||
333 | |||
334 | /** |
||
335 | * @return string[] A list of the fields that the search service will treat as OR'able. |
||
336 | */ |
||
337 | public function getOrFields() |
||
341 | |||
342 | /** |
||
343 | * @param string[] $fields A list of case-sensitive names of the attributes to return. |
||
344 | */ |
||
345 | public function addFields($fields) |
||
349 | |||
350 | /** |
||
351 | * @return string[] A list of which navigations to return from the bridge. |
||
352 | */ |
||
353 | public function getIncludedNavigations() |
||
357 | |||
358 | /** |
||
359 | * @param string[] $navigations A list of which navigations to return from the bridge. |
||
360 | */ |
||
361 | public function addIncludedNavigations($navigations) |
||
365 | |||
366 | /** |
||
367 | * @return string[] A list of which navigations to not return from the bridge. |
||
368 | */ |
||
369 | public function getExcludedNavigations() |
||
373 | |||
374 | /** |
||
375 | * @param string[] $navigations A list of which navigations to not return from the bridge. |
||
376 | */ |
||
377 | public function addExcludedNavigations($navigations) |
||
381 | |||
382 | /** |
||
383 | * @return Navigation[] |
||
384 | */ |
||
385 | public function &getNavigations() |
||
389 | |||
390 | /** |
||
391 | * @param Navigation[] $navigations |
||
392 | */ |
||
393 | public function setNavigations($navigations) |
||
397 | |||
398 | /** |
||
399 | * @param string $name The case-sensitive name of the attribute to return. |
||
400 | */ |
||
401 | public function addField($name) |
||
405 | |||
406 | /** |
||
407 | * @param string $name Field that should be treated as OR. |
||
408 | */ |
||
409 | public function addOrField($name) |
||
413 | |||
414 | /** |
||
415 | * @param string[] $fields A list of fields that should be treated as OR. |
||
416 | */ |
||
417 | public function addOrFields($fields) |
||
421 | |||
422 | /** |
||
423 | * @param string $name The parameter name. |
||
424 | * @param string $value The parameter value. |
||
425 | */ |
||
426 | public function addCustomUrlParamByName($name, $value) |
||
431 | |||
432 | /** |
||
433 | * @param CustomUrlParam $param Set an additional parameter that can be used to trigger rules. |
||
434 | */ |
||
435 | public function addCustomUrlParam($param) |
||
439 | |||
440 | public function splitRefinements($refinementString) |
||
447 | |||
448 | /** |
||
449 | * @param string $refinementString A tilde separated list of refinements. |
||
450 | */ |
||
451 | public function addRefinementsByString($refinementString) |
||
494 | |||
495 | /** |
||
496 | * @param string $navigationName The name of the Navigation. |
||
497 | * @param Refinement $refinement A RefinementRange or RefinementValue object. |
||
498 | */ |
||
499 | public function addRefinement($navigationName, $refinement) |
||
513 | |||
514 | /** |
||
515 | * @param string $navigationName The name of the refinement. |
||
516 | * @param mixed $low The low value. |
||
517 | * @param mixed $high The high value. |
||
518 | * @param bool $exclude True if the results should exclude this range refinement, false otherwise. |
||
519 | */ |
||
520 | public function addRangeRefinement($navigationName, $low, $high, $exclude = false) |
||
525 | |||
526 | /** |
||
527 | * @param string $navigationName The name of the refinement. |
||
528 | * @param mixed $value The refinement value. |
||
529 | * @param bool $exclude True if the results should exclude this value refinement, false otherwise. |
||
530 | */ |
||
531 | public function addValueRefinement($navigationName, $value, $exclude = false) |
||
536 | |||
537 | /** |
||
538 | * @return bool Are refinements with zero counts being removed. |
||
539 | */ |
||
540 | public function isPruneRefinements() |
||
544 | |||
545 | /** |
||
546 | * @param bool $pruneRefinements Specifies whether refinements should be pruned. |
||
547 | */ |
||
548 | public function setPruneRefinements($pruneRefinements) |
||
552 | |||
553 | /** |
||
554 | * @return MSort[] The current list of sort parameters. |
||
555 | */ |
||
556 | public function &getSort() |
||
560 | |||
561 | /** |
||
562 | * @param MSort[] $sort Any number of sort criteria. |
||
563 | */ |
||
564 | public function setSort($sort) |
||
568 | |||
569 | /** |
||
570 | * @return int The number of documents to skip. |
||
571 | */ |
||
572 | public function getSkip() |
||
576 | |||
577 | /** |
||
578 | * @param int $skip The number of documents to skip. |
||
579 | */ |
||
580 | public function setSkip($skip) |
||
584 | |||
585 | /** |
||
586 | * @return CustomUrlParam[] A list of custom url params. |
||
587 | */ |
||
588 | public function getCustomUrlParams() |
||
592 | |||
593 | /** |
||
594 | * @param CustomUrlParam[] $customUrlParams Set the custom url params. |
||
595 | */ |
||
596 | public function setCustomUrlParams($customUrlParams) |
||
600 | |||
601 | // /** |
||
602 | // * @return bool Is return JSON set to true. |
||
603 | // */ |
||
604 | // public function isReturnBinary() |
||
605 | // { |
||
606 | // return $this->returnBinary; |
||
607 | // } |
||
608 | // |
||
609 | // /** |
||
610 | // * @param bool $returnBinary Whether to tell the bridge to return binary data rather than JSON. |
||
611 | // */ |
||
612 | // public function setReturnBinary($returnBinary) |
||
613 | // { |
||
614 | // $this->returnBinary = $returnBinary; |
||
615 | // } |
||
616 | |||
617 | /** |
||
618 | * @return string The current language restrict value. |
||
619 | */ |
||
620 | public function getLanguage() |
||
624 | |||
625 | /** |
||
626 | * @param string $language The value for language restrict. |
||
627 | */ |
||
628 | public function setLanguage($language) |
||
632 | |||
633 | /** |
||
634 | * @return string The current biasing profile name. |
||
635 | */ |
||
636 | public function getBiasingProfile() |
||
640 | |||
641 | /** |
||
642 | * @param string $biasingProfile Override the biasing profile used for this query. |
||
643 | */ |
||
644 | public function setBiasingProfile($biasingProfile) |
||
648 | |||
649 | /** |
||
650 | * @return int The current page size. |
||
651 | */ |
||
652 | public function getPageSize() |
||
656 | |||
657 | /** |
||
658 | * @param int $pageSize The number of records to return with the query. |
||
659 | */ |
||
660 | public function setPageSize($pageSize) |
||
664 | |||
665 | /** |
||
666 | * @return boolean |
||
667 | */ |
||
668 | public function isDisableAutocorrection() |
||
672 | |||
673 | /** |
||
674 | * @param boolean $disableAutocorrection Specifies whether the auto-correction behavior should be disabled. |
||
675 | * By default, when no results are returned for the given query (and there is |
||
676 | * a did-you-mean available), the first did-you-mean is automatically queried |
||
677 | * instead. |
||
678 | */ |
||
679 | public function setDisableAutocorrection($disableAutocorrection) |
||
683 | |||
684 | /** |
||
685 | * @return boolean |
||
686 | */ |
||
687 | public function isWildcardSearchEnabled() |
||
691 | |||
692 | /** |
||
693 | * @param boolean $wildcardSearchEnabled Indicate if the *(star) character in the search string should be treated |
||
694 | * as a wildcard prefix search. For example, `sta*` will match `star` and |
||
695 | * `start`. |
||
696 | */ |
||
697 | public function setWildcardSearchEnabled($wildcardSearchEnabled) |
||
701 | |||
702 | /** |
||
703 | * <b>Warning</b> This will count as two queries against your search index. |
||
704 | * |
||
705 | * Typically, this feature is used when you have a large number of navigation items that will overwhelm the end |
||
706 | * user. It works by using one of the existing navigation items to decide what the query is about and fires a second |
||
707 | * query to restrict the navigation to the most relevant set of navigation items for this search term. |
||
708 | * |
||
709 | * For example, if you pass in a search of `paper` and a restrict navigation of `category:2` |
||
710 | * |
||
711 | * The bridge will find the category navigation refinements in the first query and fire a second query for the top 2 |
||
712 | * most populous categories. Therefore, a search for something generic like "paper" will bring back top category |
||
713 | * matches like copy paper (1,030), paper pads (567). The bridge will fire off the second query with the search |
||
714 | * term, plus an OR refinement with the most likely categories. The navigation items in the first query are |
||
715 | * entirely replaced with the navigation items in the second query, except for the navigation that was used for the |
||
716 | * restriction so that users still have the ability to navigate by all category types. |
||
717 | * |
||
718 | * @param RestrictNavigation $restrictNavigation Restriction criteria |
||
719 | */ |
||
720 | public function setRestrictNavigation($restrictNavigation) |
||
724 | |||
725 | /** @return RestrictNavigation */ |
||
726 | public function getRestrictNavigation() |
||
730 | |||
731 | /** |
||
732 | * @return MBiasing |
||
733 | */ |
||
734 | public function getBiasing() |
||
738 | |||
739 | /** |
||
740 | * Add a biasing profile, which is defined at query time. |
||
741 | * |
||
742 | * @param MBiasing $biasing |
||
743 | */ |
||
744 | public function setBiasing($biasing) |
||
748 | |||
749 | /** |
||
750 | * @param string[] $bringToTop |
||
751 | */ |
||
752 | public function setBringToTop($bringToTop) |
||
759 | |||
760 | /** |
||
761 | * @param boolean $augment |
||
762 | */ |
||
763 | public function setBiasingAugment($augment) |
||
770 | |||
771 | /** |
||
772 | * @param float $influence |
||
773 | */ |
||
774 | public function setInfluence($influence) |
||
781 | |||
782 | /** |
||
783 | * @return string A string representation of all of the currently set refinements. |
||
784 | */ |
||
785 | View Code Duplication | public function getRefinementString() |
|
800 | |||
801 | /** |
||
802 | * @return string A string representation of all of the currently set custom url parameters. |
||
803 | */ |
||
804 | View Code Duplication | public function getCustomUrlParamsString() |
|
817 | |||
818 | /** |
||
819 | * @return MMatchStrategy |
||
820 | */ |
||
821 | public function getMatchStrategy() { |
||
824 | |||
825 | /** |
||
826 | * @param MMatchStrategy $matchStrategy |
||
827 | */ |
||
828 | public function setMatchStrategy($matchStrategy) { |
||
831 | |||
832 | /** |
||
833 | * @param MSort $sort |
||
834 | * |
||
835 | * @return RSort |
||
836 | */ |
||
837 | protected static function convertSort($sort) |
||
855 | |||
856 | /** |
||
857 | * @param MMatchStrategy $strategy |
||
858 | * |
||
859 | * @return RMatchStrategy |
||
860 | */ |
||
861 | protected static function convertPartialMatchStrategy($strategy) |
||
878 | |||
879 | /** |
||
880 | * @param MPartialMatchRule $rule |
||
881 | * |
||
882 | * @return RPartialMatchRule |
||
883 | */ |
||
884 | protected static function convertPartialMatchRule($rule) |
||
897 | |||
898 | /** |
||
899 | * @param MBias $bias |
||
900 | * |
||
901 | * @return Bias |
||
902 | */ |
||
903 | protected static function convertBias($bias) |
||
907 | |||
908 | /** |
||
909 | * @param MBias[] $biases |
||
910 | * |
||
911 | * @return Bias[] |
||
912 | */ |
||
913 | protected static function convertBiases($biases) |
||
917 | |||
918 | /** |
||
919 | * @param MNumericBoost $boost |
||
920 | * |
||
921 | * @return RNumericBoost |
||
922 | */ |
||
923 | protected static function convertNumericBoost($boost) { |
||
926 | |||
927 | /** |
||
928 | * @param MNumericBoost[] $boosts |
||
929 | * |
||
930 | * @return RNumericBoost[] |
||
931 | */ |
||
932 | protected static function convertNumericBoosts($boosts) { |
||
935 | |||
936 | /** |
||
937 | * @param MBiasing $biasing |
||
938 | * |
||
939 | * @return Biasing |
||
940 | */ |
||
941 | protected static function convertBiasing($biasing) |
||
970 | |||
971 | /** |
||
972 | * @return string |
||
973 | */ |
||
974 | public function getUserId() |
||
978 | |||
979 | /** |
||
980 | * @param string $userId |
||
981 | * |
||
982 | * @return Query |
||
983 | */ |
||
984 | public function setUserId($userId) |
||
989 | |||
990 | } |
||
991 |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.