Completed
Push — master ( 5fed62...2a4737 )
by Alexis
17:13
created

Item::getUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
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_item")
13
* @ORM\Entity
14
*/
15
class Item implements ItemInterface
16
{
17
    /**
18
     * @var integer
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")
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
    public function __construct()
67
    {
68
        $this->children = new ArrayCollection();
69
    }
70
71
    /**
72
     * Get string defined
73
     *
74
     * @return string
75
     */
76
    public function __toString()
77
    {
78
        return $this->name;
79
    }
80
81
    /**
82
     * Get Id
83
     *
84
     * @return integer
85
     */
86
    public function getId()
87
    {
88
        return $this->id;
89
    }
90
91
    /**
92
     * Get menu
93
     *
94
     * @return Menu
95
     */
96
    public function getMenu()
97
    {
98
        return $this->menu;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->menu; (Alpixel\Bundle\MenuBundle\Model\MenuInterface) is incompatible with the return type declared by the interface Alpixel\Bundle\MenuBundl...\ItemInterface::getMenu of type Alpixel\Bundle\MenuBundle\Model\Menu.

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...
99
    }
100
101
    /**
102
     * Set menu
103
     *
104
     * @param Menu $menu
105
     *
106
     * @return self
107
     */
108
    public function setMenu(MenuInterface $menu)
109
    {
110
        $this->menu = $menu;
111
112
        return $this;
113
    }
114
115
    /**
116
     * Get parent Item
117
     *
118
     * @return null\Item
119
     */
120
    public function getParent()
121
    {
122
        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 122 which is incompatible with the return type declared by the interface Alpixel\Bundle\MenuBundl...temInterface::getParent of type Alpixel\Bundle\MenuBundle\Model\null\Item.
Loading history...
123
    }
124
125
    /**
126
     * Set parent Item
127
     *
128
     * @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...
129
     *
130
     * @return self
131
     */
132
    public function setParent(ItemInterface $item = null)
133
    {
134
        $this->parent = $item;
135
136
        return $this;
137
    }
138
139
140
    /**
141
     * Get children of Item
142
     *
143
     * @return ArrayCollection (Item)
144
     */
145
    public function getChildren()
146
    {
147
        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...
148
    }
149
150
    /**
151
     * Set Item from ArrayCollection
152
     *
153
     * @param null\ArrayCollection
154
     *
155
     * @return self
156
     */
157
    public function addChildren(ArrayCollection $collection)
158
    {
159
        foreach ($collection as $item) {
160
            $this->setChildren($item);
161
        }
162
163
        return $this;
164
    }
165
166
    /**
167
     * Set children of Item
168
     *
169
     * @param Item      $item
170
     *
171
     * @return self
172
     */
173
    public function setChildren(ItemInterface $item)
174
    {
175
        if ($this->children->contains($item) === false) {
176
            $this->children->add($item);
177
        }
178
179
        return $this;
180
    }
181
182
    /**
183
     * Remove children
184
     *
185
     * @param ItemInterface $item
186
     *
187
     * @return $this
188
     */
189
    public function removeChildren(ItemInterface $item)
190
    {
191
        if ($this->children->contains($item) === true) {
192
            $this->children->removeElement($item);
193
        }
194
195
        return $this;
196
    }
197
198
    /**
199
     * Get name displayed in Item
200
     *
201
     * @return string
202
     */
203
    public function getName()
204
    {
205
        return $this->name;
206
    }
207
208
    /**
209
     * Set name displayed in Item
210
     *
211
     * @param string
212
     *
213
     * @return self
214
     */
215
    public function setName($name)
216
    {
217
        $this->name = $name;
218
219
        return $this;
220
    }
221
222
    /**
223
     * Get URL
224
     *
225
     * @return string
226
     */
227
    public function getUri()
228
    {
229
        return $this->uri;
230
    }
231
232
    /**
233
     * Set URL
234
     *
235
     * @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...
236
     *
237
     * @return self
238
     */
239
    public function setUri($uri)
240
    {
241
        $this->uri = $uri;
242
243
        return $this;
244
    }
245
246
    /**
247
     * Get position of Item
248
     *
249
     * @return integer
250
     */
251
    public function getPosition()
252
    {
253
        return $this->position;
254
    }
255
256
257
    /**
258
     * Set position
259
     *
260
     * @param  int    $position
261
     *
262
     * @return self
263
     */
264
    public function setPosition($position)
265
    {
266
        $this->position = $position;
267
268
        return $this;
269
    }
270
}
271