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
|
|||
68 | } |
||
69 | |||
70 | } |
||
71 |
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.