Complex classes like Geocoders 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 Geocoders, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | final class Geocoders { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Associative with geoservice identifiers as keys containing instances of |
||
| 26 | * the geocoder classes. |
||
| 27 | * |
||
| 28 | * Note: This list only contains the instances, so is not to be used for |
||
| 29 | * looping over all available services, as not all of them are guaranteed |
||
| 30 | * to have an instance already, use $registeredServices for this purpouse. |
||
| 31 | * |
||
| 32 | * @since 0.7 |
||
| 33 | * |
||
| 34 | * @var Geocoder[] |
||
| 35 | */ |
||
| 36 | protected static $geocoders = []; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Associative with geoservice identifiers as keys containing the class |
||
| 40 | * name of the geocoders. This is used for registration of a geocoder |
||
| 41 | * without immediately instantiating it. |
||
| 42 | * |
||
| 43 | * @since 0.7 |
||
| 44 | * |
||
| 45 | * @var array of string => string |
||
| 46 | */ |
||
| 47 | public static $registeredGeocoders = []; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The global geocoder cache, holding geocoded data when enabled. |
||
| 51 | * |
||
| 52 | * @since 0.7 |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | private static $globalGeocoderCache = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Can geocoding happen, ie are there any geocoders available. |
||
| 60 | * |
||
| 61 | * @since 1.0.3 |
||
| 62 | * @var boolean |
||
| 63 | */ |
||
| 64 | protected static $canGeocode = false; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Returns if this class can do geocoding operations. |
||
| 68 | * Ie. if there are any geocoders available. |
||
| 69 | * |
||
| 70 | * @since 0.7 |
||
| 71 | * |
||
| 72 | * @return boolean |
||
| 73 | */ |
||
| 74 | public static function canGeocode() { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Gets a list of available geocoders. |
||
| 81 | * |
||
| 82 | * @since 1.0.3 |
||
| 83 | * |
||
| 84 | * @return array |
||
| 85 | */ |
||
| 86 | public static function getAvailableGeocoders() { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Initiate the geocoding functionality. |
||
| 93 | * |
||
| 94 | * @since 1.0.3 |
||
| 95 | * |
||
| 96 | * @return boolean Indicates if init happened |
||
| 97 | */ |
||
| 98 | public static function init() { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * This function first determines whether the provided string is a pair or coordinates |
||
| 118 | * or an address. If it's the later, an attempt to geocode will be made. The function will |
||
| 119 | * return the coordinates or false, in case a geocoding attempt was made but failed. |
||
| 120 | * |
||
| 121 | * @since 0.7 |
||
| 122 | * |
||
| 123 | * @param string $coordsOrAddress |
||
| 124 | * @param string $geoservice |
||
| 125 | * @param string|false $mappingService |
||
| 126 | * @param boolean $checkForCoords |
||
| 127 | * |
||
| 128 | * @return LatLongValue|false |
||
| 129 | */ |
||
| 130 | public static function attemptToGeocode( $coordsOrAddress, $geoservice = '', $mappingService = false, $checkForCoords = true ) { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * |
||
| 147 | * |
||
| 148 | * @since 0.7 |
||
| 149 | * |
||
| 150 | * @param string $coordsOrAddress |
||
| 151 | * @param string $geoService |
||
| 152 | * @param string|false $mappingService |
||
| 153 | * |
||
| 154 | * @return boolean |
||
| 155 | */ |
||
| 156 | public static function isLocation( $coordsOrAddress, $geoService = '', $mappingService = false ) { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Geocodes an address with the provided geocoding service and returns the result |
||
| 162 | * as a string with the optionally provided format, or false when the geocoding failed. |
||
| 163 | * |
||
| 164 | * @since 0.7 |
||
| 165 | * |
||
| 166 | * @param string $coordsOrAddress |
||
| 167 | * @param string $service |
||
| 168 | * @param string $mappingService |
||
| 169 | * @param boolean $checkForCoords |
||
| 170 | * @param string $targetFormat The notation to which they should be formatted. Defaults to floats. |
||
| 171 | * @param boolean $directional Indicates if the target notation should be directional. Defaults to false. |
||
| 172 | * |
||
| 173 | * @return string|false |
||
| 174 | */ |
||
| 175 | public static function attemptToGeocodeToString( $coordsOrAddress, $service = '', $mappingService = false, $checkForCoords = true, $targetFormat = Maps_COORDS_FLOAT, $directional = false ) { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Geocodes an address with the provided geocoding service and returns the result |
||
| 194 | * as an array, or false when the geocoding failed. |
||
| 195 | * |
||
| 196 | * FIXME: complexity |
||
| 197 | * |
||
| 198 | * @since 0.7 |
||
| 199 | * |
||
| 200 | * @param string $address |
||
| 201 | * @param string $geoService |
||
| 202 | * @param string|false $mappingService |
||
| 203 | * |
||
| 204 | * @return LatLongValue|false |
||
| 205 | * @throws MWException |
||
| 206 | */ |
||
| 207 | public static function geocode( $address, $geoService = '', $mappingService = false ) { |
||
| 243 | |||
| 244 | private static function getGeocoded( Geocoder $geocoder, $address ) { |
||
| 256 | |||
| 257 | private static function getGeocodedAsArray( Geocoder $geocoder, $address ) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Returns already coordinates already known from previous geocoding operations, |
||
| 272 | * or false if there is no match found in the cache. |
||
| 273 | * |
||
| 274 | * @since 0.7 |
||
| 275 | * |
||
| 276 | * @param string $address |
||
| 277 | * |
||
| 278 | * @return LatLongValue|boolean false |
||
| 279 | */ |
||
| 280 | protected static function cacheRead( $address ) { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Writes the geocoded result to the cache if the cache is on. |
||
| 293 | * |
||
| 294 | * @since 0.7 |
||
| 295 | * |
||
| 296 | * @param string $address |
||
| 297 | * @param LatLongValue $coordinates |
||
| 298 | */ |
||
| 299 | protected static function cacheWrite( $address, LatLongValue $coordinates ) { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Registers a geocoder linked to an identifier. |
||
| 310 | * |
||
| 311 | * @since 0.7 |
||
| 312 | * |
||
| 313 | * @param string $geocoderIdentifier |
||
| 314 | * @param string|\Maps\Geocoders\Geocoder $geocoder |
||
| 315 | */ |
||
| 316 | public static function registerGeocoder( $geocoderIdentifier, $geocoder ) { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Returns the instance of the geocoder linked to the provided identifier |
||
| 322 | * or the default one when it's not valid. False is returned when there |
||
| 323 | * are no geocoders available. |
||
| 324 | * |
||
| 325 | * @since 0.7 |
||
| 326 | * |
||
| 327 | * @param string $geocoderIdentifier |
||
| 328 | * |
||
| 329 | * @return Geocoder|bool |
||
| 330 | */ |
||
| 331 | protected static function getValidGeocoderInstance( $geocoderIdentifier ) { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Returns the instance of a geocoder. This function assumes there is a |
||
| 337 | * geocoder linked to the identifier you provide - if you are not sure |
||
| 338 | * it does, use getValidGeocoderInstance instead. |
||
| 339 | * |
||
| 340 | * @since 0.7 |
||
| 341 | * |
||
| 342 | * @param string $geocoderIdentifier |
||
| 343 | * |
||
| 344 | * @return Geocoder|bool |
||
| 345 | * @throws MWException |
||
| 346 | */ |
||
| 347 | protected static function getGeocoderInstance( $geocoderIdentifier ) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Returns a valid geocoder idenifier. If the given one is a valid main identifier, |
||
| 376 | * it will simply be returned. If it's an alias, it will be turned into the correponding |
||
| 377 | * main identifier. If it's not recognized at all (or empty), the default will be used. |
||
| 378 | * Only call this function when there are geocoders available, else an erro will be thrown. |
||
| 379 | * |
||
| 380 | * @since 0.7 |
||
| 381 | * |
||
| 382 | * @param string $geocoderIdentifier |
||
| 383 | * |
||
| 384 | * @return string|bool |
||
| 385 | * @throws MWException |
||
| 386 | */ |
||
| 387 | protected static function getValidGeocoderIdentifier( $geocoderIdentifier ) { |
||
| 412 | |||
| 413 | } |
||
| 414 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.