Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

src/Kunstmaan/NodeBundle/Helper/NodeMenuItem.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\NodeBundle\Helper;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\NodeBundle\Entity\HasNodeInterface;
7
use Kunstmaan\NodeBundle\Entity\Node;
8
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
9
10
/**
11
 * NodeMenuItem
12
 */
13
class NodeMenuItem
14
{
15
    /**
16
     * @var EntityManager
17
     */
18
    private $em;
19
20
    /**
21
     * @var Node
22
     */
23
    private $node;
24
25
    /**
26
     * @var NodeTranslation
27
     */
28
    private $nodeTranslation;
29
30
    /**
31
     * @var NodeMenuItem[]
32
     */
33
    private $children = null;
34
35
    /**
36
     * @var NodeMenuItem
37
     */
38
    private $parent;
39
40
    /**
41
     * @var NodeMenu
42
     */
43
    private $menu;
44
45
    /**
46
     * @param Node                    $node            The node
47
     * @param NodeTranslation         $nodeTranslation The nodetranslation
48
     * @param NodeMenuItem|null|false $parent          The parent nodemenuitem
49
     * @param NodeMenu                $menu            The menu
50
     */
51
    public function __construct(Node $node, NodeTranslation $nodeTranslation, $parent = false, NodeMenu $menu)
52
    {
53
        $this->node = $node;
54
        $this->nodeTranslation = $nodeTranslation;
55
        // false = look up parent later if required (default); null = top menu item; NodeMenuItem = parent item already fetched
56
        $this->parent = $parent;
0 ignored issues
show
Documentation Bug introduced by
It seems like $parent can also be of type false. However, the property $parent is declared as type object<Kunstmaan\NodeBundle\Helper\NodeMenuItem>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
57
        $this->menu = $menu;
58
        $this->em = $menu->getEntityManager();
59
    }
60
61
    /**
62
     * @return int
63
     */
64
    public function getId()
65
    {
66
        return $this->node->getId();
67
    }
68
69
    /**
70
     * @return Node
71
     */
72
    public function getNode()
73
    {
74
        return $this->node;
75
    }
76
77
    /**
78
     * @return NodeTranslation
79
     */
80
    public function getNodeTranslation()
81
    {
82
        return $this->nodeTranslation;
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getTitle()
89
    {
90
        $nodeTranslation = $this->getNodeTranslation();
91
        if ($nodeTranslation) {
92
            return $nodeTranslation->getTitle();
93
        }
94
95
        return 'Untranslated';
96
    }
97
98
    /**
99
     * @return bool
100
     */
101
    public function getOnline()
102
    {
103
        $nodeTranslation = $this->getNodeTranslation();
104
        if ($nodeTranslation) {
105
            return $nodeTranslation->isOnline();
106
        }
107
108
        return false;
109
    }
110
111
    /**
112
     * @return string|null
113
     */
114
    public function getSlugPart()
115
    {
116
        $nodeTranslation = $this->getNodeTranslation();
117
        if ($nodeTranslation) {
118
            return $nodeTranslation->getFullSlug();
119
        }
120
121
        return null;
122
    }
123
124
    /**
125
     * @return string
126
     */
127
    public function getSlug()
128
    {
129
        return $this->getUrl();
130
    }
131
132
    /**
133
     * @return string
134
     */
135
    public function getUrl()
136
    {
137
        $nodeTranslation = $this->getNodeTranslation();
138
        if ($nodeTranslation) {
139
            return $nodeTranslation->getUrl();
140
        }
141
142
        return null;
143
    }
144
145
    /**
146
     * @return NodeMenuItem|null
147
     */
148
    public function getParent()
149
    {
150
        if ($this->parent === false) {
151
            // We need to calculate the parent
152
            $this->parent = $this->menu->getParent($this->node);
153
        }
154
155
        return $this->parent;
156
    }
157
158
    /**
159
     * @param NodeMenuItem|null|false $parent
160
     */
161
    public function setParent($parent = false)
162
    {
163
        $this->parent = $parent;
164
    }
165
166
    /**
167
     * @param string $class
168
     *
169
     * @return NodeMenuItem|null
170
     */
171
    public function getParentOfClass($class)
172
    {
173
        // Check for namespace alias
174 View Code Duplication
        if (strpos($class, ':') !== false) {
175
            list($namespaceAlias, $simpleClassName) = explode(':', $class);
176
            $class = $this->em->getConfiguration()->getEntityNamespace($namespaceAlias) . '\\' . $simpleClassName;
177
        }
178
        if ($this->getParent() === null) {
179
            return null;
180
        }
181
        if ($this->parent->getPage() instanceof $class) {
182
            return $this->parent;
183
        }
184
185
        return $this->parent->getParentOfClass($class);
186
    }
187
188
    /**
189
     * @return NodeMenuItem[]
190
     */
191 View Code Duplication
    public function getParents()
192
    {
193
        $parent = $this->getParent();
194
        $parents = array();
195
        while ($parent !== null) {
196
            $parents[] = $parent;
197
            $parent = $parent->getParent();
198
        }
199
200
        return array_reverse($parents);
201
    }
202
203
    /**
204
     * @param bool $includeHiddenFromNav Include hiddenFromNav nodes
205
     *
206
     * @return NodeMenuItem[]
207
     */
208
    public function getChildren($includeHiddenFromNav = true)
209
    {
210
        if (is_null($this->children)) {
211
            $children = $this->menu->getChildren($this->node, true);
212
            /* @var NodeMenuItem $child */
213
            foreach ($children as $child) {
214
                $child->setParent($this);
215
            }
216
            $this->children = $children;
217
        }
218
219 View Code Duplication
        $children = array_filter($this->children, function (NodeMenuItem $entry) use ($includeHiddenFromNav) {
220
            if ($entry->getNode()->isHiddenFromNav() && !$includeHiddenFromNav) {
221
                return false;
222
            }
223
224
            return true;
225
        });
226
227
        return $children;
228
    }
229
230
    /**
231
     * @param string $class
232
     *
233
     * @return NodeMenuItem[]
234
     */
235
    public function getChildrenOfClass($class)
236
    {
237
        // Check for namespace alias
238 View Code Duplication
        if (strpos($class, ':') !== false) {
239
            list($namespaceAlias, $simpleClassName) = explode(':', $class);
240
            $class = $this->em->getConfiguration()->getEntityNamespace($namespaceAlias) . '\\' . $simpleClassName;
241
        }
242
        $result = array();
243
        $children = $this->getChildren();
244
        foreach ($children as $child) {
245
            if ($child->getPage() instanceof $class) {
246
                $result[] = $child;
247
            }
248
        }
249
250
        return $result;
251
    }
252
253
    /**
254
     * Get the first child of class, this is not using the getChildrenOfClass method for performance reasons
255
     *
256
     * @param string $class
257
     *
258
     * @return NodeMenuItem
259
     */
260
    public function getChildOfClass($class)
261
    {
262
        // Check for namespace alias
263 View Code Duplication
        if (strpos($class, ':') !== false) {
264
            list($namespaceAlias, $simpleClassName) = explode(':', $class);
265
            $class = $this->em->getConfiguration()->getEntityNamespace($namespaceAlias) . '\\' . $simpleClassName;
266
        }
267
        foreach ($this->getChildren() as $child) {
268
            if ($child->getPage() instanceof $class) {
269
                return $child;
270
            }
271
        }
272
273
        return null;
274
    }
275
276
    /**
277
     * @return HasNodeInterface
278
     */
279
    public function getPage()
280
    {
281
        return $this->getNodeTranslation()->getPublicNodeVersion()->getRef($this->em);
282
    }
283
284
    /**
285
     * @return bool
286
     */
287
    public function getActive()
288
    {
289
        return $this->menu->getActive($this->getSlug());
290
    }
291
}
292