Complex classes like ShopSearch 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 ShopSearch, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class ShopSearch extends Object |
||
|
|||
10 | { |
||
11 | const FACET_TYPE_LINK = 'link'; |
||
12 | const FACET_TYPE_CHECKBOX = 'checkbox'; |
||
13 | const FACET_TYPE_RANGE = 'range'; |
||
14 | |||
15 | /** @var string - class name of adapter class to use */ |
||
16 | private static $adapter_class = 'ShopSearchSimple'; |
||
17 | |||
18 | /** @var array - these classes will be added to the index - e.g. Category, Page, etc. */ |
||
19 | private static $searchable = array(); |
||
20 | |||
21 | /** @var bool - if true, all buyable models will be added to the index automatically */ |
||
22 | private static $buyables_are_searchable = true; |
||
23 | |||
24 | /** @var int - size of paging in the search */ |
||
25 | private static $page_size = 10; |
||
26 | |||
27 | /** @var bool */ |
||
28 | private static $suggest_enabled = true; |
||
29 | |||
30 | /** @var int - how many suggestions to provide */ |
||
31 | private static $suggest_limit = 5; |
||
32 | |||
33 | /** @var bool */ |
||
34 | private static $search_as_you_type_enabled = true; |
||
35 | |||
36 | /** @var int - how may sayt (search-as-you-type) entries to provide */ |
||
37 | private static $sayt_limit = 5; |
||
38 | |||
39 | /** @var bool - automatically create facets for static attributes */ |
||
40 | private static $auto_facet_attributes = false; |
||
41 | |||
42 | /** @var string - optionally, a different template to run ajax results through (sans-Page.ss) */ |
||
43 | private static $ajax_results_template = ''; |
||
44 | |||
45 | /** @var string - these allow you to use different querystring params in you need to */ |
||
46 | private static $qs_query = 'q'; |
||
47 | private static $qs_filters = 'f'; |
||
48 | private static $qs_parent_search = '__ps'; |
||
49 | private static $qs_title = '__t'; |
||
50 | private static $qs_source = '__src'; // used to log searches from search-as-you-type |
||
51 | private static $qs_sort = 'sort'; |
||
52 | |||
53 | /** @var array - I'm leaving this particularly bare b/c with config merging it's a pain to remove items */ |
||
54 | private static $sort_options = array( |
||
55 | 'score desc' => 'Relevance', |
||
56 | // 'SiteTree_Title asc' => 'Alphabetical (A-Z)', |
||
57 | // 'SiteTree_Title dsc' => 'Alphabetical (Z-A)', |
||
58 | ); |
||
59 | |||
60 | /** |
||
61 | * @var array - default search facets (price, category, etc) |
||
62 | * Key field name - e.g. Price - can be a VirtualFieldIndex field |
||
63 | * Value facet label - e.g. Search By Category - if the value is a relation or returns an array or |
||
64 | * list all values will be faceted individually |
||
65 | * NOTE: this can also be another array with keys: Label, Type, and Values (for checkbox only) |
||
66 | */ |
||
67 | private static $facets = array(); |
||
68 | |||
69 | /** @var array - field definition for Solr only */ |
||
70 | private static $solr_fulltext_fields = array(); |
||
71 | |||
72 | /** @var array - field definition for Solr only */ |
||
73 | private static $solr_filter_fields = array(); |
||
74 | |||
75 | /** @var string - if present, will create a copy of SiteTree_Title that's suited for alpha sorting */ |
||
76 | private static $solr_title_sort_field = ''; |
||
77 | |||
78 | /** |
||
79 | * @var string - If present, everything matching the following regex will be removed from |
||
80 | * keyword search queries before passing to the search adapter. |
||
81 | */ |
||
82 | private static $keyword_filter_regex = '/[^a-zA-Z0-9\s\-]/'; |
||
83 | |||
84 | |||
85 | /** |
||
86 | * @return array |
||
87 | */ |
||
88 | public static function get_searchable_classes() |
||
110 | |||
111 | /** |
||
112 | * Returns an array of categories suitable for a dropdown menu |
||
113 | * TODO: cache this |
||
114 | * |
||
115 | * @param int $parentID [optional] |
||
116 | * @param string $prefix [optional] |
||
117 | * @param int $maxDepth [optional] |
||
118 | * @return array |
||
119 | * @static |
||
120 | */ |
||
121 | public static function get_category_hierarchy($parentID = 0, $prefix = '', $maxDepth = 999) |
||
146 | |||
147 | /** |
||
148 | * @return ShopSearchAdapter |
||
149 | */ |
||
150 | public static function adapter() |
||
155 | |||
156 | /** |
||
157 | * @return ShopSearch |
||
158 | */ |
||
159 | public static function inst() |
||
163 | |||
164 | /** |
||
165 | * The result will contain at least the following: |
||
166 | * Matches - SS_List of results |
||
167 | * TotalMatches - total # of results, unlimited |
||
168 | * Query - query string |
||
169 | * Also saves a log record. |
||
170 | * |
||
171 | * @param array $vars |
||
172 | * @param bool $logSearch [optional] |
||
173 | * @param bool $useFacets [optional] |
||
174 | * @param int $start [optional] |
||
175 | * @param int $limit [optional] |
||
176 | * @return ArrayData |
||
177 | */ |
||
178 | public function search(array $vars, $logSearch=true, $useFacets=true, $start=-1, $limit=-1) |
||
267 | |||
268 | /** |
||
269 | * @param string $str |
||
270 | * @return SS_Query |
||
271 | */ |
||
272 | public function getSuggestQuery($str='') |
||
298 | |||
299 | |||
300 | /** |
||
301 | * @param string $str |
||
302 | * @return array |
||
303 | */ |
||
304 | public function suggest($str='') |
||
313 | |||
314 | |||
315 | /** |
||
316 | * Returns an array that can be made into json and passed to the controller |
||
317 | * containing both term suggestions and a few product matches. |
||
318 | * |
||
319 | * @param array $searchVars |
||
320 | * @return array |
||
321 | */ |
||
322 | public function suggestWithResults(array $searchVars) |
||
383 | } |
||
384 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.