Completed
Push — master ( 2abc3b...5aa123 )
by Alexis
16:40
created

Item::setChidlren()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
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
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->chidlren = new ArrayCollection();
0 ignored issues
show
Bug introduced by
The property chidlren does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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 chidlren of Item
142
     *
143
     * @return null\ArrayCollection (Item)
144
     */
145
    public function getChidlren()
146
    {
147
        return $this->children;
148
    }
149
150
    /**
151
     * Set Item from ArrayCollection
152
     *
153
     * @param null\ArrayCollection
154
     *
155
     * @return self
156
     */
157
    public function addChildren(ArrayCollection $collection = null)
158
    {
159
        foreach ($collection as $item) {
0 ignored issues
show
Bug introduced by
The expression $collection of type null|object<Doctrine\Com...ctions\ArrayCollection> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
160
            if($this->chidlren->contains($item) === false) {
161
                $this->setChidlren($item);
162
            }
163
        }
164
165
        return $this;
166
    }
167
168
    /**
169
     * Set chidlren of Item
170
     *
171
     * @param Item      $item
172
     *
173
     * @return self
174
     */
175
    public function setChidlren(ItemInterface $item)
176
    {
177
        $this->chidlren->add($item);
178
179
        return $this;
180
    }
181
182
    /**
183
     * Get name displayed in Item
184
     *
185
     * @return string
186
     */
187
    public function getName()
188
    {
189
        return $this->name;
190
    }
191
192
    /**
193
     * Set name displayed in Item
194
     *
195
     * @param string
196
     *
197
     * @return self
198
     */
199
    public function setName($name)
200
    {
201
        $this->name = $name;
202
203
        return $this;
204
    }
205
206
    /**
207
     * Get URL
208
     *
209
     * @return string
210
     */
211
    public function getUri()
212
    {
213
        return $this->uri;
214
    }
215
216
    /**
217
     * Set URL
218
     *
219
     * @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...
220
     *
221
     * @return self
222
     */
223
    public function setUri($uri)
224
    {
225
        $this->uri = $uri;
226
227
        return $this;
228
    }
229
230
    /**
231
     * Get position of Item
232
     *
233
     * @return integer
234
     */
235
    public function getPosition()
236
    {
237
        return $this->position;
238
    }
239
240
241
    /**
242
     * Set position
243
     *
244
     * @param  int    $position
245
     *
246
     * @return self
247
     */
248
    public function setPosition($position)
249
    {
250
        $this->position = $position;
251
252
        return $this;
253
    }
254
}
255