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 Google Geocoding Service (v3). |
||
5 | * |
||
6 | * Webservice documentation: http://code.google.com/apis/maps/documentation/geocoding/ |
||
7 | * |
||
8 | * @licence GNU GPL v2+ |
||
9 | * @author Jeroen De Dauw < [email protected] > |
||
10 | * @author Sergey Chernyshev |
||
11 | * @author Desiree Gennaro |
||
12 | */ |
||
13 | final class MapsGoogleGeocoder extends \Maps\Geocoder { |
||
14 | |||
15 | /** |
||
16 | * Registers the geocoder. |
||
17 | * |
||
18 | * No LSB in pre-5.3 PHP *sigh*. |
||
19 | * This is to be refactored as soon as php >=5.3 becomes acceptable. |
||
20 | * |
||
21 | * @since 0.7 |
||
22 | */ |
||
23 | public static function register() { |
||
24 | \Maps\Geocoders::registerGeocoder( 'google', __CLASS__ ); |
||
25 | return true; |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * @see \Maps\Geocoder::getRequestUrl |
||
30 | * |
||
31 | * @since 0.7 |
||
32 | * |
||
33 | * @param string $address |
||
34 | * |
||
35 | * @return string |
||
36 | */ |
||
37 | protected function getRequestUrl( $address ) { |
||
0 ignored issues
–
show
|
|||
38 | $urlArgs = [ |
||
39 | 'address' => urlencode( $address ) |
||
40 | ]; |
||
41 | if ( $GLOBALS['egMapsGMaps3ApiKey'] !== '' ) { |
||
42 | $urlArgs['key'] = $GLOBALS['egMapsGMaps3ApiKey']; |
||
43 | } |
||
44 | if ( $GLOBALS['egMapsGMaps3ApiVersion'] !== '' ) { |
||
45 | $urlArgs['v'] = $GLOBALS['egMapsGMaps3ApiVersion']; |
||
46 | } |
||
47 | |||
48 | return 'https://maps.googleapis.com/maps/api/geocode/xml?' . wfArrayToCgi($urlArgs); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @see \Maps\Geocoder::parseResponse |
||
53 | * |
||
54 | * @since 0.7 |
||
55 | * |
||
56 | * @param string $response |
||
57 | * |
||
58 | * @return array |
||
59 | */ |
||
60 | protected function parseResponse( $response ) { |
||
61 | $lon = self::getXmlElementValue( $response, 'lng' ); |
||
62 | $lat = self::getXmlElementValue( $response, 'lat' ); |
||
63 | |||
64 | // In case on of the values is not found, return false. |
||
65 | if ( !$lon || !$lat ) return false; |
||
0 ignored issues
–
show
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
Loading history...
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 For '' == false // true
'' == null // true
'ab' == false // false
'ab' == null // false
// It is often better to use strict comparison
'' === false // false
'' === null // false
Loading history...
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 For '' == false // true
'' == null // true
'ab' == false // false
'ab' == null // false
// It is often better to use strict comparison
'' === false // false
'' === null // false
Loading history...
|
|||
66 | |||
67 | return [ |
||
68 | 'lat' => (float)$lat, |
||
69 | 'lon' => (float)$lon |
||
70 | ]; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @see \Maps\Geocoder::getOverrides |
||
75 | * |
||
76 | * @since 0.7 |
||
77 | * |
||
78 | * @return array |
||
79 | */ |
||
80 | public static function getOverrides() { |
||
81 | return [ 'googlemaps3' ]; |
||
82 | } |
||
83 | |||
84 | } |
||
85 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: