Completed
Push — master ( f04bbc...eab9d5 )
by Jeroen De
07:09
created

Circle::setCircleRadius()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Maps\Elements;
4
5
use DataValues\Geo\Values\LatLongValue;
6
use InvalidArgumentException;
7
8
/**
9
 * @since 3.0
10
 *
11
 *
12
 * @licence GNU GPL v2+
13
 * @author Kim Eik < [email protected] >
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class Circle extends \MapsBaseFillableElement {
17
18
	/**
19
	 * @var LatLongValue
20
	 */
21
	protected $circleCentre;
22
23
	/**
24
	 * @var integer|float
25
	 */
26
	protected $circleRadius;
27
28
	/**
29
	 * @param LatLongValue $circleCentre
30
	 * @param integer|float $circleRadius
31
	 *
32
	 * @throws InvalidArgumentException
33
	 */
34 7
	public function __construct( LatLongValue $circleCentre, $circleRadius ) {
35 7
		if ( !is_float( $circleRadius ) && !is_int( $circleRadius ) ) {
36 1
			throw new InvalidArgumentException( '$circleRadius must be a float or int' );
37
		}
38
39 6
		if ( $circleRadius <= 0 ) {
40 2
			throw new InvalidArgumentException( '$circleRadius must be greater than zero' );
41
		}
42
43 4
		parent::__construct();
44
45 4
		$this->setCircleCentre( $circleCentre );
46 4
		$this->setCircleRadius( $circleRadius );
47 4
	}
48
49
	/**
50
	 * @return LatLongValue
51
	 */
52 4
	public function getCircleCentre() {
53 4
		return $this->circleCentre;
54
	}
55
56
	/**
57
	 * @param LatLongValue $circleCentre
58
	 */
59 4
	public function setCircleCentre( LatLongValue $circleCentre ) {
60 4
		$this->circleCentre = $circleCentre;
61 4
	}
62
63
	/**
64
	 * @return integer|float
65
	 */
66 4
	public function getCircleRadius() {
67 4
		return $this->circleRadius;
68
	}
69
70
	/**
71
	 * @param integer|float $circleRadius
72
	 */
73 4
	public function setCircleRadius( $circleRadius ) {
74 4
		$this->circleRadius = $circleRadius;
75 4
	}
76
77
	public function getJSONObject( $defText = '' , $defTitle = '' ) {
78
		$parentArray = parent::getJSONObject( $defText , $defTitle );
79
80
		$array = [
81
			'centre' => [
82
				'lon' => $this->getCircleCentre()->getLongitude(),
83
				'lat' => $this->getCircleCentre()->getLatitude()
84
			],
85
			'radius' => intval( $this->getCircleRadius() ),
86
		];
87
88
		return array_merge( $parentArray, $array );
0 ignored issues
show
Best Practice introduced by
The expression return array_merge($parentArray, $array); seems to be an array, but some of its elements' types (array<string,double>) are incompatible with the return type of the parent method MapsBaseFillableElement::getJSONObject of type array<string,string>.

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 new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
89
	}
90
91
}
92