Passed
Push — master ( e69457...6104b4 )
by Christophe
03:08
created

GanttChart::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace CMEN\GoogleChartsBundle\GoogleCharts\Charts;
4
5
use CMEN\GoogleChartsBundle\GoogleCharts\Chart;
6
use CMEN\GoogleChartsBundle\GoogleCharts\Options\GanttChart\GanttChartOptions;
7
8
/**
9
 * @author Christophe Meneses
10
 */
11
class GanttChart extends Chart
12
{
13
    /**
14
     * @var GanttChartOptions
15
     */
16
    protected $options;
17
18
    /**
19
     * GanttChart constructor.
20
     */
21 1
    public function __construct()
22
    {
23 1
        parent::__construct();
24
25 1
        $this->options = new GanttChartOptions();
26 1
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 1
    public function getType()
32
    {
33 1
        return 'Gantt';
34 1
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 1
    public function getPackage()
40
    {
41 1
        return 'gantt';
42
    }
43
44
    /**
45
     * @return GanttChartOptions
46
     */
47 1
    public function getOptions()
48
    {
49 1
        return $this->options;
50
    }
51
52
    /**
53
     * @param $options GanttChartOptions
54
     *
55
     * @return GanttChart
56
     */
57
    public function setOptions($options)
58
    {
59
        $this->options = $options;
0 ignored issues
show
Documentation Bug introduced by
$options is of type object<CMEN\GoogleCharts...\ChartOptionsInterface>, but the property $options was declared to be of type object<CMEN\GoogleCharts...hart\GanttChartOptions>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
60
61
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (CMEN\GoogleChartsBundle\...harts\Charts\GanttChart) 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