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

RectangleParser::parse()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.439
c 0
b 0
f 0
cc 6
eloc 17
nc 32
nop 1
1
<?php
2
3
namespace Maps;
4
5
use Maps\Elements\Rectangle;
6
use ValueParsers\ParseException;
7
use ValueParsers\StringValueParser;
8
use ValueParsers\ValueParser;
9
10
/**
11
 * @since 3.0
12
 *
13
 * @licence GNU GPL v2+
14
 * @author Kim Eik
15
 * @author Jeroen De Dauw < [email protected] >
16
 */
17
class RectangleParser implements ValueParser {
18
19
	private $metaDataSeparator = '~';
20
21
	private $geocoder;
22
23
	public function __construct() {
24
		$this->geocoder = MapsFactory::newDefault()->newGeocoder();
25
	}
26
27
	/**
28
	 * @see StringValueParser::stringParse
29
	 *
30
	 * @since 3.0
31
	 *
32
	 * @param string $value
33
	 *
34
	 * @return Rectangle
35
	 */
36
	public function parse( $value ) {
37
		$metaData = explode( $this->metaDataSeparator , $value );
38
		$rectangleData = explode( ':' , array_shift( $metaData ) );
39
40
		$rectangle = new Rectangle(
41
			$this->stringToLatLongValue( $rectangleData[0] ),
0 ignored issues
show
Bug introduced by
It seems like $this->stringToLatLongValue($rectangleData[0]) can be null; however, __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
			$this->stringToLatLongValue( $rectangleData[1] )
0 ignored issues
show
Bug introduced by
It seems like $this->stringToLatLongValue($rectangleData[1]) can be null; however, __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...
43
		);
44
45
		if ( $metaData !== [] ) {
46
			$rectangle->setTitle( array_shift( $metaData ) );
47
		}
48
49
		if ( $metaData !== [] ) {
50
			$rectangle->setText( array_shift( $metaData ) );
51
		}
52
53
		if ( $metaData !== [] ) {
54
			$rectangle->setStrokeColor( array_shift( $metaData ) );
55
		}
56
57
		if ( $metaData !== [] ) {
58
			$rectangle->setStrokeOpacity( array_shift( $metaData ) );
59
		}
60
61
		if ( $metaData !== [] ) {
62
			$rectangle->setStrokeWeight( array_shift( $metaData ) );
63
		}
64
65
		return $rectangle;
66
	}
67
68
	private function stringToLatLongValue( $location ) {
69
		$latLong = $this->geocoder->geocode( $location );
70
71
		if ( $location === null ) {
72
			throw new ParseException( 'Failed to parse or geocode' );
73
		}
74
75
		return $latLong;
76
	}
77
78
}
79