Complex classes like GetLatLngFromGoogleUsingAddress 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 GetLatLngFromGoogleUsingAddress, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class GetLatLngFromGoogleUsingAddress extends Object |
||
| 22 | { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * For debugging. |
||
| 26 | * needs to be static because we use static methods |
||
| 27 | * in this class |
||
| 28 | * @var Boolean for debugging |
||
| 29 | */ |
||
| 30 | private static $debug = false; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * location for API |
||
| 34 | * |
||
| 35 | * @var String |
||
| 36 | */ |
||
| 37 | private static $geocode_url = "https://maps.googleapis.com/maps/api/geocode/json?address=%s"; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Additional data to send to the Google Server |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | private static $additional_params = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * default user to first result that is returned. |
||
| 48 | * |
||
| 49 | * @var boolean |
||
| 50 | */ |
||
| 51 | private static $default_to_first_result = true; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * |
||
| 55 | * tells you if CURL / file_get_contents is available |
||
| 56 | * we recommend you set to true , unless it is not sure if CURL is available |
||
| 57 | * |
||
| 58 | * @var boolean |
||
| 59 | */ |
||
| 60 | private static $server_side_available = true; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * alternative to api key |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | private static $google_client_id = ""; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * alternative api key. |
||
| 70 | * |
||
| 71 | * This has been added so that you can set up IP restrictions and HTTP referrer |
||
| 72 | * restrictions within the Google API Console (you can not have both at the same time). |
||
| 73 | * |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | private static $alternative_google_map_api_key = ""; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Get first placemark as flat array |
||
| 80 | * |
||
| 81 | * @param string $q |
||
| 82 | * @param bool $tryAnyway |
||
| 83 | * @param array $params |
||
| 84 | * |
||
| 85 | * @return array |
||
| 86 | */ |
||
| 87 | public static function get_placemark_as_array($q, $tryAnyway = false, $params = []) |
||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * Get first placemark from google, or return false. |
||
| 146 | * |
||
| 147 | * @param string $q |
||
| 148 | * @param bool $tryAnyway |
||
| 149 | * @param array $params |
||
| 150 | * |
||
| 151 | * @return Object Single placemark | false |
||
| 152 | */ |
||
| 153 | protected static function get_placemark($q, $tryAnyway = false, $params = []) |
||
| 169 | |||
| 170 | |||
| 171 | /** |
||
| 172 | * Get geocode from google. |
||
| 173 | * |
||
| 174 | * @see http://code.google.com/apis/maps/documentation/services.html#Geocoding_Direct |
||
| 175 | * |
||
| 176 | * @param string $q Place name (e.g. 'Portland' or '30th Avenue, New York") |
||
| 177 | * @param array $params any additional params for the cURL request. |
||
| 178 | * |
||
| 179 | * @return Object Multiple Placemarks and status code |
||
|
|
|||
| 180 | */ |
||
| 181 | protected static function get_geocode_obj($q, $params = []) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * |
||
| 227 | * @param String (JSON) |
||
| 228 | * @param Boolean $assoc |
||
| 229 | * |
||
| 230 | * @return Array |
||
| 231 | */ |
||
| 232 | private static function json_decoder($content, $assoc = false) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * |
||
| 239 | * @param Array |
||
| 240 | * |
||
| 241 | * @return Array |
||
| 242 | */ |
||
| 243 | private static function json_encoder($content) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * |
||
| 250 | * |
||
| 251 | GOOGLE: |
||
| 252 | street_address indicates a precise street address. |
||
| 253 | route indicates a named route (such as "US 101"). |
||
| 254 | intersection indicates a major intersection, usually of two major roads. |
||
| 255 | political indicates a political entity. Usually, this type indicates a polygon of some civil administration. |
||
| 256 | country indicates the national political entity, and is typically the highest order type returned by the Geocoder. |
||
| 257 | administrative_area_level_1 indicates a first-order civil entity below the country level. Within the United States, these administrative levels are states. Not all nations exhibit these administrative levels. |
||
| 258 | administrative_area_level_2 indicates a second-order civil entity below the country level. Within the United States, these administrative levels are counties. Not all nations exhibit these administrative levels. |
||
| 259 | administrative_area_level_3 indicates a third-order civil entity below the country level. This type indicates a minor civil division. Not all nations exhibit these administrative levels. |
||
| 260 | colloquial_area indicates a commonly-used alternative name for the entity. |
||
| 261 | locality indicates an incorporated city or town political entity. |
||
| 262 | sublocality indicates an first-order civil entity below a locality |
||
| 263 | neighborhood indicates a named neighborhood |
||
| 264 | premise indicates a named location, usually a building or collection of buildings with a common name |
||
| 265 | subpremise indicates a first-order entity below a named location, usually a singular building within a collection of buildings with a common name |
||
| 266 | postal_code indicates a postal code as used to address postal mail within the country. |
||
| 267 | natural_feature indicates a prominent natural feature. |
||
| 268 | airport indicates an airport. |
||
| 269 | park indicates a named park. |
||
| 270 | point_of_interest indicates a named point of interest. Typically, these "POI"s are prominent local entities that don't easily fit in another category such as "Empire State Building" or "Statue of Liberty." |
||
| 271 | |||
| 272 | post_box indicates a specific postal box. |
||
| 273 | street_number indicates the precise street number. |
||
| 274 | floor indicates the floor of a building address. |
||
| 275 | room indicates the room of a building address. |
||
| 276 | |||
| 277 | SS: |
||
| 278 | 'Latitude' => 'Double(12,7)', |
||
| 279 | 'Longitude' => 'Double(12,7)', |
||
| 280 | 'PointString' => 'Text', |
||
| 281 | 'Address' => 'Text', |
||
| 282 | 'FullAddress' => 'Text', |
||
| 283 | 'CountryNameCode' => 'Varchar(3)', |
||
| 284 | 'AdministrativeAreaName' => 'Varchar(255)', |
||
| 285 | 'SubAdministrativeAreaName' => 'Varchar(255)', |
||
| 286 | 'LocalityName' => 'Varchar(255)', |
||
| 287 | 'PostalCodeNumber' => 'Varchar(30)', |
||
| 288 | */ |
||
| 289 | |||
| 290 | protected static $google_2_ss_translation_array = array( |
||
| 291 | "administrative_area_level_1" => "AdministrativeAreaName", |
||
| 292 | //two into one |
||
| 293 | "locality" => "SubAdministrativeAreaName", |
||
| 294 | "administrative_area_level_2" => "SubAdministrativeAreaName", |
||
| 295 | //two into one! |
||
| 296 | "sublocality" => "LocalityName", |
||
| 297 | "locality" => "LocalityName", |
||
| 298 | //two into one! |
||
| 299 | "street_address" => "FullAddress", |
||
| 300 | "formatted_address" => "FullAddress", |
||
| 301 | //key ones |
||
| 302 | "lng" => "Longitude", |
||
| 303 | "lat" => "Latitude", |
||
| 304 | "country" => "CountryNameCode", |
||
| 305 | "postal_code" => "PostalCodeNumber" |
||
| 306 | ); |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Converts Google Response INTO Silverstripe Google Map Array |
||
| 310 | * that can be saved into a GoogleMapLocationsObject |
||
| 311 | * @param GoogleResponseObject (JSON) |
||
| 312 | * @return Array |
||
| 313 | */ |
||
| 314 | protected static function google_2_ss($responseObj) |
||
| 350 | } |
||
| 351 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.