Passed
Pull Request — master (#38)
by Christophe
02:59
created

Map   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 43.75%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 52
ccs 7
cts 16
cp 0.4375
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getPackage() 0 4 1
A getType() 0 4 1
A getAvailableEventTypes() 0 7 1
A getOptions() 0 4 1
A setOptions() 0 6 1
1
<?php
2
3
namespace CMEN\GoogleChartsBundle\GoogleCharts\Charts;
4
5
use CMEN\GoogleChartsBundle\GoogleCharts\Chart;
6
use CMEN\GoogleChartsBundle\GoogleCharts\EventType;
7
use CMEN\GoogleChartsBundle\GoogleCharts\Options\Map\MapOptions;
8
9
/**
10
 * @author Christophe Meneses
11
 */
12
class Map extends Chart
13
{
14
    /**
15
     * @var MapOptions
16
     */
17
    protected $options;
18
19 1
    public function __construct()
20
    {
21 1
        parent::__construct();
22
23 1
        $this->options = new MapOptions();
24 1
    }
25
26
    public function getType()
27
    {
28
        return 'Map';
29
    }
30
31
    public function getPackage()
32
    {
33
        return 'map';
34
    }
35
36 1
    public function getAvailableEventTypes()
37
    {
38
        return [
39 1
            EventType::ERROR,
40 1
            EventType::SELECT,
41
        ];
42
    }
43
44
    /**
45
     * @return MapOptions
46
     */
47
    public function getOptions()
48
    {
49
        return $this->options;
50
    }
51
52
    /**
53
     * @param MapOptions $options
54
     *
55
     * @return Map
56
     */
57
    public function setOptions($options)
58
    {
59
        $this->options = $options;
60
61
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (CMEN\GoogleChartsBundle\GoogleCharts\Charts\Map) is incompatible with the return type declared by the abstract method CMEN\GoogleChartsBundle\...harts\Chart::setOptions of type CMEN\GoogleChartsBundle\...s\ChartOptionsInterface.

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 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('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...
62
    }
63
}
64