Completed
Push — nominatim-geocoder ( cb2402 )
by Peter
11:27
created

MapsNominatimGeocoder::parseResponse()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 11
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 20
ccs 0
cts 10
cp 0
crap 20
rs 9.2
1
<?php
2
3
/**
4
 * Class for geocoding requests with the OSM Nominatim.
5
 * 
6
 * Webservice documentation: http://wiki.openstreetmap.org/wiki/Nominatim
7
 *
8
 * @licence GNU GPL v2+
9
 * @author Peter Grassberger < [email protected] >
10
 */
11
final class MapsNominatimGeocoder 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 0.7
20
	 */
21
	public static function register() {
22
		\Maps\Geocoders::registerGeocoder( 'nominatim', __CLASS__ );
23
		return true;
24
	}
25
	
26
	/**
27
	 * @see \Maps\Geocoder::getRequestUrl
28
	 * 
29
	 * @since 0.7
30
	 * 
31
	 * @param string $address
32
	 * 
33
	 * @return string
34
	 */	
35
	protected function getRequestUrl( $address ) {
36
		$urlArgs = [
37
			'q' => urlencode( $address ),
38
			'format' => 'jsonv2',
39
			'limit' => 1,
40
		];
41
42
		return 'http://nominatim.openstreetmap.org/search?' . wfArrayToCgi($urlArgs);
43
	}
44
	
45
	/**
46
	 * @see \Maps\Geocoder::parseResponse
47
	 * 
48
	 * @since 0.7
49
	 * 
50
	 * @param string $response
51
	 * 
52
	 * @return array
53
	 */
54
	protected function parseResponse( $response ) {
55
		$jsonResponse = json_decode( $response );
56
57
		if (count($jsonResponse) < 1) {
58
			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...
59
		}
60
61
		$location  = $jsonResponse[0];
62
63
		$lon = $location->lon;
64
		$lat = $location->lat;
65
66
		// In case on of the values is not found, return false.
67
		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...
68
69
		return [
70
			'lat' => (float)$lat,
71
			'lon' => (float)$lon
72
		];
73
	}
74
	
75
}
76