Completed
Pull Request — master (#3)
by ARCANEDEV
07:13
created

SidebarItem::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
cc 1
nc 1
nop 1
ccs 11
cts 11
cp 1
crap 1
rs 9.7998
1
<?php namespace Arcanesoft\Sidebar;
2
3
use Illuminate\Contracts\Support\Arrayable;
4
use Illuminate\Contracts\Support\Jsonable;
5
use Illuminate\Support\Arr;
6
use JsonSerializable;
7
8
/**
9
 * Class     SidebarItem
10
 *
11
 * @package  Arcanesoft\Sidebar
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class SidebarItem implements Arrayable, Jsonable, JsonSerializable
15
{
16
    /* -----------------------------------------------------------------
17
     |  Properties
18
     | -----------------------------------------------------------------
19
     */
20
21
    /** @var  string */
22
    protected $name;
23
24
    /** @var  string */
25
    public $title;
26
27
    /** @var  string */
28
    public $url;
29
30
    /** @var  string */
31
    public $icon;
32
33
    /** @var  \Arcanesoft\Sidebar\SidebarCollection */
34
    public $children;
35
36
    /** @var  array */
37
    protected $roles;
38
39
    /** @var  array */
40
    protected $permissions;
41
42
    /** @var boolean */
43
    private $selected;
44
45
    /* -----------------------------------------------------------------
46
     |  Constructor
47
     | -----------------------------------------------------------------
48
     */
49
50
    /**
51
     * SidebarItem constructor.
52
     *
53
     * @param  array  $attributes
54
     */
55 52
    public function __construct(array $attributes)
56
    {
57 52
        $this->name        = Arr::pull($attributes, 'name');
58 52
        $this->setTitle(Arr::pull($attributes, 'title'));
59 52
        $this->icon        = Arr::pull($attributes, 'icon');
60 52
        $this->roles       = Arr::pull($attributes, 'roles', []);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Illuminate\Support\Arr:...utes, 'roles', array()) of type * is incompatible with the declared type array of property $roles.

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...
61 52
        $this->permissions = Arr::pull($attributes, 'permissions', []);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Illuminate\Support\Arr:...'permissions', array()) of type * is incompatible with the declared type array of property $permissions.

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...
62 52
        $this->children    = (new SidebarCollection)->pushItems(
63 52
            Arr::pull($attributes, 'children', [])
64
        );
65 52
        $this->selected    = false;
66
67 52
        $this->parseUrl($attributes);
68 52
    }
69
70
    /* -----------------------------------------------------------------
71
     |  Setters & Getters
72
     | -----------------------------------------------------------------
73
     */
74
75
    /**
76
     * Get the sidebar item's name.
77
     *
78
     * @return string
79
     */
80 8
    public function name()
81
    {
82 8
        return $this->name;
83
    }
84
85
    /**
86
     * Set the title.
87
     *
88
     * @param  string  $title
89
     *
90
     * @return $this
91
     */
92 52
    public function setTitle(string $title)
93
    {
94
        /** @var  \Illuminate\Translation\Translator  $translator */
95 52
        $translator  = trans();
96 52
        $this->title = $translator->has($title)
0 ignored issues
show
Documentation Bug introduced by
It seems like $translator->has($title)...r->get($title) : $title can also be of type array. However, the property $title is declared as type string. 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...
97 4
            ? $translator->get($title)
98 48
            : $title;
99
100 52
        return $this;
101
    }
102
103
    /**
104
     * Set the url.
105
     *
106
     * @param  string  $url
107
     *
108
     * @return $this
109
     */
110 52
    public function setUrl(string $url)
111
    {
112 52
        $this->url = $url;
113
114 52
        return $this;
115
    }
116
117
    /**
118
     * Set the selected item.
119
     *
120
     * @param  string  $name
121
     *
122
     * @return $this
123
     */
124 12
    public function setSelected(string $name)
125
    {
126 12
        $this->selected = ($this->name === $name);
127 12
        $this->children->setSelected($name);
128
129 12
        return $this;
130
    }
131
132
    /**
133
     * Get the roles.
134
     *
135
     * @return array|mixed
136
     */
137 8
    public function roles()
138
    {
139 8
        return $this->roles;
140
    }
141
142
    /**
143
     * Get the permissions.
144
     *
145
     * @return array|mixed
146
     */
147 8
    public function permissions()
148
    {
149 8
        return $this->permissions;
150
    }
151
152
    /* -----------------------------------------------------------------
153
     |  Main Methods
154
     | -----------------------------------------------------------------
155
     */
156
157
    /**
158
     * Set the url from the route.
159
     *
160
     * @param  string  $name
161
     * @param  array   $params
162
     *
163
     * @return $this
164
     */
165 4
    public function route($name, array $params = [])
166
    {
167 4
        return $this->setUrl(
168 3
            route($name, $params)
169
        );
170
    }
171
172
    /**
173
     * Set the url from the action.
174
     *
175
     * @param  string|array  $name
176
     * @param  array         $params
177
     *
178
     * @return $this
179
     */
180
    public function action($name, array $params = [])
181
    {
182
        return $this->setUrl(
183
            action($name, $params)
184
        );
185
    }
186
187
    /**
188
     * Get the active/inactive class.
189
     *
190
     * @param  string  $active
191
     * @param  string  $inactive
192
     *
193
     * @return string
194
     */
195 4
    public function active($active = 'active', $inactive = '') : string
196
    {
197 4
        return $this->isActive() ? $active : $inactive;
198
    }
199
200
    /**
201
     * Push multiple child items.
202
     *
203
     * @param  array  $items
204
     *
205
     * @return $this
206
     */
207 4
    public function addChildren(array $items)
208
    {
209 4
        $this->children->pushItems($items);
210
211 4
        return $this;
212
    }
213
214
    /* -----------------------------------------------------------------
215
     |  Check Methods
216
     | -----------------------------------------------------------------
217
     */
218
219
    /**
220
     * Check if has children.
221
     *
222
     * @return bool
223
     */
224 44
    public function hasChildren() : bool
225
    {
226 44
        return $this->children->isNotEmpty();
227
    }
228
229
    /**
230
     * Check if has any selected children.
231
     *
232
     * @return bool
233
     */
234 40
    public function hasAnySelectedChildren() : bool
235
    {
236 40
        return $this->hasChildren()
237 40
            && $this->children->hasAnySelected();
238
    }
239
240
    /**
241
     * Check if the item is active.
242
     *
243
     * @return bool
244
     */
245 40
    public function isActive() : bool
246
    {
247 40
        return $this->isSelected() || $this->hasAnySelectedChildren();
248
    }
249
250
    /**
251
     * Check if the item is selected.
252
     *
253
     * @return bool
254
     */
255 40
    public function isSelected() : bool
256
    {
257 40
        return $this->selected;
258
    }
259
260
    /* -----------------------------------------------------------------
261
     |  Other Methods
262
     | -----------------------------------------------------------------
263
     */
264
265
    /**
266
     * Parse the url attribute.
267
     *
268
     * @param  array  $attributes
269
     *
270
     * @return void
271
     */
272 52
    protected function parseUrl(array $attributes) : void
273
    {
274 52
        if (isset($attributes['url']))
275 52
            $this->setUrl($attributes['url']);
276 4
        elseif (isset($attributes['route']))
277 4
            $this->route(...Arr::wrap($attributes['route']));
0 ignored issues
show
Documentation introduced by
\Illuminate\Support\Arr:...p($attributes['route']) is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
278
        elseif (isset($attributes['action']))
279
            $this->action(...Arr::wrap($attributes['action']));
280
        else
281
            $this->setUrl('#');
282 52
    }
283
284
    /**
285
     * Get the instance as an array.
286
     *
287
     * @return array
288
     */
289 24
    public function toArray()
290
    {
291
        return [
292 24
            'name'        => $this->name,
293 24
            'title'       => $this->title,
294 24
            'url'         => $this->url,
295 24
            'icon'        => $this->icon,
296 24
            'active'      => $this->isActive(),
297 24
            'children'    => $this->children->toArray(),
298 24
            'roles'       => $this->roles,
299 24
            'permissions' => $this->permissions,
300
        ];
301
    }
302
303
    /**
304
     * Convert the object into something JSON serializable.
305
     *
306
     * @return array
307
     */
308 8
    public function jsonSerialize()
309
    {
310 8
        return $this->toArray();
311
    }
312
313
    /**
314
     * Get the collection of items as JSON.
315
     *
316
     * @param  int  $options
317
     *
318
     * @return string
319
     */
320 4
    public function toJson($options = 0)
321
    {
322 4
        return json_encode($this->jsonSerialize(), $options);
323
    }
324
325
    /**
326
     * Check if has roles.
327
     *
328
     * @return bool
329
     */
330 8
    public function hasRoles()
331
    {
332 8
        return ! empty($this->roles);
333
    }
334
335
    /**
336
     * Check if has permissions.
337
     *
338
     * @return bool
339
     */
340 8
    public function hasPermissions()
341
    {
342 8
        return ! empty($this->permissions);
343
    }
344
}
345