Complex classes like SearchableHelper 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 SearchableHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
4 | class SearchableHelper { |
||
5 | |||
6 | |||
7 | public static function addIndexedFields($name, &$spec, $ownerClassName) { |
||
8 | // in the case of a relationship type will not be set |
||
9 | if(isset($spec['type'])) { |
||
10 | if($spec['type'] == 'string') { |
||
11 | $unstemmed = array(); |
||
12 | $unstemmed['type'] = "string"; |
||
13 | $unstemmed['analyzer'] = "unstemmed"; |
||
14 | $unstemmed['term_vector'] = "yes"; |
||
15 | $extraFields = array('standard' => $unstemmed); |
||
16 | |||
17 | $shingles = array(); |
||
18 | $shingles['type'] = "string"; |
||
19 | $shingles['analyzer'] = "shingles"; |
||
20 | $shingles['term_vector'] = "yes"; |
||
21 | $extraFields['shingles'] = $shingles; |
||
22 | |||
23 | //Add autocomplete field if so required |
||
24 | $autocomplete = \Config::inst()->get($ownerClassName, 'searchable_autocomplete'); |
||
25 | |||
26 | if(isset($autocomplete) && in_array($name, $autocomplete)) { |
||
27 | $autocompleteField = array(); |
||
28 | $autocompleteField['type'] = "string"; |
||
29 | $autocompleteField['index_analyzer'] = "autocomplete_index_analyzer"; |
||
30 | $autocompleteField['search_analyzer'] = "autocomplete_search_analyzer"; |
||
31 | $autocompleteField['term_vector'] = "yes"; |
||
32 | $extraFields['autocomplete'] = $autocompleteField; |
||
33 | } |
||
34 | |||
35 | $spec['fields'] = $extraFields; |
||
36 | // FIXME - make index/locale specific, get from settings |
||
37 | $spec['analyzer'] = 'stemmed'; |
||
38 | $spec['term_vector'] = "yes"; |
||
39 | } |
||
40 | } |
||
41 | } |
||
42 | |||
43 | |||
44 | /** |
||
45 | * @param string &$name |
||
46 | * @param boolean $storeMethodName |
||
47 | * @param boolean $recurse |
||
48 | */ |
||
49 | public static function assignSpecForRelationship(&$name, $resultType, &$spec, $storeMethodName, $recurse) { |
||
50 | $resultTypeInstance = \Injector::inst()->create($resultType); |
||
51 | $resultTypeMapping = array(); |
||
52 | // get the fields for the result type, but do not recurse |
||
53 | if($recurse) { |
||
54 | $resultTypeMapping = $resultTypeInstance->getElasticaFields($storeMethodName, false); |
||
55 | } |
||
56 | $resultTypeMapping['ID'] = array('type' => 'integer'); |
||
57 | if($storeMethodName) { |
||
58 | $resultTypeMapping['__method'] = $name; |
||
59 | } |
||
60 | $spec = array('properties' => $resultTypeMapping); |
||
61 | // we now change the name to the result type, not the method name |
||
62 | $name = $resultType; |
||
63 | } |
||
64 | |||
65 | |||
66 | /** |
||
67 | * @param string $name |
||
68 | */ |
||
69 | public static function assignSpecForStandardFieldType($name, $class, &$spec, &$html_fields, &$mappings) { |
||
|
|||
70 | if(($pos = strpos($class, '('))) { |
||
71 | // Valid in the case of Varchar(255) |
||
72 | $class = substr($class, 0, $pos); |
||
73 | } |
||
74 | |||
75 | if(array_key_exists($class, $mappings)) { |
||
76 | $spec['type'] = $mappings[$class]; |
||
77 | if($spec['type'] === 'date') { |
||
78 | $spec['format'] = SearchableHelper::getFormatForDate($class); |
||
1 ignored issue
–
show
|
|||
79 | } |
||
80 | |||
81 | if($class === 'HTMLText' || $class === 'HTMLVarchar') { |
||
82 | array_push($html_fields, $name); |
||
83 | } |
||
84 | } |
||
85 | } |
||
86 | |||
87 | |||
88 | public static function getFormatForDate($class) { |
||
89 | $format = 'y-M-d'; // default |
||
90 | switch ($class) { |
||
91 | case 'Date': |
||
92 | $format = 'y-M-d'; |
||
93 | break; |
||
94 | case 'SS_Datetime': |
||
95 | $format = 'y-M-d H:m:s'; |
||
96 | break; |
||
97 | case 'Datetime': |
||
98 | $format = 'y-M-d H:m:s'; |
||
99 | break; |
||
100 | case 'Time': |
||
101 | $format = 'H:m:s'; |
||
102 | break; |
||
103 | } |
||
104 | |||
105 | return $format; |
||
106 | } |
||
107 | |||
108 | |||
109 | |||
110 | public static function getListRelationshipMethods($instance) { |
||
111 | $has_manys = $instance->has_many(); |
||
112 | $many_manys = $instance->many_many(); |
||
113 | |||
114 | // array of method name to retuned object ClassName for relationships returning lists |
||
115 | $has_lists = $has_manys; |
||
116 | foreach(array_keys($many_manys) as $key) { |
||
117 | $has_lists[$key] = $many_manys[$key]; |
||
118 | } |
||
119 | |||
120 | return $has_lists; |
||
121 | } |
||
122 | |||
123 | |||
124 | public static function isInSiteTree($classname) { |
||
125 | $inSiteTree = ($classname === 'SiteTree' ? true : false); |
||
126 | if(!$inSiteTree) { |
||
127 | $class = new \ReflectionClass($classname); |
||
128 | while($class = $class->getParentClass()) { |
||
129 | $parentClass = $class->getName(); |
||
130 | if($parentClass == 'SiteTree') { |
||
131 | $inSiteTree = true; |
||
132 | break; |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 | return $inSiteTree; |
||
137 | } |
||
138 | |||
139 | |||
140 | public static function storeMethodTextValue($instance, $field, &$fields, $html_fields) { |
||
152 | |||
153 | |||
154 | public static function storeFieldHTMLValue($instance, $field, &$fields) { |
||
162 | |||
163 | |||
164 | public static function storeRelationshipValue($instance, $field, &$fields, $config, $recurse) { |
||
194 | |||
195 | |||
196 | public static function findOrCreateSearchableClass($classname) { |
||
207 | |||
208 | |||
209 | public static function findOrCreateSearchableField($className, $fieldName, $searchableField, $searchableClass) { |
||
235 | |||
236 | |||
237 | /* |
||
238 | Evaluate each field, e.g. 'Title', 'Member.Name' |
||
239 | */ |
||
240 | public static function fieldsToElasticaConfig($fields) { |
||
241 | // Copied from DataObject::searchableFields() as there is no separate accessible method |
||
257 | |||
258 | |||
259 | |||
260 | } |
||
261 |
This check marks parameter names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.