Passed
Push — master ( 48e02b...108ba0 )
by Caen
03:55 queued 15s
created

DocumentationSidebar::makeGroupTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Features\Navigation;
6
7
use Hyde\Hyde;
8
use Hyde\Facades\Config;
9
use Hyde\Foundation\Facades\Routes;
10
use Hyde\Pages\DocumentationPage;
11
use Hyde\Support\Facades\Render;
12
use Hyde\Support\Models\Route;
13
use Illuminate\Support\Collection;
14
use Illuminate\Support\Str;
15
16
use function collect;
17
18
class DocumentationSidebar extends BaseNavigationMenu
19
{
20
    protected function generate(): void
21
    {
22
        Routes::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\Routes. 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

22
        Routes::/** @scrutinizer ignore-call */ 
23
                getRoutes(DocumentationPage::class)->each(function (Route $route): void {
Loading history...
23
            if ($this->canAddRoute($route)) {
24
                $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

24
                $this->items->put(/** @scrutinizer ignore-type */ $route->getRouteKey(), NavItem::fromRoute($route));
Loading history...
25
            }
26
        });
27
28
        // If there are no pages other than the index page, we add it to the sidebar so that it's not empty
29
        if ($this->items->count() === 0 && DocumentationPage::home() !== null) {
30
            $this->items->push(NavItem::fromRoute(DocumentationPage::home(), group: 'other'));
0 ignored issues
show
Bug introduced by
It seems like Hyde\Pages\DocumentationPage::home() can also be of type null; however, parameter $route of Hyde\Framework\Features\...on\NavItem::fromRoute() does only seem to accept Hyde\Support\Models\Route, maybe add an additional type check? ( Ignorable by Annotation )

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

30
            $this->items->push(NavItem::fromRoute(/** @scrutinizer ignore-type */ DocumentationPage::home(), group: 'other'));
Loading history...
31
        }
32
    }
33
34
    public function hasGroups(): bool
35
    {
36
        return (count($this->getGroups()) >= 1) && ($this->getGroups() !== ['other']);
37
    }
38
39
    /** @return array<string> */
40
    public function getGroups(): array
41
    {
42
        return $this->items->map(function (NavItem $item): string {
43
            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...
44
        })->unique()->toArray();
45
    }
46
47
    /** @return Collection<\Hyde\Framework\Features\Navigation\NavItem> */
48
    public function getItemsInGroup(?string $group): Collection
49
    {
50
        return $this->items->filter(function (NavItem $item) use ($group): bool {
51
            return ($item->getGroup() === $group) || ($item->getGroup() === Str::slug($group));
52
        })->sortBy('navigation.priority')->values();
53
    }
54
55
    public function isGroupActive(string $group): bool
56
    {
57
        return Str::slug(Render::getPage()->navigationMenuGroup()) === $group
58
            || $this->isPageIndexPage() && $this->shouldIndexPageBeActive($group);
59
    }
60
61
    public function makeGroupTitle(string $group): string
62
    {
63
        return Config::getNullableString("docs.sidebar_group_labels.$group") ?? Hyde::makeTitle($group);
64
    }
65
66
    protected function canAddRoute(Route $route): bool
67
    {
68
        return parent::canAddRoute($route) && ! $route->is(DocumentationPage::homeRouteName());
69
    }
70
71
    private function isPageIndexPage(): bool
72
    {
73
        return Render::getPage()->getRoute()->is(DocumentationPage::homeRouteName());
74
    }
75
76
    private function shouldIndexPageBeActive(string $group): bool
77
    {
78
        return Render::getPage()->navigationMenuGroup() === 'other' && $group === collect($this->getGroups())->first();
0 ignored issues
show
Bug introduced by
$this->getGroups() of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

78
        return Render::getPage()->navigationMenuGroup() === 'other' && $group === collect(/** @scrutinizer ignore-type */ $this->getGroups())->first();
Loading history...
79
    }
80
}
81