Passed
Push — master ( 0ff158...930e60 )
by Caen
14:22 queued 13s
created

DocumentationSidebar::getItemsInGroup()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Features\Navigation;
6
7
use Hyde\Foundation\Facades\Router;
8
use Hyde\Pages\DocumentationPage;
9
use Hyde\Support\Models\Route;
10
use Illuminate\Support\Collection;
11
use Illuminate\Support\Str;
12
13
/**
14
 * @see \Hyde\Framework\Testing\Feature\Services\DocumentationSidebarTest
15
 */
16
class DocumentationSidebar extends BaseNavigationMenu
17
{
18
    /** @return $this */
19
    public function generate(): static
20
    {
21
        Router::getRoutes(DocumentationPage::class)->each(function (Route $route): void {
0 ignored issues
show
Bug introduced by
The method getRoutes() does not exist on Hyde\Foundation\Facades\Router. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
        Router::/** @scrutinizer ignore-call */ 
22
                getRoutes(DocumentationPage::class)->each(function (Route $route): void {
Loading history...
22
            $this->items->put($route->getRouteKey(), NavItem::fromRoute($route));
0 ignored issues
show
Bug introduced by
$route->getRouteKey() of type string is incompatible with the type Illuminate\Support\TKey expected by parameter $key of Illuminate\Support\Collection::put(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

22
            $this->items->put(/** @scrutinizer ignore-type */ $route->getRouteKey(), NavItem::fromRoute($route));
Loading history...
23
        });
24
25
        return $this;
26
    }
27
28
    public function hasGroups(): bool
29
    {
30
        return (count($this->getGroups()) >= 1) && ($this->getGroups() !== ['other']);
31
    }
32
33
    public function getGroups(): array
34
    {
35
        return $this->items->map(function (NavItem $item): string {
36
            return $item->getGroup();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $item->getGroup() could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
37
        })->unique()->toArray();
38
    }
39
40
    public function getItemsInGroup(?string $group): Collection
41
    {
42
        return $this->items->filter(function (NavItem $item) use ($group): bool {
43
            return ($item->getGroup() === $group) || ($item->getGroup() === Str::slug($group));
44
        })->sortBy('navigation.priority')->values();
45
    }
46
47
    protected static function shouldItemBeHidden(NavItem $item): bool
48
    {
49
        return parent::shouldItemBeHidden($item) || $item->getRoute()?->is('docs/index');
50
    }
51
}
52