Item   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 306
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 1
dl 0
loc 306
c 0
b 0
f 0
rs 10

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __toString() 0 4 1
A getId() 0 4 1
A getMenu() 0 4 1
A setMenu() 0 6 1
A getParent() 0 4 1
A setParent() 0 6 1
A getChildren() 0 6 1
A addChildren() 0 8 2
A setChildren() 0 8 2
A removeChildren() 0 8 2
A getName() 0 4 1
A setName() 0 6 1
A getUri() 0 4 1
A setUri() 0 6 1
A getPosition() 0 4 1
A setPosition() 0 6 1
A getSlug() 0 4 1
A setSlug() 0 6 1
A getUriType() 0 8 1
A uriTypeExists() 0 4 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 Alpixel\Bundle\MenuBundle\Validator\Constraints\RouteExists;
8
use Doctrine\Common\Collections\ArrayCollection;
9
use Doctrine\ORM\Mapping as ORM;
10
use Gedmo\Mapping\Annotation as Gedmo;
11
12
/**
13
 * @ORM\Table(name="alpixel_menu_item")
14
 * @ORM\Entity(repositoryClass="Alpixel\Bundle\MenuBundle\Entity\Repository\ItemRepository")
15
 */
16
class Item implements ItemInterface
17
{
18
    const URI_TYPE_ANCHOR = 'anchor';
19
    const URI_TYPE_EXTERNAL = 'external';
20
    const URI_TYPE_INTERNAL = 'internal';
21
22
    /**
23
     * @var int
24
     *
25
     * @ORM\Column(name="item_id", type="integer", nullable=false)
26
     * @ORM\Id
27
     * @ORM\GeneratedValue(strategy="IDENTITY")
28
     */
29
    protected $id;
30
31
    /**
32
     * @Gedmo\SortableGroup
33
     *
34
     * @ORM\ManyToOne(targetEntity="Alpixel\Bundle\MenuBundle\Entity\Item", inversedBy="children")
35
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="item_id", onDelete="SET NULL")
36
     */
37
    protected $parent;
38
39
    /**
40
     * @ORM\OneToMany(targetEntity="Alpixel\Bundle\MenuBundle\Entity\Item", mappedBy="parent")
41
     * @ORM\OrderBy({"position": "ASC"})
42
     */
43
    protected $children;
44
45
    /**
46
     * @Gedmo\SortableGroup
47
     *
48
     * @ORM\ManyToOne(targetEntity="Alpixel\Bundle\MenuBundle\Entity\Menu", inversedBy="items")
49
     * @ORM\JoinColumn(name="menu_id", referencedColumnName="menu_id")
50
     */
51
    protected $menu;
52
53
    /**
54
     * @var string
55
     *
56
     * @ORM\Column(name="name", type="string", length=255, nullable=false)
57
     */
58
    protected $name;
59
60
    /**
61
     * @RouteExists
62
     * @ORM\Column(name="uri", type="text", nullable=false)
63
     */
64
    protected $uri;
65
66
    /**
67
     * @Gedmo\SortablePosition
68
     *
69
     * @ORM\Column(name="position", type="integer", nullable=false)
70
     */
71
    protected $position;
72
73
    /**
74
     * @Gedmo\Slug(fields={"name"}, updatable=true, separator="_")
75
     * @ORM\Column(length=128)
76
     **/
77
    protected $slug;
78
79
    public function __construct()
80
    {
81
        $this->children = new ArrayCollection();
82
    }
83
84
    /**
85
     * Get string defined.
86
     *
87
     * @return string
88
     */
89
    public function __toString()
90
    {
91
        return (string) $this->name;
92
    }
93
94
    /**
95
     * Get Id.
96
     *
97
     * @return int
98
     */
99
    public function getId()
100
    {
101
        return $this->id;
102
    }
103
104
    /**
105
     * Get menu.
106
     *
107
     * @return Menu
108
     */
109
    public function getMenu()
110
    {
111
        return $this->menu;
112
    }
113
114
    /**
115
     * Set menu.
116
     *
117
     * @param Menu $menu
118
     *
119
     * @return self
120
     */
121
    public function setMenu(MenuInterface $menu)
122
    {
123
        $this->menu = $menu;
124
125
        return $this;
126
    }
127
128
    /**
129
     * Get parent Item.
130
     *
131
     * @return null\Item
132
     */
133
    public function getParent()
134
    {
135
        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 135 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...
136
    }
137
138
    /**
139
     * Set parent Item.
140
     *
141
     * @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...
142
     *
143
     * @return self
144
     */
145
    public function setParent(ItemInterface $item = null)
146
    {
147
        $this->parent = $item;
148
149
        return $this;
150
    }
151
152
    /**
153
     * Get children of Item.
154
     *
155
     * @return ArrayCollection (Item)
156
     */
157
    public function getChildren()
158
    {
159
        $children = $this->children;
160
161
        return $children;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $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...
162
    }
163
164
    /**
165
     * Set Item from ArrayCollection.
166
     *
167
     * @param null\ArrayCollection
168
     *
169
     * @return self
170
     */
171
    public function addChildren(ArrayCollection $collection)
172
    {
173
        foreach ($collection as $item) {
174
            $this->setChildren($item);
175
        }
176
177
        return $this;
178
    }
179
180
    /**
181
     * Set children of Item.
182
     *
183
     * @param Item $item
184
     *
185
     * @return self
186
     */
187
    public function setChildren(ItemInterface $item)
188
    {
189
        if ($this->children->contains($item) === false) {
190
            $this->children->add($item);
191
        }
192
193
        return $this;
194
    }
195
196
    /**
197
     * Remove children.
198
     *
199
     * @param ItemInterface $item
200
     *
201
     * @return $this
202
     */
203
    public function removeChildren(ItemInterface $item)
204
    {
205
        if ($this->children->contains($item) === true) {
206
            $this->children->removeElement($item);
207
        }
208
209
        return $this;
210
    }
211
212
    /**
213
     * Get name displayed in Item.
214
     *
215
     * @return string
216
     */
217
    public function getName()
218
    {
219
        return $this->name;
220
    }
221
222
    /**
223
     * Set name displayed in Item.
224
     *
225
     * @param string
226
     *
227
     * @return self
228
     */
229
    public function setName($name)
230
    {
231
        $this->name = $name;
232
233
        return $this;
234
    }
235
236
    /**
237
     * Get URL.
238
     *
239
     * @return string
240
     */
241
    public function getUri()
242
    {
243
        return $this->uri;
244
    }
245
246
    /**
247
     * Set URL.
248
     *
249
     * @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...
250
     *
251
     * @return self
252
     */
253
    public function setUri($uri)
254
    {
255
        $this->uri = $uri;
256
257
        return $this;
258
    }
259
260
    /**
261
     * Get position of Item.
262
     *
263
     * @return int
264
     */
265
    public function getPosition()
266
    {
267
        return $this->position;
268
    }
269
270
    /**
271
     * Set position.
272
     *
273
     * @param int $position
274
     *
275
     * @return self
276
     */
277
    public function setPosition($position)
278
    {
279
        $this->position = $position;
280
281
        return $this;
282
    }
283
284
    /**
285
     * Gets the value of slug.
286
     *
287
     * @return mixed
288
     */
289
    public function getSlug()
290
    {
291
        return $this->slug;
292
    }
293
294
    /**
295
     * Sets the value of slug.
296
     *
297
     * @param mixed $slug the slug
298
     *
299
     * @return self
300
     */
301
    public function setSlug($slug)
302
    {
303
        $this->slug = $slug;
304
305
        return $this;
306
    }
307
308
    public static function getUriType()
309
    {
310
        return [
311
            self::URI_TYPE_ANCHOR,
312
            self::URI_TYPE_EXTERNAL,
313
            self::URI_TYPE_INTERNAL,
314
        ];
315
    }
316
317
    public static function uriTypeExists($type)
318
    {
319
        return in_array($type, self::getUriType());
320
    }
321
}
322