Completed
Push — master ( 2b3dd9...7d2542 )
by ARCANEDEV
08:42
created

Manager::syncCurrentName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4286
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php namespace Arcanesoft\Core\Helpers\Sidebar;
2
3
use Arcanesoft\Core\Helpers\Sidebar\Entities\Item;
4
use Arcanesoft\Core\Helpers\Sidebar\Entities\ItemCollection;
5
6
/**
7
 * Class     Manager
8
 *
9
 * @package  Arcanesoft\Core\Helpers\Sidebar
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class Manager implements Contracts\Sidebar
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Properties
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    /**
19
     * The view name.
20
     *
21
     * @var string
22
     */
23
    protected $view = '';
24
25
    /**
26
     * The current name.
27
     *
28
     * @var string
29
     */
30
    protected $currentName;
31
32
    /**
33
     * The sidebar items collection.
34
     *
35
     * @var \Arcanesoft\Core\Helpers\Sidebar\Entities\ItemCollection
36
     */
37
    protected $items;
38
39
    /* ------------------------------------------------------------------------------------------------
40
     |  Constructor
41
     | ------------------------------------------------------------------------------------------------
42
     */
43
    public function __construct()
44
    {
45
        $this->items = new ItemCollection;
46
    }
47
48
    /* ------------------------------------------------------------------------------------------------
49
     |  Getters & Setters
50
     | ------------------------------------------------------------------------------------------------
51
     */
52
    /**
53
     * Set the view name.
54
     *
55
     * @param  string  $view
56
     *
57
     * @return self
58
     */
59
    public function setView($view)
60
    {
61
        if ( ! is_null($view)) {
62
            $this->view = $view;
63
        }
64
65
        return $this;
66
    }
67
68
    /**
69
     * Get the current item name.
70
     *
71
     * @return string
72
     */
73
    public function getCurrent()
74
    {
75
        return $this->currentName;
76
    }
77
78
    /**
79
     * Set the current item name.
80
     *
81
     * @param  string  $currentName
82
     *
83
     * @return $this
84
     */
85
    public function setCurrent($currentName)
86
    {
87
        $this->currentName = $currentName;
88
89
        return $this;
90
    }
91
92
    /**
93
     * Get the sidebar items.
94
     *
95
     * @return \Arcanesoft\Core\Helpers\Sidebar\Entities\ItemCollection
96
     */
97
    public function getItems()
98
    {
99
        return $this->items;
100
    }
101
102
    /* ------------------------------------------------------------------------------------------------
103
     |  Main Functions
104
     | ------------------------------------------------------------------------------------------------
105
     */
106
    /**
107
     * Add a routed item.
108
     *
109
     * @param  string       $name
110
     * @param  string       $title
111
     * @param  string       $route
112
     * @param  array        $parameters
113
     * @param  string|null  $icon
114
     *
115
     * @return self
116
     */
117
    public function addRouteItem($name, $title, $route, array $parameters = [], $icon = null)
118
    {
119
        return $this->addItem($name, $title, route($route, $parameters), $icon);
120
    }
121
122
    /**
123
     * Add an item.
124
     *
125
     * @param  string       $name
126
     * @param  string       $title
127
     * @param  string       $url
128
     * @param  string|null  $icon
129
     *
130
     * @return self
131
     */
132
    public function addItem($name, $title, $url = '#', $icon = null)
133
    {
134
        return $this->add(compact('name', 'title', 'url', 'icon'));
135
    }
136
137
    /**
138
     * Add an item from array.
139
     *
140
     * @param  array  $array
141
     *
142
     * @return self
143
     */
144
    public function add(array $array)
145
    {
146
        $item = Item::makeFromArray($array);
147
148
        $this->items->push($item);
149
150
        return $this;
151
    }
152
153
    /**
154
     * Render the sidebar.
155
     *
156
     * @param  string|null  $view
157
     *
158
     * @return string
159
     */
160
    public function render($view = null)
161
    {
162
        $this->syncCurrentName()->setView($view);
163
164
        return view($this->view, ['sidebarItems' => $this->items])->render();
0 ignored issues
show
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
165
    }
166
167
    /* ------------------------------------------------------------------------------------------------
168
     |  Check Functions
169
     | ------------------------------------------------------------------------------------------------
170
     */
171
    /**
172
     * Check if the sidebar has items.
173
     *
174
     * @return bool
175
     */
176
    public function hasItems()
177
    {
178
        return ! $this->items->isEmpty();
179
    }
180
181
    /* ------------------------------------------------------------------------------------------------
182
     |  Other Functions
183
     | ------------------------------------------------------------------------------------------------
184
     */
185
    /**
186
     * Sync the current name wih the sidebar items.
187
     *
188
     * @return self
189
     */
190
    private function syncCurrentName()
191
    {
192
        $this->items->setCurrent($this->currentName);
193
194
        return $this;
195
    }
196
}
197