Completed
Pull Request — master (#257)
by Éloi
02:08
created

MediaThumbnail::setHeight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the feed-io package.
4
 *
5
 * (c) Alexandre Debril <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace FeedIo\Feed\Item;
12
13
class MediaThumbnail implements MediaThumbnailInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $url;
19
20
    /**
21
     * @var int
22
     */
23
    protected $width;
24
25
    /**
26
     * @var int
27
     */
28
    protected $height;
29
30
    /**
31
     * @var \DateTime
32
     */
33
    protected $time;
34
35
36
    /**
37
     * @return string
38
     */
39 3
    public function getUrl() : ? string
40
    {
41 3
        return $this->url;
42
    }
43
44
    /**
45
     * @param  string $url
46
     * @return MediaThumbnailInterface
47
     */
48 3
    public function setUrl(?string $url) : MediaThumbnailInterface
49
    {
50 3
        $this->url = $url;
51
52 3
        return $this;
53
    }
54
55
    /**
56
     * @return int
57
     */
58 2
    public function getWidth() : ?int
59
    {
60 2
        return $this->width;
61
    }
62
63
    /**
64
     * @param  int $width
65
     * @return MediaThumbnailInterface
66
     */
67 3
    public function setWidth(?int $width) : MediaThumbnailInterface
68
    {
69 3
        $this->width = $width;
70
71 3
        return $this;
72
    }
73
74
    /**
75
     * @return int
76
     */
77 2
    public function getHeight() : ?int
78
    {
79 2
        return $this->height;
80
    }
81
82
    /**
83
     * @param  int $height
84
     * @return MediaThumbnailInterface
85
     */
86 3
    public function setHeight(?int $height) : MediaThumbnailInterface
87
    {
88 3
        $this->height = $height;
89
90 3
        return $this;
91
    }
92
93
    /**
94
     * @return \DateTime
95
     */
96 2
    public function getTime() : ? \DateTime
97
    {
98 2
        return $this->time;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->time; (DateTime) is incompatible with the return type declared by the interface FeedIo\Feed\Item\MediaThumbnailInterface::getTime of type FeedIo\Feed\Item\DateTime.

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...
99
    }
100
101
    /**
102
     * @param  \DateTime $time
103
     * @return MediaThumbnailInterface
104
     */
105 1
    public function setTime(? \DateTime $time) : MediaThumbnailInterface
106
    {
107 1
        $this->time = $time;
108
109 1
        return $this;
110
    }
111
}
112