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
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...
|
|||
51 | |||
52 | return [ |
||
53 | 'lat' => (float)$lat, |
||
54 | 'lon' => (float)$lon |
||
55 | ]; |
||
56 | } |
||
57 | } |
||
58 |
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.