Completed
Push — address-as-title ( 9b6eeb...601934 )
by Peter
11:07
created

Rectangle::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 11
rs 9.4285
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 Rectangle extends \MapsBaseFillableElement {
17
18
	/**
19
	 * @since 3.0
20
	 * @var LatLongValue
21
	 */
22
	protected $rectangleNorthEast;
23
24
	/**
25
	 * @since 3.0
26
	 * @var LatLongValue
27
	 */
28
	protected $rectangleSouthWest;
29
30
	/**
31
	 * @since 3.0
32
	 *
33
	 * @param LatLongValue $rectangleNorthEast
34
	 * @param LatLongValue $rectangleSouthWest
35
	 *
36
	 * @throws InvalidArgumentException
37
	 */
38
	public function __construct( LatLongValue $rectangleNorthEast, LatLongValue $rectangleSouthWest ) {
39
		if ( $rectangleNorthEast->equals( $rectangleSouthWest ) ) {
40
			throw new InvalidArgumentException( '$rectangleNorthEast cannot be equal to $rectangleSouthWest' );
41
		}
42
43
		parent::__construct();
44
45
		// TODO: validate bounds are correct, if not, flip
46
		$this->setRectangleNorthEast( $rectangleNorthEast );
47
		$this->setRectangleSouthWest( $rectangleSouthWest );
48
	}
49
50
	/**
51
	 * @since 3.0
52
	 *
53
	 * @return LatLongValue
54
	 */
55
	public function getRectangleNorthEast() {
56
		return $this->rectangleNorthEast;
57
	}
58
59
	/**
60
	 * @since 3.0
61
	 *
62
	 * @return LatLongValue
63
	 */
64
	public function getRectangleSouthWest() {
65
		return $this->rectangleSouthWest;
66
	}
67
68
	/**
69
	 * @since 3.0
70
	 *
71
	 * @param LatLongValue $rectangleSouthWest
72
	 */
73
	public function setRectangleSouthWest( LatLongValue $rectangleSouthWest ) {
74
		$this->rectangleSouthWest = $rectangleSouthWest;
75
	}
76
77
	/**
78
	 * @since 3.0
79
	 *
80
	 * @param LatLongValue $rectangleNorthEast
81
	 */
82
	public function setRectangleNorthEast( LatLongValue $rectangleNorthEast ) {
83
		$this->rectangleNorthEast = $rectangleNorthEast;
84
	}
85
86
	/**
87
	 * @since 3.0
88
	 *
89
	 * @param string $defText
90
	 * @param string $defTitle
91
	 *
92
	 * @return array
93
	 */
94
	public function getJSONObject( $defText = '' , $defTitle = '' ) {
95
		$parentArray = parent::getJSONObject( $defText , $defTitle );
96
		$array = [
97
			'ne' => [
98
				'lon' => $this->getRectangleNorthEast()->getLongitude(),
99
				'lat' => $this->getRectangleNorthEast()->getLatitude()
100
			],
101
			'sw' => [
102
				'lon' => $this->getRectangleSouthWest()->getLongitude(),
103
				'lat' => $this->getRectangleSouthWest()->getLatitude()
104
			],
105
		];
106
107
		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...
108
	}
109
110
}
111