Completed
Push — master ( 467065...02de5b )
by Benjamin
02:38
created

Item::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
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
use Gedmo\Mapping\Annotation as Gedmo;
10
11
/**
12
 * @ORM\Table(name="alpixel_menu_item")
13
 * @ORM\Entity
14
 */
15
class Item implements ItemInterface
16
{
17
    /**
18
     * @var int
19
     *
20
     * @ORM\Column(name="item_id", type="integer", nullable=false)
21
     * @ORM\Id
22
     * @ORM\GeneratedValue(strategy="IDENTITY")
23
     */
24
    protected $id;
25
26
    /**
27
     * @Gedmo\SortableGroup
28
     *
29
     * @ORM\ManyToOne(targetEntity="Alpixel\Bundle\MenuBundle\Entity\Item", inversedBy="children")
30
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="item_id", onDelete="SET NULL")
31
     */
32
    protected $parent;
33
34
    /**
35
     * @ORM\OneToMany(targetEntity="Alpixel\Bundle\MenuBundle\Entity\Item", mappedBy="parent")
36
     */
37
    protected $children;
38
39
    /**
40
     * @ORM\ManyToOne(targetEntity="Alpixel\Bundle\MenuBundle\Entity\Menu", inversedBy="items")
41
     * @ORM\JoinColumn(name="menu_id", referencedColumnName="menu_id")
42
     */
43
    protected $menu;
44
45
    /**
46
     * @var string
47
     *
48
     * @ORM\Column(name="name", type="string", length=255, nullable=false)
49
     */
50
    protected $name;
51
52
    /**
53
     * @var string
54
     *
55
     * @ORM\Column(name="uri", type="text", nullable=false)
56
     */
57
    protected $uri;
58
59
    /**
60
     * @Gedmo\SortablePosition
61
     *
62
     * @ORM\Column(name="position", type="integer", nullable=false)
63
     */
64
    protected $position;
65
66
    /**
67
     * @Gedmo\Slug(fields={"name"}, updatable=true, separator="_")
68
     * @ORM\Column(length=128)
69
     **/
70
    protected $slug;
71
72
73
    public function __construct()
74
    {
75
        $this->children = new ArrayCollection();
76
    }
77
78
    /**
79
     * Get string defined.
80
     *
81
     * @return string
82
     */
83
    public function __toString()
84
    {
85
        return $this->name;
86
    }
87
88
    /**
89
     * Get Id.
90
     *
91
     * @return int
92
     */
93
    public function getId()
94
    {
95
        return $this->id;
96
    }
97
98
    /**
99
     * Get menu.
100
     *
101
     * @return Menu
102
     */
103
    public function getMenu()
104
    {
105
        return $this->menu;
106
    }
107
108
    /**
109
     * Set menu.
110
     *
111
     * @param Menu $menu
112
     *
113
     * @return self
114
     */
115
    public function setMenu(MenuInterface $menu)
116
    {
117
        $this->menu = $menu;
118
119
        return $this;
120
    }
121
122
    /**
123
     * Get parent Item.
124
     *
125
     * @return null\Item
126
     */
127
    public function getParent()
128
    {
129
        return $this->parent;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->parent; of type null|Alpixel\Bundle\MenuBundle\Model\ItemInterface adds the type Alpixel\Bundle\MenuBundle\Model\ItemInterface to the return on line 129 which is incompatible with the return type declared by the interface Alpixel\Bundle\MenuBundl...temInterface::getParent of type null|Alpixel\Bundle\MenuBundle\Model\MenuInterface.
Loading history...
130
    }
131
132
    /**
133
     * Set parent Item.
134
     *
135
     * @param ItemInterface $menu
0 ignored issues
show
Bug introduced by
There is no parameter named $menu. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
136
     *
137
     * @return self
138
     */
139
    public function setParent(ItemInterface $item = null)
140
    {
141
        $this->parent = $item;
142
143
        return $this;
144
    }
145
146
    /**
147
     * Get children of Item.
148
     *
149
     * @return ArrayCollection (Item)
150
     */
151
    public function getChildren()
152
    {
153
        return $this->children;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->children; (Doctrine\Common\Collections\ArrayCollection) is incompatible with the return type declared by the interface Alpixel\Bundle\MenuBundl...mInterface::getChildren of type Alpixel\Bundle\MenuBundl...el\null\ArrayCollection.

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...
154
    }
155
156
    /**
157
     * Set Item from ArrayCollection.
158
     *
159
     * @param null\ArrayCollection
160
     *
161
     * @return self
162
     */
163
    public function addChildren(ArrayCollection $collection)
164
    {
165
        foreach ($collection as $item) {
166
            $this->setChildren($item);
167
        }
168
169
        return $this;
170
    }
171
172
    /**
173
     * Set children of Item.
174
     *
175
     * @param Item $item
176
     *
177
     * @return self
178
     */
179
    public function setChildren(ItemInterface $item)
180
    {
181
        if ($this->children->contains($item) === false) {
182
            $this->children->add($item);
183
        }
184
185
        return $this;
186
    }
187
188
    /**
189
     * Remove children.
190
     *
191
     * @param ItemInterface $item
192
     *
193
     * @return $this
194
     */
195
    public function removeChildren(ItemInterface $item)
196
    {
197
        if ($this->children->contains($item) === true) {
198
            $this->children->removeElement($item);
199
        }
200
201
        return $this;
202
    }
203
204
    /**
205
     * Get name displayed in Item.
206
     *
207
     * @return string
208
     */
209
    public function getName()
210
    {
211
        return $this->name;
212
    }
213
214
    /**
215
     * Set name displayed in Item.
216
     *
217
     * @param string
218
     *
219
     * @return self
220
     */
221
    public function setName($name)
222
    {
223
        $this->name = $name;
224
225
        return $this;
226
    }
227
228
    /**
229
     * Get URL.
230
     *
231
     * @return string
232
     */
233
    public function getUri()
234
    {
235
        return $this->uri;
236
    }
237
238
    /**
239
     * Set URL.
240
     *
241
     * @param string $url
0 ignored issues
show
Bug introduced by
There is no parameter named $url. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
242
     *
243
     * @return self
244
     */
245
    public function setUri($uri)
246
    {
247
        $this->uri = $uri;
248
249
        return $this;
250
    }
251
252
    /**
253
     * Get position of Item.
254
     *
255
     * @return int
256
     */
257
    public function getPosition()
258
    {
259
        return $this->position;
260
    }
261
262
    /**
263
     * Set position.
264
     *
265
     * @param int $position
266
     *
267
     * @return self
268
     */
269
    public function setPosition($position)
270
    {
271
        $this->position = $position;
272
273
        return $this;
274
    }
275
276
    /**
277
     * Gets the value of slug.
278
     *
279
     * @return mixed
280
     */
281
    public function getSlug()
282
    {
283
        return $this->slug;
284
    }
285
286
    /**
287
     * Sets the value of slug.
288
     *
289
     * @param mixed $slug the slug
290
     *
291
     * @return self
292
     */
293
    public function setSlug($slug)
294
    {
295
        $this->slug = $slug;
296
297
        return $this;
298
    }
299
}
300