GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 25ce45...0b6a33 )
by Sebastian
02:58
created

Menu::map()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 14
rs 9.4285
cc 2
eloc 7
nc 2
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A Menu::registerFilter() 0 6 1
1
<?php
2
3
namespace Spatie\Menu;
4
5
use Spatie\HtmlElement\HtmlElement;
6
use Spatie\Menu\Items\Link;
7
use Spatie\Menu\Traits\HtmlAttributes;
8
use Spatie\Menu\Traits\ParentAttributes;
9
use function Spatie\Menu\first_parameter_type;
10
11
class Menu implements Item
12
{
13
    use HtmlAttributes, ParentAttributes;
14
15
    /** @var array */
16
    protected $items = [];
17
18
    /** @var string */
19
    protected $prepend = '';
20
21
    /** @var string */
22
    protected $append = '';
23
24
    /** @var array */
25
    protected $filters = [];
26
27
    /**
28
     * @param \Spatie\Menu\Item[] ...$items
29
     */
30
    protected function __construct(Item ...$items)
31
    {
32
        $this->items = $items;
33
34
        $this->bootHtmlAttributes();
35
        $this->bootParentAttributes();
36
    }
37
38
    /**
39
     * Create a new menu, optionally prefilled with items.
40
     *
41
     * @param array $items
42
     *
43
     * @return static
44
     */
45
    public static function new(array $items = [])
46
    {
47
        return new static(...array_values($items));
48
    }
49
50
    /**
51
     * Add an item to the menu. This also applies all registered filters on the item. If a filter
52
     * returns false, the item won't be added.
53
     *
54
     * @param \Spatie\Menu\Item $item
55
     *
56
     * @return $this
57
     */
58
    public function add(Item $item)
59
    {
60
        foreach ($this->filters as $filter) {
61
            $this->applyFilter($filter, $item);
62
        }
63
64
        $this->items[] = $item;
65
66
        return $this;
67
    }
68
69
    /**
70
     * Apply a filter to an item. Returns the result of the filter.
71
     *
72
     * @param callable $filter
73
     * @param \Spatie\Menu\Item $item
74
     */
75
    protected function applyFilter(callable $filter, Item $item)
76
    {
77
        $type = first_parameter_type($filter);
78
79
        if ($type !== null && !$item instanceof $type) {
80
            return;
81
        }
82
83
        $filter($item);
84
    }
85
86
    /**
87
     * Iterate over all the items and apply a callback. If you typehint the
88
     * item parameter in the callable, it wil only be applied to items of that type.
89
     *
90
     * @param callable $callable
91
     *
92
     * @return $this
93
     */
94
    public function each(callable $callable)
95
    {
96
        $type = first_parameter_type($callable);
97
98
        foreach ($this->items as $item) {
99
            if ($type !== null && !$item instanceof $type) {
100
                continue;
101
            }
102
103
            $callable($item);
104
        }
105
106
        return $this;
107
    }
108
109
    /**
110
     * Register a filter to the menu. When an item is added, all filters will be applied to the
111
     * item. If you typehint the item
112
     * parameter in the callable, it wil only be applied to items of that type.
113
     *
114
     * @param callable $callable
115
     *
116
     * @return $this
117
     */
118
    public function registerFilter(callable $callable)
119
    {
120
        $this->filters[] = $callable;
121
122
        return $this;
123
    }
124
125
    /**
126
     * Apply a callable to all existing items, and register it as a filter so it will get applied
127
     * to all new items too. If you typehint the item parameter in the callable, it wil only be
128
     * applied to items of that type.
129
     *
130
     * @param callable $callable
131
     *
132
     * @return $this
133
     */
134
    public function applyToAll(callable $callable)
135
    {
136
        $this->each($callable);
137
        $this->registerFilter($callable);
138
139
        return $this;
140
    }
141
142
    /**
143
     * Prepend a string of html to the menu on render.
144
     *
145
     * @param string $prefix
146
     *
147
     * @return $this
148
     */
149
    public function prefixLinks(string $prefix)
150
    {
151
        return $this->applyToAll(function (Link $link) use ($prefix) {
152
            $link->prefix($prefix);
153
        });
154
    }
155
156
    /**
157
     * Prepend the menu with a string of html on render.
158
     *
159
     * @param string $prepend
160
     *
161
     * @return $this
162
     */
163
    public function prepend(string $prepend)
164
    {
165
        $this->prepend = $prepend;
166
167
        return $this;
168
    }
169
170
    /**
171
     * Prepend the menu with a string of html on render if a certain condition is met.
172
     *
173
     * @param bool $condition
174
     * @param string $prepend
175
     *
176
     * @return $this
177
     */
178
    public function prependIf(bool $condition, string $prepend)
179
    {
180
        if ($condition) {
181
            return $this->prepend($prepend);
182
        }
183
184
        return $this;
185
    }
186
187
    /**
188
     * Append a string of html to the menu on render.
189
     *
190
     * @param string $append
191
     *
192
     * @return $this
193
     */
194
    public function append(string $append)
195
    {
196
        $this->append = $append;
197
198
        return $this;
199
    }
200
201
    /**
202
     * Append the menu with a string of html on render if a certain condition is met.
203
     *
204
     * @param bool $condition
205
     * @param string $append
206
     *
207
     * @return static
208
     */
209
    public function appendIf(bool $condition, string $append)
210
    {
211
        if ($condition) {
212
            return $this->append($append);
213
        }
214
215
        return $this;
216
    }
217
218
    /**
219
     * Determine whether the menu is active.
220
     *
221
     * @return bool
222
     */
223
    public function isActive() : bool
224
    {
225
        foreach ($this->items as $item) {
226
            if ($item->isActive()) {
227
                return true;
228
            }
229
        }
230
231
        return false;
232
    }
233
234
    /**
235
     * Set multiple items in the menu as active based on a callable that filters through items.
236
     * If you typehint the item parameter in the callable, it wil only be applied to items of
237
     * that type.
238
     *
239
     * @param callable|string $patternOrCallable
240
     * @param string $root
241
     *
242
     * @return $this
243
     */
244
    public function setActive($patternOrCallable, string $root = '')
245
    {
246
        if (is_string($patternOrCallable)) {
247
            return $this->setActiveFromPattern($patternOrCallable, $root);
248
        }
249
250
        if (is_callable($patternOrCallable)) {
251
            return $this->setActiveFromCallable();
0 ignored issues
show
Bug introduced by
The call to setActiveFromCallable() misses a required argument $callable.

This check looks for function calls that miss required arguments.

Loading history...
252
        }
253
254
        throw new \InvalidArgumentException('`setActive` requires a pattern or a callable');
255
    }
256
257
    /**
258
     * @param string $pattern
259
     * @param string $root
260
     *
261
     * @throws \Exception
262
     */
263
    public function setActiveFromPattern(string $pattern, string $root = '')
0 ignored issues
show
Unused Code introduced by
The parameter $pattern is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $root is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
264
    {
265
        throw new \Exception('Todo');
266
    }
267
268
    /**
269
     * @param callable $callable
270
     *
271
     * @return $this
272
     */
273
    public function setActiveFromCallable(callable $callable)
274
    {
275
        $type = first_parameter_type($callable);
276
277
        return $this->applyToAll(function (Item $item) use ($callable, $type) {
278
            if ($type !== null && !$item instanceof $type) {
279
                return;
280
            }
281
282
            if ($callable($item)) {
283
                $item->setActive();
284
            }
285
        });
286
    }
287
288
    /**
289
     * @return string
290
     */
291
    public function render() : string
292
    {
293
        $contents = HtmlElement::render(
294
            'ul',
295
            $this->htmlAttributes->toArray(),
296
            array_map(function (Item $item) {
297
                return HtmlElement::render(
298
                    $item->isActive() ? 'li.active' : 'li',
299
                    $item->getParentAttributes(),
300
                    $item->render()
301
                );
302
            }, $this->items)
303
        );
304
305
        return "{$this->prepend}{$contents}{$this->append}";
306
    }
307
308
    /**
309
     * @return string
310
     */
311
    public function __toString() : string
312
    {
313
        return $this->render();
314
    }
315
}
316