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

CircleParser::stringParse()   D

Complexity

Conditions 8
Paths 128

Size

Total Lines 36
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 9.1669

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 36
ccs 14
cts 19
cp 0.7368
rs 4.6666
cc 8
eloc 19
nc 128
nop 1
crap 9.1669
1
<?php
2
3
namespace Maps;
4
5
use Maps\Elements\Circle;
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 CircleParser 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 Circle
35
	 */
36
	public function parse( $value ) {
37
		$metaData = explode( $this->metaDataSeparator , $value );
38
		$circleData = explode( ':' , array_shift( $metaData ) );
39
40
		$circle = new Circle( $this->stringToLatLongValue( $circleData[0] ), (float)$circleData[1] );
0 ignored issues
show
Bug introduced by
It seems like $this->stringToLatLongValue($circleData[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...
41
42
		if ( $metaData !== [] ) {
43
			$circle->setTitle( array_shift( $metaData ) );
44
		}
45
46
		if ( $metaData !== [] ) {
47
			$circle->setText( array_shift( $metaData ) );
48
		}
49
50
		if ( $metaData !== [] ) {
51
			$circle->setStrokeColor( array_shift( $metaData ) );
52
		}
53
54
		if ( $metaData !== [] ) {
55
			$circle->setStrokeOpacity( array_shift( $metaData ) );
56
		}
57
58
		if ( $metaData !== [] ) {
59
			$circle->setStrokeWeight( array_shift( $metaData ) );
60
		}
61
62
		if ( $metaData !== [] ) {
63
			$circle->setFillColor( array_shift( $metaData ) );
64
		}
65
66
		if ( $metaData !== [] ) {
67
			$circle->setFillOpacity( array_shift( $metaData ) );
68
		}
69
70
		return $circle;
71
	}
72
73
	private function stringToLatLongValue( $location ) {
74
		$latLong = $this->geocoder->geocode( $location );
75
76
		if ( $location === null ) {
77
			throw new ParseException( 'Failed to parse or geocode' );
78
		}
79
80
		return $latLong;
81
	}
82
83
}
84