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 | 1 | public static function addIndexedFields($name, &$spec, $ownerClassName) { |
|
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * @param string &$name |
||
| 46 | * @param boolean $storeMethodName |
||
| 47 | * @param boolean $recurse |
||
| 48 | */ |
||
| 49 | 1 | public static function assignSpecForRelationship(&$name, $resultType, &$spec, $storeMethodName, $recurse) { |
|
| 64 | |||
| 65 | |||
| 66 | /** |
||
| 67 | * @param string $name |
||
| 68 | */ |
||
| 69 | 1 | public static function assignSpecForStandardFieldType($name, $class, &$spec, &$html_fields, &$mappings) { |
|
| 86 | |||
| 87 | |||
| 88 | 1 | public static function getFormatForDate($class) { |
|
| 107 | |||
| 108 | |||
| 109 | |||
| 110 | 1 | public static function getListRelationshipMethods($instance) { |
|
| 122 | |||
| 123 | |||
| 124 | 1 | public static function isInSiteTree($classname) { |
|
| 138 | |||
| 139 | |||
| 140 | public static function storeMethodTextValue($instance, $field, &$fields, $html_fields) { |
||
| 141 | if(in_array($field, $html_fields)) { |
||
| 142 | // Parse short codes in HTML, and then convert to text |
||
| 143 | $fields[$field] = $instance->$field; |
||
| 144 | $html = \ShortcodeParser::get_active()->parse($instance->$field()); |
||
| 145 | $txt = \Convert::html2raw($html); |
||
| 146 | $fields[$field] = $txt; |
||
| 147 | } else { |
||
| 148 | // Plain text |
||
| 149 | $fields[$field] = $instance->$field(); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | |||
| 154 | 1 | public static function storeFieldHTMLValue($instance, $field, &$fields) { |
|
| 162 | |||
| 163 | |||
| 164 | 1 | public static function storeRelationshipValue($instance, $field, &$fields, $config, $recurse) { |
|
| 165 | 1 | if(isset($config['properties']['__method'])) { |
|
| 166 | $methodName = $config['properties']['__method']; |
||
| 167 | $data = $instance->$methodName(); |
||
| 168 | $relArray = array(); |
||
| 169 | |||
| 170 | $has_ones = $instance->has_one(); |
||
| 171 | // get the fields of a has_one relational object |
||
| 172 | if(isset($has_ones[$methodName])) { |
||
| 173 | if($data->ID > 0) { |
||
| 174 | $item = $data->getFieldValuesAsArray(false); |
||
| 175 | $relArray = $item; |
||
| 176 | } |
||
| 177 | |||
| 178 | // get the fields for a has_many or many_many relational list |
||
| 179 | } else { |
||
| 180 | foreach($data->getIterator() as $item) { |
||
| 181 | if($recurse) { |
||
| 182 | // populate the subitem but do not recurse any further if more relationships |
||
| 183 | $itemDoc = $item->getFieldValuesAsArray(false); |
||
| 184 | array_push($relArray, $itemDoc); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | } |
||
| 188 | // save the relation as an array (for now) |
||
| 189 | $fields[$methodName] = $relArray; |
||
| 190 | } else { |
||
| 191 | 1 | $fields[$field] = $instance->$field; |
|
| 192 | } |
||
| 193 | 1 | } |
|
| 194 | |||
| 195 | |||
| 196 | 1 | public static function findOrCreateSearchableClass($classname) { |
|
| 207 | |||
| 208 | |||
| 209 | 1 | public static function findOrCreateSearchableField($className, $fieldName, $searchableField, $searchableClass) { |
|
| 235 | |||
| 236 | |||
| 237 | /* |
||
| 238 | Evaluate each field, e.g. 'Title', 'Member.Name' |
||
| 239 | */ |
||
| 240 | 1 | public static function fieldsToElasticaConfig($fields) { |
|
| 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.