Completed
Push — master ( 9278f3...6933e0 )
by Benjamin
05:11 queued 02:31
created

Menu::setLocale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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")
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 int
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->removeElement($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);
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...
160
            }
161
        }
162
163
        return $this;
164
    }
165
166
167
    /**
168
     * Set items for the menu.
169
     * @deprecated
170
     * @param null\ItemInterface $item
171
     *
172
     * @return Item
173
     */
174
    public function setItem(ItemInterface $item)
175
    {
176
        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...
177
    }
178
179
    /**
180
     * Set items for the menu.
181
     *
182
     * @param null\ItemInterface $item
183
     *
184
     * @return Item
185
     */
186
    public function addItem(ItemInterface $item)
187
    {
188
        $item->setMenu($this);
189
        $this->items->add($item);
190
191
        return $this;
192
    }
193
194
    /**
195
     * Get the locale language.
196
     *
197
     * @return string
198
     */
199
    public function getLocale()
200
    {
201
        return $this->locale;
202
    }
203
204
    /**
205
     * Set the locale language.
206
     *
207
     * @return self
208
     */
209
    public function setLocale($locale)
210
    {
211
        $this->locale = $locale;
212
213
        return $this;
214
    }
215
}
216