Passed
Push — master ( a77e6b...67a175 )
by Caen
13:07 queued 12s
created

DocumentationSidebar::hasGroups()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Features\Navigation;
6
7
use Hyde\Foundation\Facades\Routes;
8
use Hyde\Pages\DocumentationPage;
9
use Hyde\Support\Facades\Render;
10
use Hyde\Support\Models\Route;
11
use Illuminate\Support\Collection;
12
use Illuminate\Support\Str;
13
use function collect;
14
15
/**
16
 * @see \Hyde\Framework\Testing\Feature\Services\DocumentationSidebarTest
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
29
    public function hasGroups(): bool
30
    {
31
        return (count($this->getGroups()) >= 1) && ($this->getGroups() !== ['other']);
32
    }
33
34
    /** @return array<string> */
35
    public function getGroups(): array
36
    {
37
        return $this->items->map(function (NavItem $item): string {
38
            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...
39
        })->unique()->toArray();
40
    }
41
42
    /** @return Collection<\Hyde\Framework\Features\Navigation\NavItem> */
43
    public function getItemsInGroup(?string $group): Collection
44
    {
45
        return $this->items->filter(function (NavItem $item) use ($group): bool {
46
            return ($item->getGroup() === $group) || ($item->getGroup() === Str::slug($group));
47
        })->sortBy('navigation.priority')->values();
48
    }
49
50
    public function isGroupActive(string $group): bool
51
    {
52
        return Str::slug(Render::getPage()->navigationMenuGroup()) === $group
53
            || $this->isPageIndexPage() && $this->shouldIndexPageBeActive($group);
54
    }
55
56
    protected function canAddRoute(Route $route): bool
57
    {
58
        return parent::canAddRoute($route) && ! $route->is(DocumentationPage::homeRouteName());
59
    }
60
61
    private function isPageIndexPage(): bool
62
    {
63
        return Render::getPage()->getRoute()->is(DocumentationPage::homeRouteName());
64
    }
65
66
    private function shouldIndexPageBeActive(string $group): bool
67
    {
68
        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

68
        return Render::getPage()->navigationMenuGroup() === 'other' && $group === collect(/** @scrutinizer ignore-type */ $this->getGroups())->first();
Loading history...
69
    }
70
}
71