Menu::htmlIfCan()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Menu\Laravel;
4
5
use Illuminate\Contracts\Auth\Access\Gate;
6
use Illuminate\Contracts\Support\Htmlable;
7
use Illuminate\Support\Traits\Macroable;
8
use Spatie\Menu\Item;
9
use Spatie\Menu\Menu as BaseMenu;
10
11
class Menu extends BaseMenu implements Htmlable
12
{
13
    use Macroable;
14
15
    /**
16
     * Set all relevant children active based on the current request's URL.
17
     *
18
     * /, /about, /contact => request to /about will set the about link active.
19
     *
20
     * /en, /en/about, /en/contact => request to /en won't set /en active if the
21
     *                                request root is set to /en.
22
     *
23
     * @param string $requestRoot If the link's URL is an exact match with the
24
     *                            request root, the link won't be set active.
25
     *                            This behavior is to avoid having home links
26
     *                            active on every request.
27
     *
28
     * @return $this
29
     */
30
    public function setActiveFromRequest(string $requestRoot = '/')
31
    {
32
        return $this->setActive(app('request')->url(), $requestRoot);
33
    }
34
35
    /**
36
     * @param string $path
37
     * @param string $text
38
     * @param mixed $parameters
39
     * @param bool|null $secure
40
     *
41
     * @return $this
42
     */
43
    public function url(string $path, string $text, $parameters = [], $secure = null)
44
    {
45
        return $this->add(Link::toUrl($path, $text, $parameters, $secure));
46
    }
47
48
    /**
49
     * @param string|array $action
50
     * @param string $text
51
     * @param mixed $parameters
52
     * @param bool $absolute
53
     *
54
     * @return $this
55
     */
56
    public function action($action, string $text, $parameters = [], bool $absolute = true)
57
    {
58
        return $this->add(Link::toAction($action, $text, $parameters, $absolute));
59
    }
60
61
    /**
62
     * @param string $name
63
     * @param string $text
64
     * @param mixed $parameters
65
     * @param bool $absolute
66
     *
67
     * @return $this
68
     */
69
    public function route(string $name, string $text, $parameters = [], bool $absolute = true)
70
    {
71
        return $this->add(Link::toRoute($name, $text, $parameters, $absolute));
72
    }
73
74
    /**
75
     * @param string $name
76
     * @param array $data
77
     *
78
     * @return $this
79
     */
80
    public function view(string $name, array $data = [])
81
    {
82
        return $this->add(View::create($name, $data));
83
    }
84
85
    /**
86
     * @param bool $condition
87
     * @param string $path
88
     * @param string $text
89
     * @param array $parameters
90
     * @param bool|null $secure
91
     *
92
     * @return $this
93
     */
94
    public function urlIf($condition, string $path, string $text, array $parameters = [], $secure = null)
95
    {
96
        return $this->addIf($condition, Link::toUrl($path, $text, $parameters, $secure));
97
    }
98
99
    /**
100
     * @param bool $condition
101
     * @param string|array $action
102
     * @param string $text
103
     * @param array $parameters
104
     * @param bool $absolute
105
     *
106
     * @return $this
107
     */
108
    public function actionIf($condition, $action, string $text, array $parameters = [], bool $absolute = true)
109
    {
110
        return $this->addIf($condition, Link::toAction($action, $text, $parameters, $absolute));
111
    }
112
113
    /**
114
     * @param bool $condition
115
     * @param string $name
116
     * @param string $text
117
     * @param array $parameters
118
     * @param bool $absolute
119
     *
120
     * @return $this
121
     */
122
    public function routeIf($condition, string $name, string $text, array $parameters = [], bool $absolute = true)
123
    {
124
        return $this->addIf($condition, Link::toRoute($name, $text, $parameters, $absolute));
125
    }
126
127
    /**
128
     * @param $condition
129
     * @param string $name
130
     * @param array $data
131
     *
132
     * @return $this
133
     */
134
    public function viewIf($condition, string $name, array $data = null)
135
    {
136
        return $this->addIf($condition, View::create($name, $data));
0 ignored issues
show
Bug introduced by
It seems like $data defined by parameter $data on line 134 can also be of type null; however, Spatie\Menu\Laravel\View::create() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
137
    }
138
139
    /**
140
     * @param string|array $authorization
141
     * @param \Spatie\Menu\Item $item
142
     *
143
     * @return $this
144
     */
145
    public function addIfCan($authorization, Item $item)
146
    {
147
        $ablityArguments = is_array($authorization) ? $authorization : [$authorization];
148
        $ability = array_shift($ablityArguments);
149
150
        return $this->addIf(app(Gate::class)->allows($ability, $ablityArguments), $item);
151
    }
152
153
    /**
154
     * @param string|array $authorization
155
     * @param string $url
156
     * @param string $text
157
     *
158
     * @return $this
159
     */
160
    public function linkIfCan($authorization, string $url, string $text)
161
    {
162
        return $this->addIfCan($authorization, Link::to($url, $text));
163
    }
164
165
    /**
166
     * @param string|array $authorization
167
     * @param string $html
168
     *
169
     * @return \Spatie\Menu\Laravel\Menu
170
     */
171
    public function htmlIfCan($authorization, string $html)
172
    {
173
        return $this->addIfCan($authorization, Html::raw($html));
174
    }
175
176
    /**
177
     * @param string|array $authorization
178
     * @param callable|\Spatie\Menu\Menu|\Spatie\Menu\Item $header
179
     * @param callable|\Spatie\Menu\Menu|null $menu
180
     *
181
     * @return $this
182
     */
183
    public function submenuIfCan($authorization, $header, $menu = null)
184
    {
185
        [$authorization, $header, $menu] = $this->parseSubmenuIfCanArgs(...func_get_args());
186
187
        $menu = $this->createSubmenuMenu($menu);
0 ignored issues
show
Bug introduced by
It seems like $menu can also be of type null; however, Spatie\Menu\Menu::createSubmenuMenu() does only seem to accept callable, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
188
        $header = $this->createSubmenuHeader($header);
0 ignored issues
show
Bug introduced by
It seems like $header can also be of type callable; however, Spatie\Menu\Menu::createSubmenuHeader() does only seem to accept object<Spatie\Menu\Item>|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
189
190
        return $this->addIfCan($authorization, $menu->prependIf($header, $header));
0 ignored issues
show
Documentation introduced by
$header is of type string, but the function expects a boolean.

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...
191
    }
192
193
    protected function parseSubmenuIfCanArgs($authorization, ...$args): array
194
    {
195
        return array_merge([$authorization], $this->parseSubmenuArgs($args));
196
    }
197
198
    /**
199
     * @param string|array $authorization
200
     * @param string $path
201
     * @param string $text
202
     * @param array $parameters
203
     * @param bool|null $secure
204
     *
205
     * @return $this
206
     */
207
    public function urlIfCan($authorization, string $path, string $text, array $parameters = [], $secure = null)
208
    {
209
        return $this->addIfCan($authorization, Link::toUrl($path, $text, $parameters, $secure));
210
    }
211
212
    /**
213
     * @param string|array $authorization
214
     * @param string|array $action
215
     * @param string $text
216
     * @param array $parameters
217
     * @param bool $absolute
218
     *
219
     * @return $this
220
     */
221
    public function actionIfCan($authorization, $action, string $text, array $parameters = [], bool $absolute = true)
222
    {
223
        return $this->addIfCan($authorization, Link::toAction($action, $text, $parameters, $absolute));
224
    }
225
226
    /**
227
     * @param string|array $authorization
228
     * @param string $name
229
     * @param string $text
230
     * @param array $parameters
231
     * @param bool $absolute
232
     *
233
     * @return $this
234
     */
235
    public function routeIfCan($authorization, string $name, string $text, array $parameters = [], bool $absolute = true)
236
    {
237
        return $this->addIfCan($authorization, Link::toRoute($name, $text, $parameters, $absolute));
238
    }
239
240
    /**
241
     * @param $authorization
242
     * @param string $name
243
     * @param array $data
244
     *
245
     * @return $this
246
     * @internal param $condition
247
     */
248
    public function viewIfCan($authorization, string $name, array $data = null)
249
    {
250
        return $this->addIfCan($authorization, View::create($name, $data));
0 ignored issues
show
Bug introduced by
It seems like $data defined by parameter $data on line 248 can also be of type null; however, Spatie\Menu\Laravel\View::create() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
251
    }
252
253
    /**
254
     * @return string
255
     */
256
    public function toHtml(): string
257
    {
258
        return $this->render();
259
    }
260
}
261