Menu::getItems()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Alpixel\Bundle\MenuBundle\Entity;
4
5
use Alpixel\Bundle\MenuBundle\Model\ItemInterface;
6
use Alpixel\Bundle\MenuBundle\Model\MenuInterface;
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\ORM\Mapping as ORM;
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 int
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", fetch="EAGER")
27
     * @ORM\OrderBy({"position": "ASC"})
28
     */
29
    protected $items;
30
31
    /**
32
     * @var string
33
     *
34
     * @ORM\Column(name="machine_name", type="string", length=255, nullable=false)
35
     */
36
    protected $machineName;
37
38
    /**
39
     * @var string
40
     *
41
     * @ORM\Column(name="name", type="string", length=255, nullable=false)
42
     */
43
    protected $name;
44
45
    /**
46
     * @var string
47
     *
48
     * @ORM\Column(name="locale", type="string", length=10, nullable=false)
49
     */
50
    protected $locale;
51
52
    public function __construct()
53
    {
54
        $this->items = new ArrayCollection();
55
    }
56
57
    /**
58
     * Get string defined.
59
     *
60
     * @return string
61
     */
62
    public function __toString()
63
    {
64
        return $this->machineName;
65
    }
66
67
    /**
68
     * Get Id.
69
     *
70
     * @return int
71
     */
72
    public function getId()
73
    {
74
        return $this->id;
75
    }
76
77
    /**
78
     * Get the machineName the key for querying a menu.
79
     *
80
     * @return string
81
     */
82
    public function getMachineName()
83
    {
84
        return $this->machineName;
85
    }
86
87
    /**
88
     * Set the machineName the key for querying a menu.
89
     *
90
     * @param string
91
     *
92
     * @return self
93
     */
94
    public function setMachineName($machineName)
95
    {
96
        $this->machineName = $machineName;
97
98
        return $this;
99
    }
100
101
    /**
102
     * Get the name the value displayed to the administrator.
103
     *
104
     * @return self
105
     */
106
    public function getName()
107
    {
108
        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...
109
    }
110
111
    /**
112
     * Set the name the value displayed to the administrator.
113
     *
114
     * @return self
115
     */
116
    public function setName($name)
117
    {
118
        $this->name = $name;
119
120
        return $this;
121
    }
122
123
    /**
124
     * Get the items.
125
     *
126
     * @return ArrayCollection of Item object
127
     */
128
    public function getItems()
129
    {
130
        return $this->items;
131
    }
132
133
    /**
134
     * Remove Item obejct from ArrayCollection items.
135
     *
136
     * @param ItemInterface $item
137
     *
138
     * @return self
139
     */
140
    public function removeItem(ItemInterface $item)
141
    {
142
        if ($this->items->contains($item) === true) {
143
            $this->items->removeElement($item);
144
        }
145
146
        return $this;
147
    }
148
149
    /**
150
     * Add Items in items ArrayCollection.
151
     *
152
     * @param ArrayCollection of Item object $items
153
     *
154
     * @return self
155
     */
156
    public function addItems(ArrayCollection $items)
157
    {
158
        foreach ($items as $item) {
159
            if ($this->items->contains($item) === false) {
160
                $this->setItem($item);
0 ignored issues
show
Deprecated Code introduced by
The method Alpixel\Bundle\MenuBundle\Entity\Menu::setItem() has been deprecated.

This method has been deprecated.

Loading history...
161
            }
162
        }
163
164
        return $this;
165
    }
166
167
    /**
168
     * Set items for the menu.
169
     *
170
     * @deprecated
171
     *
172
     * @param null\ItemInterface $item
173
     *
174
     * @return Item
175
     */
176
    public function setItem(ItemInterface $item)
177
    {
178
        return $this->addItem($item);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->addItem($item); (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\ItemInterface.

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...
179
    }
180
181
    /**
182
     * Set items for the menu.
183
     *
184
     * @param null\ItemInterface $item
185
     *
186
     * @return Item
187
     */
188
    public function addItem(ItemInterface $item)
189
    {
190
        $item->setMenu($this);
191
        $this->items->add($item);
192
193
        return $this;
194
    }
195
196
    /**
197
     * Get the locale language.
198
     *
199
     * @return string
200
     */
201
    public function getLocale()
202
    {
203
        return $this->locale;
204
    }
205
206
    /**
207
     * Set the locale language.
208
     *
209
     * @return self
210
     */
211
    public function setLocale($locale)
212
    {
213
        $this->locale = $locale;
214
215
        return $this;
216
    }
217
}
218