Completed
Push — master ( 684184...9a35aa )
by Jeroen De
08:07
created

src/Elements/Polygon.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Maps\Elements;
4
5
use InvalidArgumentException;
6
7
/**
8
 * @since 3.0
9
 *
10
 * @licence GNU GPL v2+
11
 * @author Kim Eik < [email protected] >
12
 * @author Jeroen De Dauw < [email protected] >
13
 */
14
class Polygon extends Line implements \iHoverableMapElement {
15
16
	protected $onlyVisibleOnHover = false;
17
	protected $fillOpacity = '0.5';
18
	protected $fillColor = '#FF0000';
19
20
	/**
21
	 * @since 3.0
22
	 *
23
	 * @param boolean $visible
24
	 *
25
	 * @throws InvalidArgumentException
26
	 */
27
	public function setOnlyVisibleOnHover( $visible ) {
28
		if ( !is_bool( $visible ) ) {
29
			throw new InvalidArgumentException( '$visible should be a boolean' );
30
		}
31
32
		$this->onlyVisibleOnHover = $visible;
33
	}
34
35
	/**
36
	 * @since 3.0
37
	 *
38
	 * @return boolean
39
	 */
40
	public function isOnlyVisibleOnHover() {
41
		return $this->onlyVisibleOnHover;
42
	}
43
44
	public function setFillOpacity( $fillOpacity ) {
45
		if ( !is_string( $fillOpacity ) ) {
46
			throw new InvalidArgumentException( '$fillOpacity should be a string' );
47
		}
48
49
		$this->fillOpacity = $fillOpacity;
50
	}
51
52
	public function setFillColor( $fillColor ) {
53
		if ( !is_string( $fillColor ) ) {
54
			throw new InvalidArgumentException( '$fillColor should be a string' );
55
		}
56
57
		$this->fillColor = $fillColor;
58
	}
59
60
	public function getJSONObject( $defText = '' , $defTitle = '' ) {
61
		$json = parent::getJSONObject( $defText, $defTitle );
62
63
		$json['onlyVisibleOnHover'] = $this->onlyVisibleOnHover;
64
		$json['fillColor'] = $this->fillColor;
65
		$json['fillOpacity'] = $this->fillOpacity;
66
67
		return $json;
0 ignored issues
show
The expression return $json; seems to be an array, but some of its elements' types (boolean) are incompatible with the return type of the parent method Maps\Elements\Line::getJSONObject of type array<string,string|array>.

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...
68
	}
69
70
}
71