Completed
Push — master ( 2abc3b...5aa123 )
by Alexis
16:40
created

Menu   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 16
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 188
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __toString() 0 4 1
A getId() 0 4 1
A getMachineName() 0 4 1
A setMachineName() 0 6 1
A getName() 0 4 1
A setName() 0 6 1
A getItems() 0 4 1
A removeItem() 0 8 2
A addItems() 0 10 3
A setItem() 0 6 1
A getLocale() 0 4 1
A setLocale() 0 6 1
1
<?php
2
3
namespace Alpixel\Bundle\MenuBundle\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Alpixel\Bundle\MenuBundle\Model\MenuInterface;
8
use Alpixel\Bundle\MenuBundle\Model\ItemInterface;
9
10
/**
11
* @ORM\Table(name="alpixel_menu")
12
* @ORM\Entity(repositoryClass="Alpixel\Bundle\MenuBundle\Entity\Repository\MenuRepository")
13
*/
14
class Menu implements MenuInterface
15
{
16
    /**
17
     * @var integer
18
     *
19
     * @ORM\Column(name="menu_id", type="integer", nullable=false)
20
     * @ORM\Id
21
     * @ORM\GeneratedValue(strategy="IDENTITY")
22
     */
23
    protected $id;
24
25
    /**
26
     * @ORM\OneToMany(targetEntity="Alpixel\Bundle\MenuBundle\Entity\Item", mappedBy="menu")
27
     */
28
    protected $items;
29
30
    /**
31
     * @var string
32
     *
33
     * @ORM\Column(name="machine_name", type="string", length=255, nullable=false)
34
     */
35
    protected $machineName;
36
37
    /**
38
     * @var string
39
     *
40
     * @ORM\Column(name="name", type="string", length=255, nullable=false)
41
     */
42
    protected $name;
43
44
    /**
45
     * @var string
46
     *
47
     * @ORM\Column(name="locale", type="string", length=10, nullable=false)
48
     */
49
    protected $locale;
50
51
    public function __construct()
52
    {
53
        $this->items = new ArrayCollection();
54
    }
55
56
    /**
57
     * Get string defined
58
     *
59
     * @return string
60
     */
61
    public function __toString()
62
    {
63
        return $this->machineName;
64
    }
65
66
    /**
67
     * Get Id
68
     *
69
     * @return integer
70
     */
71
    public function getId()
72
    {
73
        return $this->id;
74
    }
75
76
    /**
77
     * Get the machineName the key for querying a menu.
78
     *
79
     * @return string
80
     */
81
    public function getMachineName()
82
    {
83
        return $this->machineName;
84
    }
85
86
    /**
87
     * Set the machineName the key for querying a menu.
88
     *
89
     * @param string
90
     *
91
     * @return self
92
     */
93
    public function setMachineName($machineName)
94
    {
95
        $this->machineName = $machineName;
96
97
        return $this;
98
    }
99
100
    /**
101
     * Get the name the value displayed to the administrator
102
     *
103
     * @return self
104
     */
105
    public function getName()
106
    {
107
        return $this->name;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->name; (string) is incompatible with the return type declared by the interface Alpixel\Bundle\MenuBundl...\MenuInterface::getName of type Alpixel\Bundle\MenuBundle\Model\MenuInterface.

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...
108
    }
109
110
    /**
111
     * Set the name the value displayed to the administrator
112
     *
113
     * @return self
114
     */
115
    public function setName($name)
116
    {
117
        $this->name = $name;
118
119
        return $this;
120
    }
121
122
    /**
123
     * Get the items
124
     *
125
     * @return ArrayCollection of Item object
126
     */
127
    public function getItems()
128
    {
129
        return $this->items;
130
    }
131
132
    /**
133
     * Remove Item obejct from ArrayCollection items
134
     *
135
     * @param  ItemInterface $item
136
     *
137
     * @return self
138
     */
139
    public function removeItem(ItemInterface $item)
140
    {
141
        if($this->items->contains($item) === true) {
142
            $this->items->remove($item);
143
        }
144
145
        return $this;
146
    }
147
148
    /**
149
     * Add Items in items ArrayCollection
150
     *
151
     * @param ArrayCollection of Item object $items
152
     *
153
     * @return self
154
     */
155
    public function addItems(ArrayCollection $items)
156
    {
157
        foreach ($items as $item) {
158
            if($this->items->contains($item) === false) {
159
                $this->setItem($item);
160
            }
161
        }
162
163
        return $this;
164
    }
165
166
    /**
167
     * Set items for the menu
168
     *
169
     * @param null\ItemInterface $item
170
     *
171
     * @return Item
172
     */
173
    public function setItem(ItemInterface $item)
174
    {
175
        $this->items->add($item);
176
177
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Alpixel\Bundle\MenuBundle\Entity\Menu) is incompatible with the return type declared by the interface Alpixel\Bundle\MenuBundl...\MenuInterface::setItem of type Alpixel\Bundle\MenuBundle\Model\Item.

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...
178
    }
179
180
    /**
181
     * Get the locale language
182
     *
183
     * @return string
184
     */
185
    public function getLocale()
186
    {
187
        return $this->locale;
188
    }
189
190
    /**
191
     * Set the locale language
192
     *
193
     * @return self
194
     */
195
    public function setLocale($locale)
196
    {
197
        $this->locale = $locale;
198
199
        return $this;
200
    }
201
}
202