Complex classes like GoogleMapLocationsObject 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 GoogleMapLocationsObject, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class GoogleMapLocationsObject extends DataObject |
||
| 15 | { |
||
| 16 | private static $parent_point_counts = array(); |
||
| 17 | |||
| 18 | /** |
||
| 19 | * e.g. Page / home Page / Product Page / My Page |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | private static $singular_name = 'Location'; |
||
|
|
|||
| 23 | public function i18n_singular_name() |
||
| 27 | |||
| 28 | |||
| 29 | /** |
||
| 30 | * e.g. Pages / home Pages / Product Pages / My Pages |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | private static $plural_name = 'Locations'; |
||
| 34 | public function i18n_plural_name() |
||
| 38 | |||
| 39 | private static $db = array( |
||
| 40 | 'PointType' =>'Enum("none, point, polyline, polygon", "point")', |
||
| 41 | 'Accuracy' => 'Varchar(100)', |
||
| 42 | 'Longitude' => 'Double(12,7)', |
||
| 43 | 'Latitude' => 'Double(12,7)', |
||
| 44 | 'PointString' => 'Text', |
||
| 45 | 'Address' => 'Text', |
||
| 46 | 'FullAddress' => 'Text', |
||
| 47 | 'CountryNameCode' => 'Varchar(3)', |
||
| 48 | 'AdministrativeAreaName' => 'Varchar(255)', |
||
| 49 | 'SubAdministrativeAreaName' => 'Varchar(255)', |
||
| 50 | 'LocalityName' => 'Varchar(255)', |
||
| 51 | 'PostalCodeNumber' => 'Varchar(30)', |
||
| 52 | 'Manual' => 'Boolean', |
||
| 53 | 'CustomPopUpWindowTitle' => "Varchar(50)", |
||
| 54 | 'CustomPopUpWindowInfo' => "HTMLText(255)" |
||
| 55 | //'GeoPointField' => 'GeoPoint', |
||
| 56 | //'GeoPolygonField' => 'GeoPolygon', |
||
| 57 | //'GeoLineString' => 'GeoLineString' |
||
| 58 | ); |
||
| 59 | |||
| 60 | private static $summary_fields = array( |
||
| 61 | 'FullAddress' => "FullAddress", |
||
| 62 | ); |
||
| 63 | |||
| 64 | private static $has_one = array( |
||
| 65 | 'Parent' => 'SiteTree' |
||
| 66 | ); |
||
| 67 | |||
| 68 | private static $indexes = array( |
||
| 69 | "Latitude" => true, |
||
| 70 | "Longitude" => true |
||
| 71 | ); |
||
| 72 | |||
| 73 | private static $field_labels = array( |
||
| 74 | 'PointType' =>'Marker Type', |
||
| 75 | 'Accuracy' => 'Accuracy', |
||
| 76 | 'Longitude' => 'Longitude', |
||
| 77 | 'Latitude' => 'Latitude', |
||
| 78 | 'PointString' => 'PointString', |
||
| 79 | 'Address' => 'Searched For Address', |
||
| 80 | 'FullAddress' => 'Found Address', |
||
| 81 | 'CountryNameCode' => 'Country Code', |
||
| 82 | 'AdministrativeAreaName' => 'Main Area', |
||
| 83 | 'SubAdministrativeAreaName' => 'Sub Area', |
||
| 84 | 'LocalityName' => 'Locality', |
||
| 85 | 'PostalCodeNumber' => 'Postal Code', |
||
| 86 | 'Manual' => 'Set Details Manually', |
||
| 87 | 'CustomPopUpWindowTitle' => "Marker Title", |
||
| 88 | 'CustomPopUpWindowInfo' => "Marker Description" |
||
| 89 | ); |
||
| 90 | |||
| 91 | private static $casting = array( |
||
| 92 | "ParentData" => "SiteTree", |
||
| 93 | "AjaxInfoWindowLink" => "HTMLText", |
||
| 94 | "ParentClassName" => "Varchar", |
||
| 95 | "Link" => "Varchar" |
||
| 96 | ); |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Provides MySQL snippet to work out distance between GoogleMapLocationsObject and location |
||
| 100 | * The method returns a string for use in queries |
||
| 101 | * The query snippet returns the distance between the GoogleMapLocationsObject and the latitude and longitude provided |
||
| 102 | * NOTE: 6378.137 is the radius of the earth in kilometers |
||
| 103 | * @param Double $lng - longitude of location |
||
| 104 | * @param Double $lat - latitude of location |
||
| 105 | * |
||
| 106 | * @return String |
||
| 107 | */ |
||
| 108 | public static function radius_definition($lng, $lat) |
||
| 112 | |||
| 113 | public static function radius_definition_other_table($lng, $lat, $table, $latitudeField, $longitudeField) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param Int $lng |
||
| 124 | * @param Int $lat |
||
| 125 | * |
||
| 126 | * return GoogleMapLocationsObject | Null |
||
| 127 | */ |
||
| 128 | public static function point_exists($lng, $lat) |
||
| 138 | |||
| 139 | public function CMSEditLink() |
||
| 145 | |||
| 146 | public function getCMSFields() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @casted variable |
||
| 210 | * @return SiteTree |
||
| 211 | */ |
||
| 212 | public function getParentData() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @casted variable |
||
| 219 | * @return String (HTML) |
||
| 220 | */ |
||
| 221 | public function getAjaxInfoWindowLink() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @casted variable |
||
| 236 | * @return String | Null |
||
| 237 | */ |
||
| 238 | public function getParentClassName() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @casted variable |
||
| 247 | * @return String | Null |
||
| 248 | */ |
||
| 249 | public function getLink() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * add data from Parent to the object |
||
| 258 | */ |
||
| 259 | public function addParentData() |
||
| 278 | |||
| 279 | public function onBeforeWrite() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * complete points data |
||
| 295 | * |
||
| 296 | */ |
||
| 297 | protected function completePoints() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * test to see if address is found. If address if found then |
||
| 326 | * it will write the object, otherwise it returns null. |
||
| 327 | * @params array $array (optional) |
||
| 328 | * @return this|null |
||
| 329 | */ |
||
| 330 | public function findGooglePointsAndWriteIfFound($params = []) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param array $params params for the Google Server |
||
| 341 | * @param bool $doNotWrite - do not write to Database |
||
| 342 | */ |
||
| 343 | protected function findGooglePoints($doNotWrite, $params = []) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * |
||
| 357 | * @param Array $newData |
||
| 358 | * @param Boolean $doNotWrite - do not write object to database |
||
| 359 | */ |
||
| 360 | protected function addDataFromArray($newData, $doNotWrite = false) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * provides a links to Google Maps to search for directions |
||
| 373 | * @return String |
||
| 374 | */ |
||
| 375 | public function DirectionsLink() |
||
| 379 | } |
||
| 380 |