Item::getAuthor()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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;
12
13
use FeedIo\Feed\Item\Media;
14
use FeedIo\Feed\Item\MediaInterface;
15
use FeedIo\Feed\Item\Author;
16
use FeedIo\Feed\Item\AuthorInterface;
17
18
class Item extends Node implements ItemInterface
19
{
20
21
    /**
22
     * @var \ArrayIterator
23
     */
24
    protected $medias;
25
26
    /**
27
     * @var AuthorInterface
28
     */
29
    protected $author;
30
31 93
    public function __construct()
32
    {
33 93
        $this->medias = new \ArrayIterator();
34
35 93
        parent::__construct();
36 93
    }
37
38
    /**
39
     * @param  MediaInterface $media
40
     * @return ItemInterface
41
     */
42 11
    public function addMedia(MediaInterface $media) : ItemInterface
43
    {
44 11
        $this->medias->append($media);
45
46 11
        return $this;
47
    }
48
49
    /**
50
     * @return iterable
51
     */
52 12
    public function getMedias() : iterable
53
    {
54 12
        return $this->medias;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->medias; (ArrayIterator) is incompatible with the return type declared by the interface FeedIo\Feed\ItemInterface::getMedias of type FeedIo\Feed\iterable.

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...
55
    }
56
57
    /**
58
     * @return boolean
59
     */
60 12
    public function hasMedia() : bool
61
    {
62 12
        return $this->medias->count() > 0;
63
    }
64
65
    /**
66
     * @return MediaInterface
67
     */
68 6
    public function newMedia() : MediaInterface
69
    {
70 6
        return new Media();
71
    }
72
73
    /**
74
     * @return AuthorInterface
75
     */
76 11
    public function getAuthor() : ? AuthorInterface
77
    {
78 11
        return $this->author;
79
    }
80
81
    /**
82
     * @param  AuthorInterface $author
83
     * @return ItemInterface
84
     */
85 17
    public function setAuthor(AuthorInterface $author = null) : ItemInterface
86
    {
87 17
        $this->author = $author;
88
89 17
        return $this;
90
    }
91
92
    /**
93
     * @return AuthorInterface
94
     */
95 12
    public function newAuthor() : AuthorInterface
96
    {
97 12
        return new Author();
98
    }
99
}
100