Completed
Push — master ( e0fdb0...97fd6f )
by Jeroen
24:29 queued 10:28
created

NodeMenuItem::getLang()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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;
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 1
    public function __construct(Node $node, NodeTranslation $nodeTranslation, $parent = false, NodeMenu $menu)
52
    {
53 1
        $this->node = $node;
54 1
        $this->nodeTranslation = $nodeTranslation;
55
        // false = look up parent later if required (default); null = top menu item; NodeMenuItem = parent item already fetched
56 1
        $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 1
        $this->menu = $menu;
58 1
        $this->em = $menu->getEntityManager();
59 1
    }
60
61
    /**
62
     * @return int
63
     */
64
    public function getId()
65
    {
66
        return $this->node->getId();
67
    }
68
69
    /**
70
     * @return Node
71
     */
72 1
    public function getNode()
73
    {
74 1
        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
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
126
     */
127
    public function getSlug()
128
    {
129
        return $this->getUrl();
130
    }
131
132
    /**
133
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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
0 ignored issues
show
Documentation introduced by
Should the return type not be Node|false|NodeMenuItem?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->menu->getParent($this->node) of type object<Kunstmaan\NodeBundle\Entity\Node> or false is incompatible with the declared type object<Kunstmaan\NodeBundle\Helper\NodeMenuItem> of property $parent.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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;
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...
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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
        return array_filter($this->children, function (NodeMenuItem $entry) use ($includeHiddenFromNav) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
220
            if ($entry->getNode()->isHiddenFromNav() && !$includeHiddenFromNav) {
221
                return false;
222
            }
223
224
            return true;
225
        });
226
    }
227
228
    /**
229
     * @param string $class
230
     *
231
     * @return NodeMenuItem[]
232
     */
233
    public function getChildrenOfClass($class)
234
    {
235
        // Check for namespace alias
236 View Code Duplication
        if (strpos($class, ':') !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
237
            list($namespaceAlias, $simpleClassName) = explode(':', $class);
238
            $class = $this->em->getConfiguration()->getEntityNamespace($namespaceAlias) . '\\' . $simpleClassName;
239
        }
240
        $result = array();
241
        $children = $this->getChildren();
242
        foreach ($children as $child) {
243
            if ($child->getPage() instanceof $class) {
244
                $result[] = $child;
245
            }
246
        }
247
248
        return $result;
249
    }
250
251
    /**
252
     * Get the first child of class, this is not using the getChildrenOfClass method for performance reasons
253
     *
254
     * @param string $class
255
     *
256
     * @return NodeMenuItem
0 ignored issues
show
Documentation introduced by
Should the return type not be NodeMenuItem|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
257
     */
258
    public function getChildOfClass($class)
259
    {
260
        // Check for namespace alias
261 View Code Duplication
        if (strpos($class, ':') !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
262
            list($namespaceAlias, $simpleClassName) = explode(':', $class);
263
            $class = $this->em->getConfiguration()->getEntityNamespace($namespaceAlias) . '\\' . $simpleClassName;
264
        }
265
        foreach ($this->getChildren() as $child) {
266
            if ($child->getPage() instanceof $class) {
267
                return $child;
268
            }
269
        }
270
271
        return null;
272
    }
273
274
    /**
275
     * @return HasNodeInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be HasNodeInterface|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
276
     */
277
    public function getPage()
278
    {
279
        return $this->getNodeTranslation()->getPublicNodeVersion()->getRef($this->em);
280
    }
281
282
    /**
283
     * @return bool
284
     */
285
    public function getActive()
286
    {
287
        return $this->menu->getActive($this->getSlug());
288
    }
289
290
    /**
291
     * @return string
292
     */
293
    public function getLang()
294
    {
295
        return $this->menu->getLocale();
296
    }
297
}
298