Complex classes like QueryGenerator 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 QueryGenerator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class QueryGenerator { |
||
| 17 | |||
| 18 | /* The term to search for */ |
||
| 19 | private $queryText = ''; |
||
| 20 | |||
| 21 | /* Fields to search for as an array of Name to weighting, otherwise null for all, ie not |
||
| 22 | a multi match query */ |
||
| 23 | private $fields = null; |
||
| 24 | |||
| 25 | /* Aggregations already selected in format array(key => value), e.g. array('ISO' => 400) */ |
||
| 26 | private $selectedFilters = null; |
||
| 27 | |||
| 28 | /* For an empty query, show results or not */ |
||
| 29 | private $showResultsForEmptyQuery = false; |
||
| 30 | |||
| 31 | /* Manipulator to be used for aggregations */ |
||
| 32 | private $manipulator = null; |
||
| 33 | |||
| 34 | /* The length of a page of results */ |
||
| 35 | private $pageLength = 10; |
||
| 36 | |||
| 37 | /* Where to start, normally a multiple of pageLength */ |
||
| 38 | private $start = 0; |
||
| 39 | |||
| 40 | /* Cache hit counter for test purposes */ |
||
| 41 | private static $cacheHitCtr = 0; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Comma separated list of SilverStripe ClassNames to search. Leave blank for all |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $classes = ''; |
||
| 48 | |||
| 49 | |||
| 50 | 1 | public function setQueryText($newQueryText) { |
|
| 53 | |||
| 54 | |||
| 55 | 1 | public function setFields($newFields) { |
|
| 58 | |||
| 59 | |||
| 60 | public function setSelectedFilters($newSelectedFilters) { |
||
| 61 | $this->selectedFilters = $newSelectedFilters; |
||
| 62 | } |
||
| 63 | |||
| 64 | |||
| 65 | 1 | public function setShowResultsForEmptyQuery($newShowResultsForEmptyQuery) { |
|
| 68 | |||
| 69 | |||
| 70 | public function getShowResultsForEmptyQuery() { |
||
| 73 | |||
| 74 | |||
| 75 | 1 | public function setPageLength($newPageLength) { |
|
| 78 | |||
| 79 | |||
| 80 | 1 | public function setStart($newStart) { |
|
| 83 | |||
| 84 | |||
| 85 | /** |
||
| 86 | * Update the list of Classes to search, use SilverStripe ClassName comma separated |
||
| 87 | * @param string $newClasses comma separated list of SilverStripe ClassNames |
||
| 88 | */ |
||
| 89 | 1 | public function setClasses($newClasses) { |
|
| 92 | |||
| 93 | |||
| 94 | |||
| 95 | /** |
||
| 96 | * Set the manipulator, mainly used for aggregation |
||
| 97 | * @param string $newManipulator manipulator used for aggregation, must implement ElasticaSearchHelper |
||
| 98 | */ |
||
| 99 | public function setQueryResultManipulator($newManipulator) { |
||
| 100 | $this->manipulator = $newManipulator; |
||
| 101 | } |
||
| 102 | |||
| 103 | |||
| 104 | /* |
||
| 105 | Accessor to cache hit counter, for testing purposes |
||
| 106 | */ |
||
| 107 | public static function getCacheHitCounter() { |
||
| 110 | |||
| 111 | |||
| 112 | public static function resetCacheHitCounter() { |
||
| 115 | |||
| 116 | |||
| 117 | /** |
||
| 118 | * From the input variables create a suitable query using Elastica. This is somewhat complex |
||
| 119 | * due to different formats with and without query text, with and without filters, with and |
||
| 120 | * without selected filters. Extracting this logic into a separate class makes testing much |
||
| 121 | * faster and can be used for testing new cases |
||
| 122 | * |
||
| 123 | * @return \Elastica\Query Query object suitable for searching using the Elastica library |
||
| 124 | */ |
||
| 125 | public function generateElasticaQuery() { |
||
| 181 | |||
| 182 | |||
| 183 | /** |
||
| 184 | * Generate a query for autocomplete |
||
| 185 | * @return \Elastica\Query Autocompletion query for use with Elastica library |
||
| 186 | */ |
||
| 187 | 1 | public function generateElasticaAutocompleteQuery() { |
|
| 221 | |||
| 222 | |||
| 223 | |||
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * Using a query string object, return a suitable filtered or unfiltered query object |
||
| 228 | * @param Elastica\Query\QueryString $textQuery A query_string representing the current query |
||
| 229 | */ |
||
| 230 | private function addFilters($textQuery) { |
||
| 288 | |||
| 289 | |||
| 290 | private function addAggregation(&$query) { |
||
| 296 | |||
| 297 | |||
| 298 | /* |
||
| 299 | Simplest form of search, namely search for text string against all fields. In Curl terms: |
||
| 300 | |||
| 301 | curl -XGET 'http://localhost:9200/elastica_ss_module_test_en_us/_search?pretty' -d ' |
||
| 302 | { |
||
| 303 | "query": { |
||
| 304 | "query_string": { |
||
| 305 | "query": "Image" |
||
| 306 | } |
||
| 307 | } |
||
| 308 | } |
||
| 309 | ' |
||
| 310 | */ |
||
| 311 | private function simpleTextQuery() { |
||
| 325 | |||
| 326 | |||
| 327 | // USE MATCH_ALL, see https://www.elastic.co/guide/en/elasticsearch/reference/1.4/query-dsl-match-all-query.html |
||
| 328 | private function multiMatchQuery() { |
||
| 357 | |||
| 358 | |||
| 359 | |||
| 360 | /** |
||
| 361 | * Use the configuration from the Search settings held in the database to |
||
| 362 | * form the array of fields suitable for a multimatch query. Call this |
||
| 363 | * after having called setClasses |
||
| 364 | * |
||
| 365 | * @return array Array of fieldsname to weight |
||
| 366 | */ |
||
| 367 | public function convertWeightedFieldsForElastica($fields) { |
||
| 393 | |||
| 394 | |||
| 395 | |||
| 396 | /** |
||
| 397 | * Get a hash of name to Elasticserver mapping, e.g. 'Title' => 'string' |
||
| 398 | * Use SS_Cache to save on database hits, as this data only changes at build time |
||
| 399 | * @param string $classes CSV or array of ClassNames to search, or empty for |
||
| 400 | * all of SiteTree |
||
| 401 | * @return array Array hash of fieldname to Elasticsearch mapping |
||
| 402 | */ |
||
| 403 | public static function getSearchFieldsMappingForClasses($classes = null, $fieldsAllowed = null) { |
||
| 472 | |||
| 473 | |||
| 474 | public static function getCache() { |
||
| 478 | |||
| 479 | |||
| 480 | /** |
||
| 481 | * Convert either a CSV string or an array to a CSV single quoted string, suitable for use in |
||
| 482 | * an SQL IN clause |
||
| 483 | * @param string|array $csvOrArray A string separated by commas or an array |
||
| 484 | * @return string string or array as a CSV, but values quoted with single quotes |
||
| 485 | */ |
||
| 486 | public static function convertToQuotedCSV($csvOrArray) { |
||
| 505 | } |
||
| 506 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: