Completed
Push — killgeocoders ( 08fbcb )
by Jeroen De
06:57
created

ImageOverlayParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Maps;
4
5
use Maps\Elements\ImageOverlay;
6
use Maps\Elements\WmsOverlay;
7
use ValueParsers\ParseException;
8
use ValueParsers\ValueParser;
9
10
/**
11
 * @since 3.1
12
 *
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class ImageOverlayParser implements ValueParser {
17
18
	private $geocoder;
19
20
	public function __construct() {
21
		$this->geocoder = MapsFactory::newDefault()->newGeocoder();
22
	}
23
24
	/**
25
	 * @since 3.1
26
	 *
27
	 * @param string $value
28
	 *
29
	 * @return ImageOverlay
30
	 * @throws ParseException
31
	 */
32
	public function parse( $value ) {
33
		$parameters = explode( '~', $value );
34
		$imageParameters = explode( ':', $parameters[0], 3 );
35
36
		if ( count( $imageParameters ) === 3 ) {
37
			$boundsNorthEast = $this->stringToLatLongValue( $imageParameters[0] );
38
			$boundsSouthWest = $this->stringToLatLongValue( $imageParameters[1] );
39
			$imageUrl = \MapsMapper::getFileUrl( $imageParameters[2] );
0 ignored issues
show
Deprecated Code introduced by
The method MapsMapper::getFileUrl() has been deprecated.

This method has been deprecated.

Loading history...
40
41
			return new ImageOverlay( $boundsNorthEast, $boundsSouthWest, $imageUrl );
0 ignored issues
show
Bug introduced by
It seems like $boundsNorthEast defined by $this->stringToLatLongValue($imageParameters[0]) on line 37 can be null; however, Maps\Elements\ImageOverlay::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
Bug introduced by
It seems like $boundsSouthWest defined by $this->stringToLatLongValue($imageParameters[1]) on line 38 can be null; however, Maps\Elements\ImageOverlay::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
42
		}
43
44
		throw new ParseException( 'Need 3 parameters for an image overlay' );
45
	}
46
47
	private function stringToLatLongValue( $location ) {
48
		$latLong = $this->geocoder->geocode( $location );
49
50
		if ( $location === null ) {
51
			throw new ParseException( 'Failed to parse or geocode' );
52
		}
53
54
		return $latLong;
55
	}
56
57
}
58