Element   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 95
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getName() 0 4 1
A setName() 0 6 1
A getValue() 0 4 1
A setValue() 0 6 1
A getAttribute() 0 8 2
A getAttributes() 0 4 1
A setAttribute() 0 6 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\Node;
12
13
use FeedIo\Feed\ElementsAwareInterface;
14
use FeedIo\Feed\ElementsAwareTrait;
15
16
class Element implements ElementInterface, ElementsAwareInterface
17
{
18
    use ElementsAwareTrait;
19
20
    /**
21
     * @var string
22
     */
23
    protected $name;
24
25
    /**
26
     * @var string
27
     */
28
    protected $value;
29
30
    /**
31
     * @var array
32
     */
33
    protected $attributes = array();
34
35 37
    public function __construct()
36
    {
37 37
        $this->initElements();
38 37
    }
39
40
    /**
41
     * @return string
42
     */
43 24
    public function getName() : string
44
    {
45 24
        return $this->name;
46
    }
47
48
    /**
49
     * @param  string $name
50
     * @return ElementInterface
51
     */
52 33
    public function setName(string $name) : ElementInterface
53
    {
54 33
        $this->name = $name;
55
56 33
        return $this;
57
    }
58
59
    /**
60
     * @return string
61
     */
62 15
    public function getValue() : ? string
63
    {
64 15
        return $this->value;
65
    }
66
67
    /**
68
     * @param  string $value
69
     * @return ElementInterface
70
     */
71 28
    public function setValue(string $value = null) : ElementInterface
72
    {
73 28
        $this->value = $value;
74
75 28
        return $this;
76
    }
77
78
    /**
79
     * @param string $name
80
     * @return null|string
81
     */
82 3
    public function getAttribute(string $name) : ? string
83
    {
84 3
        if (array_key_exists($name, $this->attributes)) {
85 2
            return $this->attributes[$name];
86
        }
87
88 1
        return null;
89
    }
90
91
    /**
92
     * @return iterable
93
     */
94 7
    public function getAttributes() : iterable
95
    {
96 7
        return $this->attributes;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->attributes; (array) is incompatible with the return type declared by the interface FeedIo\Feed\Node\ElementInterface::getAttributes of type FeedIo\Feed\Node\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...
97
    }
98
99
    /**
100
     * @param  string $name
101
     * @param  string $value
102
     * @return ElementInterface
103
     */
104 5
    public function setAttribute(string $name, string $value = null) : ElementInterface
105
    {
106 5
        $this->attributes[$name] = $value;
107
108 5
        return $this;
109
    }
110
}
111