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