Completed
Push — master ( 817258...6733f8 )
by Alex
29s queued 11s
created

Node   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 260
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 260
c 0
b 0
f 0
wmc 29
lcom 2
cbo 4
ccs 65
cts 65
cp 1
rs 10

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A set() 0 11 1
A getCategories() 0 4 1
A getCategoriesGenerator() 0 6 2
A addCategory() 0 6 1
A newCategory() 0 4 1
A getTitle() 0 4 1
A setTitle() 0 6 1
A getPublicId() 0 4 1
A setPublicId() 0 6 1
A getDescription() 0 4 1
A setDescription() 0 6 1
A getLastModified() 0 4 1
A setLastModified() 0 6 1
A getHost() 0 4 1
A getLink() 0 4 1
A setLink() 0 7 1
A setHost() 0 6 2
A getValue() 0 8 2
B toArray() 0 24 7
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\Node\Category;
14
use FeedIo\Feed\Node\CategoryInterface;
15
16
class Node implements NodeInterface, ElementsAwareInterface, ArrayableInterface
17
{
18
    use ElementsAwareTrait;
19
20
    /**
21
     * @var \ArrayIterator
22
     */
23
    protected $categories;
24
25
    /**
26
     * @var string
27
     */
28
    protected $title;
29
30
    /**
31
     * @var string
32
     */
33
    protected $publicId;
34
35
    /**
36
     * @var string
37
     */
38
    protected $description;
39
40
    /**
41
     * @var \DateTime
42
     */
43
    protected $lastModified;
44
45
    /**
46
     * @var string
47
     */
48
    protected $link;
49
50
    /**
51
     * @var string
52
     */
53
    protected $host;
54
55 120
    public function __construct()
56
    {
57 120
        $this->initElements();
58 120
        $this->categories = new \ArrayIterator();
59 120
    }
60
61
    /**
62
     * @param  string $name  element name
63
     * @param  string $value element value
64
     * @return NodeInterface
65
     */
66 13
    public function set(string $name, string $value = null) : NodeInterface
67
    {
68 13
        $element = $this->newElement();
69
70 13
        $element->setName($name);
71 13
        $element->setValue($value);
72
73 13
        $this->addElement($element);
74
75 13
        return $this;
76
    }
77
78
    /**
79
     * returns node's categories
80
     *
81
     * @return iterable
82
     */
83 12
    public function getCategories() : iterable
84
    {
85 12
        return $this->categories;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->categories; (ArrayIterator) is incompatible with the return type declared by the interface FeedIo\Feed\NodeInterface::getCategories 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...
86
    }
87
88
    /**
89
     * @return \Generator
90
     */
91 5
    public function getCategoriesGenerator() : \Generator
92
    {
93 5
        foreach ($this->categories as $category) {
94 2
            yield $category->getlabel();
95
        }
96 4
    }
97
98
    /**
99
     * adds a category to the node
100
     *
101
     * @param \FeedIo\Feed\Node\CategoryInterface $category
102
     * @return NodeInterface
103
     */
104 16
    public function addCategory(CategoryInterface $category) : NodeInterface
105
    {
106 16
        $this->categories->append($category);
107
108 16
        return $this;
109
    }
110
111
    /**
112
     * returns a new CategoryInterface
113
     *
114
     * @return \FeedIo\Feed\Node\CategoryInterface
115
     */
116 9
    public function newCategory() : CategoryInterface
117
    {
118 9
        return new Category();
119
    }
120
121
    /**
122
     * @return string
123
     */
124 24
    public function getTitle() : ? string
125
    {
126 24
        return $this->title;
127
    }
128
129
    /**
130
     * @param  string $title
131
     * @return NodeInterface
132
     */
133 29
    public function setTitle(string $title = null) : NodeInterface
134
    {
135 29
        $this->title = $title;
136
137 29
        return $this;
138
    }
139
140
    /**
141
     * @return string
142
     */
143 14
    public function getPublicId() : ? string
144
    {
145 14
        return $this->publicId;
146
    }
147
148
    /**
149
     * @param  string $publicId
150
     * @return NodeInterface
151
     */
152 16
    public function setPublicId(string $publicId = null) : NodeInterface
153
    {
154 16
        $this->publicId = $publicId;
155
156 16
        return $this;
157
    }
158
159
    /**
160
     * @return string
161
     */
162 15
    public function getDescription() : ? string
163
    {
164 15
        return $this->description;
165
    }
166
167
    /**
168
     * @param  string $description
169
     * @return NodeInterface
170
     */
171 23
    public function setDescription(string $description = null) : NodeInterface
172
    {
173 23
        $this->description = $description;
174
175 23
        return $this;
176
    }
177
178
    /**
179
     * @return \DateTime
180
     */
181 27
    public function getLastModified() : ? \DateTime
182
    {
183 27
        return $this->lastModified;
184
    }
185
186
    /**
187
     * @param  \DateTime $lastModified
188
     * @return NodeInterface
189
     */
190 34
    public function setLastModified(\DateTime $lastModified = null) : NodeInterface
191
    {
192 34
        $this->lastModified = $lastModified;
193
194 34
        return $this;
195
    }
196
197
    /**
198
     * @return string
199
     */
200 16
    public function getHost(): ? string
201
    {
202 16
        return $this->host;
203
    }
204
205
    /**
206
     * @return string
207
     */
208 19
    public function getLink() : ? string
209
    {
210 19
        return $this->link;
211
    }
212
213
    /**
214
     * @param  string $link
215
     * @return NodeInterface
216
     */
217 27
    public function setLink(string $link = null) : NodeInterface
218
    {
219 27
        $this->link = $link;
220 27
        $this->setHost($link);
221
222 27
        return $this;
223
    }
224
225
    /**
226
     * @param string|null $link
227
     */
228 27
    protected function setHost(string $link = null): void
229
    {
230 27
        if (!is_null($link)) {
231 27
            $this->host = '//' . parse_url($link, PHP_URL_HOST);
232
        }
233 27
    }
234
235
    /**
236
     * @param string $name element name
237
     * @return null|string
238
     */
239 4
    public function getValue(string $name) : ? string
240
    {
241 4
        foreach ($this->getElementIterator($name) as $element) {
242 4
            return $element->getValue();
243
        }
244
245 1
        return null;
246
    }
247
248
    /**
249
     * @return array
250
     */
251 4
    public function toArray() : array
252
    {
253 4
        $properties = get_object_vars($this);
254
        $properties['elements'] = iterator_to_array($this->getElementsGenerator());
255 4
        $properties['categories'] = iterator_to_array($this->getCategoriesGenerator());
256 4
257 3
        foreach ($properties as $name => $property) {
258
            if ($property instanceof \DateTime) {
259
                $properties[$name] = $property->format(\DateTime::ATOM);
260
            } elseif ($property instanceof \ArrayIterator) {
261 4
                $properties[$name] = [];
262 4
                foreach ($property as $entry) {
263
                    if ($entry instanceof ArrayableInterface) {
264 4
                        $entry = $entry->toArray();
265
                    }
266
                    $properties[$name] []= $entry;
267
                }
268
            } elseif ($property instanceof ArrayableInterface) {
269
                $properties[$name] = $property->toArray();
270
            }
271
        }
272
273
        return $properties;
274
    }
275
}
276