Completed
Push — master ( 5e1e13...f15dc3 )
by Nicolas
03:21
created

MenuItem   C

Complexity

Total Complexity 67

Size/Duplication

Total Lines 605
Duplicated Lines 4.96 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 84.21%

Importance

Changes 0
Metric Value
wmc 67
c 0
b 0
f 0
lcom 1
cbo 1
dl 30
loc 605
ccs 16
cts 19
cp 0.8421
rs 5.6791

39 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setIconAttribute() 0 13 2
A make() 0 6 1
A getRandomName() 0 4 1
A fill() 0 8 3
A child() 0 6 1
A dropdown() 21 21 2
A route() 0 16 2
A url() 9 14 2
A add() 0 8 1
A addDivider() 0 8 1
A divider() 0 4 1
A addHeader() 0 11 1
A header() 0 4 1
A getChilds() 0 10 2
A getUrl() 0 4 2
A getRequest() 0 4 1
A getIcon() 0 4 2
A getProperties() 0 4 1
A getAttributes() 0 8 2
A isDivider() 0 4 1
A isHeader() 0 4 1
A is() 0 4 1
A hasSubMenu() 0 4 1
A hasChilds() 0 4 1
A hasActiveOnChild() 0 8 3
B getActiveStateFromChilds() 0 20 9
A inactive() 0 14 3
A getActiveAttribute() 0 4 1
A getInactiveAttribute() 0 4 1
B isActive() 0 22 5
A hasRoute() 0 4 1
A getActiveStateFromRoute() 0 4 1
A getActiveStateFromUrl() 0 4 1
A order() 0 6 1
A hideWhen() 0 6 1
A hidden() 0 8 2
A toArray() 0 4 1
A __get() 0 4 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like MenuItem often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use MenuItem, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Nwidart\Menus;
4
5
use Closure;
6
use Collective\Html\HtmlFacade as HTML;
7
use Illuminate\Contracts\Support\Arrayable as ArrayableContract;
8
use Illuminate\Support\Facades\Request;
9
10
class MenuItem implements ArrayableContract
11
{
12
    /**
13
     * Array properties.
14
     *
15
     * @var array
16
     */
17
    protected $properties = [];
18
19
    /**
20
     * The child collections for current menu item.
21
     *
22
     * @var array
23
     */
24
    protected $childs = array();
25
26
    /**
27
     * The fillable attribute.
28
     *
29
     * @var array
30
     */
31
    protected $fillable = array(
32
        'url',
33
        'route',
34
        'title',
35
        'name',
36
        'icon',
37
        'parent',
38
        'attributes',
39
        'active',
40
        'order',
41
        'hideWhen',
42
    );
43
44
    /**
45
     * The hideWhen callback.
46
     *
47
     * @var Closure
48
     */
49
    protected $hideWhen;
50
51
    /**
52
     * Constructor.
53
     *
54
     * @param array $properties
55
     */
56 1
    public function __construct($properties = array())
57
    {
58 1
        $this->properties = $properties;
59 1
        $this->fill($properties);
60 1
    }
61
62
    /**
63
     * Set the icon property when the icon is defined in the link attributes.
64
     *
65
     * @param array $properties
66
     *
67
     * @return array
68
     */
69 1
    protected static function setIconAttribute(array $properties)
70
    {
71 1
        $icon = array_get($properties, 'attributes.icon');
72 1
        if (!is_null($icon)) {
73
            $properties['icon'] = $icon;
74
75
            array_forget($properties, 'attributes.icon');
76
77
            return $properties;
78
        }
79
80 1
        return $properties;
81
    }
82
83
    /**
84
     * Get random name.
85
     *
86
     * @param array $attributes
87
     *
88
     * @return string
89
     */
90
    protected static function getRandomName(array $attributes)
91
    {
92
        return substr(md5(array_get($attributes, 'title', str_random(6))), 0, 5);
93
    }
94
95
    /**
96
     * Create new static instance.
97
     *
98
     * @param array $properties
99
     *
100
     * @return static
101
     */
102 1
    public static function make(array $properties)
103
    {
104 1
        $properties = self::setIconAttribute($properties);
105
106 1
        return new static($properties);
107
    }
108
109
    /**
110
     * Fill the attributes.
111
     *
112
     * @param array $attributes
113
     */
114 1
    public function fill($attributes)
115
    {
116 1
        foreach ($attributes as $key => $value) {
117 1
            if (in_array($key, $this->fillable)) {
118 1
                $this->{$key} = $value;
119
            }
120
        }
121 1
    }
122
123
    /**
124
     * Create new menu child item using array.
125
     *
126
     * @param $attributes
127
     *
128
     * @return $this
129
     */
130
    public function child($attributes)
131
    {
132
        $this->childs[] = static::make($attributes);
133
134
        return $this;
135
    }
136
137
    /**
138
     * Register new child menu with dropdown.
139
     *
140
     * @param $title
141
     * @param callable $callback
142
     *
143
     * @return $this
144
     */
145 View Code Duplication
    public function dropdown($title, \Closure $callback, $order = 0, array $attributes = array())
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...
146
    {
147
        $properties = compact('title', 'order', 'attributes');
148
149
        if (func_num_args() == 3) {
150
            $arguments = func_get_args();
151
152
            $title = array_get($arguments, 0);
153
            $attributes = array_get($arguments, 2);
154
155
            $properties = compact('title', 'attributes');
156
        }
157
158
        $child = static::make($properties);
159
160
        call_user_func($callback, $child);
161
162
        $this->childs[] = $child;
163
164
        return $child;
165
    }
166
167
    /**
168
     * Create new menu item and set the action to route.
169
     *
170
     * @param $route
171
     * @param $title
172
     * @param array $parameters
173
     * @param array $attributes
174
     *
175
     * @return array
176
     */
177
    public function route($route, $title, $parameters = array(), $order = 0, $attributes = array())
178
    {
179
        if (func_num_args() == 4) {
180
            $arguments = func_get_args();
181
182
            return $this->add([
183
                'route' => [array_get($arguments, 0), array_get($arguments, 2)],
184
                'title' => array_get($arguments, 1),
185
                'attributes' => array_get($arguments, 3),
186
            ]);
187
        }
188
189
        $route = array($route, $parameters);
190
191
        return $this->add(compact('route', 'title', 'order', 'attributes'));
192
    }
193
194
    /**
195
     * Create new menu item  and set the action to url.
196
     *
197
     * @param $url
198
     * @param $title
199
     * @param array $attributes
200
     *
201
     * @return array
202
     */
203
    public function url($url, $title, $order = 0, $attributes = array())
204
    {
205 View Code Duplication
        if (func_num_args() == 3) {
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...
206
            $arguments = func_get_args();
207
208
            return $this->add([
209
                'url' => array_get($arguments, 0),
210
                'title' => array_get($arguments, 1),
211
                'attributes' => array_get($arguments, 2),
212
            ]);
213
        }
214
215
        return $this->add(compact('url', 'title', 'order', 'attributes'));
216
    }
217
218
    /**
219
     * Add new child item.
220
     *
221
     * @param array $properties
222
     *
223
     * @return $this
224
     */
225
    public function add(array $properties)
226
    {
227
        $item = static::make($properties);
228
229
        $this->childs[] = $item;
230
231
        return $item;
232
    }
233
234
    /**
235
     * Add new divider.
236
     *
237
     * @param int $order
238
     *
239
     * @return self
240
     */
241
    public function addDivider($order = null)
242
    {
243
        $item = static::make(array('name' => 'divider', 'order' => $order));
244
245
        $this->childs[] = $item;
246
247
        return $item;
248
    }
249
250
    /**
251
     * Alias method instead "addDivider".
252
     *
253
     * @return MenuItem
254
     */
255
    public function divider()
256
    {
257
        return $this->addDivider();
258
    }
259
260
    /**
261
     * Add dropdown header.
262
     *
263
     * @param $title
264
     *
265
     * @return $this
266
     */
267
    public function addHeader($title)
268
    {
269
        $item = static::make(array(
270
            'name' => 'header',
271
            'title' => $title,
272
        ));
273
274
        $this->childs[] = $item;
275
276
        return $item;
277
    }
278
279
    /**
280
     * Same with "addHeader" method.
281
     *
282
     * @param $title
283
     *
284
     * @return $this
285
     */
286
    public function header($title)
287
    {
288
        return $this->addHeader($title);
289
    }
290
291
    /**
292
     * Get childs.
293
     *
294
     * @return array
295
     */
296
    public function getChilds()
297
    {
298
        if (config('menus.ordering')) {
299
            return collect($this->childs)->sortBy(function ($child) {
300
                return $child->order;
301
            })->all();
302
        }
303
304
        return $this->childs;
305
    }
306
307
    /**
308
     * Get url.
309
     *
310
     * @return string
311
     */
312
    public function getUrl()
313
    {
314
        return !empty($this->route) ? route($this->route[0], $this->route[1]) : url($this->url);
0 ignored issues
show
Documentation introduced by
The property route does not exist on object<Nwidart\Menus\MenuItem>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property url does not exist on object<Nwidart\Menus\MenuItem>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
315
    }
316
317
    /**
318
     * Get request url.
319
     *
320
     * @return string
321
     */
322
    public function getRequest()
323
    {
324
        return ltrim(str_replace(url('/'), '', $this->getUrl()), '/');
325
    }
326
327
    /**
328
     * Get icon.
329
     *
330
     * @param null|string $default
331
     *
332
     * @return string
333
     */
334
    public function getIcon($default = null)
335
    {
336
        return !is_null($this->icon) ? '<i class="' . $this->icon . '"></i>' : $default;
0 ignored issues
show
Documentation introduced by
The property icon does not exist on object<Nwidart\Menus\MenuItem>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
337
    }
338
339
    /**
340
     * Get properties.
341
     *
342
     * @return array
343
     */
344
    public function getProperties()
345
    {
346
        return $this->properties;
347
    }
348
349
    /**
350
     * Get HTML attribute data.
351
     *
352
     * @return mixed
353
     */
354
    public function getAttributes()
355
    {
356
        $attributes = $this->attributes ? $this->attributes : [];
0 ignored issues
show
Documentation introduced by
The property attributes does not exist on object<Nwidart\Menus\MenuItem>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
357
358
        array_forget($attributes, ['active', 'icon']);
359
360
        return HTML::attributes($attributes);
361
    }
362
363
    /**
364
     * Check is the current item divider.
365
     *
366
     * @return bool
367
     */
368
    public function isDivider()
369
    {
370
        return $this->is('divider');
371
    }
372
373
    /**
374
     * Check is the current item divider.
375
     *
376
     * @return bool
377
     */
378
    public function isHeader()
379
    {
380
        return $this->is('header');
381
    }
382
383
    /**
384
     * Check is the current item divider.
385
     *
386
     * @param $name
387
     *
388
     * @return bool
389
     */
390
    public function is($name)
391
    {
392
        return $this->name == $name;
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Nwidart\Menus\MenuItem>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
393
    }
394
395
    /**
396
     * Check is the current item has sub menu .
397
     *
398
     * @return bool
399
     */
400
    public function hasSubMenu()
401
    {
402
        return !empty($this->childs);
403
    }
404
405
    /**
406
     * Same with hasSubMenu.
407
     *
408
     * @return bool
409
     */
410
    public function hasChilds()
411
    {
412
        return $this->hasSubMenu();
413
    }
414
415
    /**
416
     * Check the active state for current menu.
417
     *
418
     * @return mixed
419
     */
420
    public function hasActiveOnChild()
421
    {
422
        if ($this->inactive()) {
423
            return false;
424
        }
425
426
        return $this->hasChilds() ? $this->getActiveStateFromChilds() : false;
427
    }
428
429
    /**
430
     * Get active state from child menu items.
431
     *
432
     * @return bool
433
     */
434
    public function getActiveStateFromChilds()
435
    {
436
        foreach ($this->getChilds() as $child) {
437
            if ($child->inactive()) {
438
                continue;
439
            } elseif ($child->hasChilds()) {
440
                if ($child->getActiveStateFromChilds()) {
441
                    return true;
442
                }
443
            } elseif ($child->isActive()) {
444
                return true;
445
            } elseif ($child->hasRoute() && $child->getActiveStateFromRoute()) {
446
                return true;
447
            } elseif ($child->getActiveStateFromUrl()) {
448
                return true;
449
            }
450
        }
451
452
        return false;
453
    }
454
455
    /**
456
     * Get inactive state.
457
     *
458
     * @return bool
459
     */
460
    public function inactive()
461
    {
462
        $inactive = $this->getInactiveAttribute();
463
464
        if (is_bool($inactive)) {
465
            return $inactive;
466
        }
467
468
        if ($inactive instanceof \Closure) {
469
            return call_user_func($inactive);
470
        }
471
472
        return false;
473
    }
474
475
    /**
476
     * Get active attribute.
477
     *
478
     * @return string
479
     */
480
    public function getActiveAttribute()
481
    {
482
        return array_get($this->attributes, 'active');
0 ignored issues
show
Documentation introduced by
The property attributes does not exist on object<Nwidart\Menus\MenuItem>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
483
    }
484
485
    /**
486
     * Get inactive attribute.
487
     *
488
     * @return string
489
     */
490
    public function getInactiveAttribute()
491
    {
492
        return array_get($this->attributes, 'inactive');
0 ignored issues
show
Documentation introduced by
The property attributes does not exist on object<Nwidart\Menus\MenuItem>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
493
    }
494
495
    /**
496
     * Get active state for current item.
497
     *
498
     * @return mixed
499
     */
500
    public function isActive()
501
    {
502
        if ($this->inactive()) {
503
            return false;
504
        }
505
506
        $active = $this->getActiveAttribute();
507
508
        if (is_bool($active)) {
509
            return $active;
510
        }
511
512
        if ($active instanceof \Closure) {
513
            return call_user_func($active);
514
        }
515
516
        if ($this->hasRoute()) {
517
            return $this->getActiveStateFromRoute();
518
        } else {
519
            return $this->getActiveStateFromUrl();
520
        }
521
    }
522
523
    /**
524
     * Determine the current item using route.
525
     *
526
     * @return bool
527
     */
528
    protected function hasRoute()
529
    {
530
        return !empty($this->route);
0 ignored issues
show
Documentation introduced by
The property route does not exist on object<Nwidart\Menus\MenuItem>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
531
    }
532
533
    /**
534
     * Get active status using route.
535
     *
536
     * @return bool
537
     */
538
    protected function getActiveStateFromRoute()
539
    {
540
        return Request::is(str_replace(url('/') . '/', '', $this->getUrl()));
541
    }
542
543
    /**
544
     * Get active status using request url.
545
     *
546
     * @return bool
547
     */
548
    protected function getActiveStateFromUrl()
549
    {
550
        return Request::is($this->url);
0 ignored issues
show
Documentation introduced by
The property url does not exist on object<Nwidart\Menus\MenuItem>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
551
    }
552
553
    /**
554
     * Set order value.
555
     *
556
     * @param  int $order
557
     * @return self
558
     */
559
    public function order($order)
560
    {
561
        $this->order = $order;
0 ignored issues
show
Bug introduced by
The property order 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...
562
563
        return $this;
564
    }
565
566
    /**
567
     * Set hide condition for current menu item.
568
     *
569
     * @param  Closure
570
     * @return boolean
571
     */
572
    public function hideWhen(Closure $callback)
573
    {
574
        $this->hideWhen = $callback;
575
576
        return $this;
577
    }
578
579
    /**
580
     * Determine whether the menu item is hidden.
581
     *
582
     * @return boolean
583
     */
584
    public function hidden()
585
    {
586
        if (is_null($this->hideWhen)) {
587
            return false;
588
        }
589
590
        return call_user_func($this->hideWhen) == true;
591
    }
592
593
    /**
594
     * Get the instance as an array.
595
     *
596
     * @return array
597
     */
598
    public function toArray()
599
    {
600
        return $this->getProperties();
601
    }
602
603
    /**
604
     * Get property.
605
     *
606
     * @param string $key
607
     *
608
     * @return string|null
609
     */
610
    public function __get($key)
611
    {
612
        return isset($this->$key) ? $this->$key : null;
613
    }
614
}
615