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

includes/geocoders/Maps_GeocoderusGeocoder.php (3 issues)

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 geocoder.us Service.
5
 *
6
 * @licence GNU GPL v2+
7
 */
8
final class MapsGeocoderusGeocoder extends \Maps\Geocoder {
9
10
	/**
11
	 * Registers the geocoder.
12
	 * 
13
	 * No LSB in pre-5.3 PHP *sigh*.
14
	 * This is to be refactored as soon as php >=5.3 becomes acceptable.
15
	 * 
16
	 * @since 3.0
17
	 */
18
	public static function register() {
19
		\Maps\Geocoders::registerGeocoder( 'geocoderus', __CLASS__ );
20
		return true;
21
	}
22
23
	/**
24
	 * @see \Maps\Geocoder::getRequestUrl
25
	 * 
26
	 * @since 3.0
27
	 * 
28
	 * @param string $address
29
	 * 
30
	 * @return string
31
	 */
32
	protected function getRequestUrl( $address ) {
33
		return 'http://geocoder.us/service/rest/?address=' . urlencode( $address );
34
	}
35
36
	/**
37
	 * @see \Maps\Geocoder::parseResponse
38
	 * 
39
	 * @since 3.0
40
	 * 
41
	 * @param string $response
42
	 * 
43
	 * @return array
44
	 */
45
	protected function parseResponse( $response ) {
46
		$lon = self::getXmlElementValue( $response, 'geo:long' );
47
		$lat = self::getXmlElementValue( $response, 'geo:lat' );
48
49
		// In case one of the values is not found, return false.
50
		if ( !$lon || !$lat ) return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the abstract method Maps\Geocoder::parseResponse of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
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...
51
52
		return [
53
			'lat' => (float)$lat,
54
			'lon' => (float)$lon
55
		];
56
	}
57
}
58