Completed
Pull Request — master (#1)
by Michael
02:12
created

MenuItem::active()   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 Iatstuti\SimpleMenu;
4
5
use Iatstuti\SimpleMenu\Traits\ObjectOptions;
6
use Iatstuti\Support\Traits\MethodPropertyAccess;
7
8
/**
9
 * POPO object to wrap a menu item.
10
 *
11
 * @package    Iatstuti\SimpleMenu
12
 * @copyright  2016 IATSTUTI
13
 * @author     Michael Dyrynda <[email protected]>
14
 */
15
class MenuItem
16
{
17
18
    use MethodPropertyAccess, ObjectOptions;
19
20
    /**
21
     * @var string
22
     */
23
    protected $label;
24
25
    /**
26
     * @var string
27
     */
28
    protected $link;
29
30
31
    /**
32
     * MenuItem constructor.
33
     *
34
     * @param  $label
35
     * @param  $link
36
     * @param  array $options
37
     */
38
    public function __construct($label, $link, array $options = [ ])
39
    {
40
        $this->label   = $label;
41
        $this->link    = $link;
42
        $this->options = array_merge([
1 ignored issue
show
Bug introduced by
The property options 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...
43
            'active' => false,
44
            'class'  => null,
45
            'weight' => 0,
46
        ], $options);
47
    }
48
49
50
    /**
51
     * Return this menu item's label.
52
     *
53
     * @return string
54
     */
55
    public function label()
56
    {
57
        return $this->label;
58
    }
59
60
61
    /**
62
     * Return this menu item's link.
63
     *
64
     * @return string
65
     */
66
    public function link()
67
    {
68
        return $this->link;
69
    }
70
71
72
    /**
73
     * Return this menu item's weight.
74
     *
75
     * @return int
1 ignored issue
show
Documentation introduced by
Should the return type not be array?

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...
76
     */
77
    public function weight()
78
    {
79
        return $this->options('weight');
80
    }
81
82
83
    /**
84
     * Mark this menu item as the currently active one.
85
     *
86
     * @param  string $class
87
     *
88
     * @return $this
89
     */
90
    public function active($class = 'active')
91
    {
92
        $this->options = array_merge($this->options, [ 'active' => true, 'class' => $class, ]);
93
94
        return $this;
95
    }
96
}
97