Completed
Push — master ( 684184...9a35aa )
by Jeroen De
08:07
created

includes/geocoders/Maps_GeonamesGeocoder.php (2 issues)

Check for loose comparison of strings.

Best Practice Bug Major

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Class for geocoding requests with the GeoNames webservice.
5
 * 
6
 * @since 1.0
7
 *
8
 * @licence GNU GPL v2+
9
 * @author Jeroen De Dauw < [email protected] >
10
 */
11
final class MapsGeonamesGeocoder extends \Maps\Geocoder {
12
	
13
	/**
14
	 * Registers the geocoder.
15
	 * 
16
	 * No LSB in pre-5.3 PHP *sigh*.
17
	 * This is to be refactored as soon as php >=5.3 becomes acceptable.
18
	 * 
19
	 * @since 1.0
20
	 */
21
	public static function register() {
22
		global $egMapsGeoNamesUser;
23
		
24
		if ( $egMapsGeoNamesUser !== '' ) {
25
			\Maps\Geocoders::registerGeocoder( 'geonames', __CLASS__ );
26
		}
27
		
28
		return true;
29
	}	
30
	
31
	/**
32
	 * @see \Maps\Geocoder::getRequestUrl
33
	 * 
34
	 * @since 1.0
35
	 * 
36
	 * @param string $address
37
	 * 
38
	 * @return string
39
	 */	
40
	protected function getRequestUrl( $address ) {
41
		global $egMapsGeoNamesUser;
42
		return 'http://api.geonames.org/search?q=' . urlencode( $address ) . '&maxRows=1&username=' . urlencode( $egMapsGeoNamesUser );
43
	}
44
	
45
	/**
46
	 * @see \Maps\Geocoder::parseResponse
47
	 * 
48
	 * @since 1.0
49
	 * 
50
	 * @param string $response
51
	 * 
52
	 * @return array
53
	 */		
54
	protected function parseResponse( $response ) {
55
		$lon = self::getXmlElementValue( $response, 'lng' );
56
		$lat = self::getXmlElementValue( $response, 'lat' );
57
58
		// In case one of the values is not found, return false.
59
		if ( !$lon || !$lat ) return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression $lon of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $lat of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
60
61
		return [
62
			'lat' => (float)$lat,
63
			'lon' => (float)$lon
64
		];		
65
	}
66
	
67
}