Passed
Push — master ( 00b783...b7fb13 )
by Caen
03:02 queued 11s
created

constructDocumentationPageSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Hyde\Framework\Concerns\FrontMatter\Schemas;
4
5
use Illuminate\Support\Str;
6
7
trait DocumentationPageSchema
8
{
9
    /**
10
     * The sidebar category group, if any.
11
     */
12
    public ?string $category = null;
13
14
    /**
15
     * The label for the page shown in the sidebar.
16
     */
17
    public ?string $label;
18
19
    /**
20
     * Hides the page from the sidebar.
21
     */
22
    public ?bool $hidden = null;
23
24
    /**
25
     * The priority of the page used for ordering the sidebar.
26
     */
27
    public ?int $priority = null;
28
29
    protected function constructDocumentationPageSchema(): void
30
    {
31
        $this->category = static::getDocumentationPageCategory();
0 ignored issues
show
Bug Best Practice introduced by
The method Hyde\Framework\Concerns\...mentationPageCategory() is not static, but was called statically. ( Ignorable by Annotation )

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

31
        /** @scrutinizer ignore-call */ 
32
        $this->category = static::getDocumentationPageCategory();
Loading history...
32
33
        $this->hidden = $this->matter('hidden', $this->identifier === 'index');
0 ignored issues
show
Bug introduced by
It seems like matter() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

33
        /** @scrutinizer ignore-call */ 
34
        $this->hidden = $this->matter('hidden', $this->identifier === 'index');
Loading history...
34
        $this->priority = $this->matter('priority', $this->findPriorityInConfig());
35
    }
36
37
    protected function getDocumentationPageCategory(): ?string
38
    {
39
        // If the documentation page is in a subdirectory,
40
        // then we can use that as the category name.
41
        // Otherwise, we look in the front matter.
42
43
        return str_contains($this->identifier, '/')
44
            ? Str::before($this->identifier, '/')
45
            : $this->matter('category');
46
    }
47
48
    protected function findPriorityInConfig(): int
49
    {
50
        $orderIndexArray = config('docs.sidebar_order', []);
51
52
        if (! in_array($this->identifier, $orderIndexArray)) {
53
            return 500;
54
        }
55
56
        return array_search($this->identifier, $orderIndexArray) + 250;
57
58
        // Adding 250 makes so that pages with a front matter priority that is lower
59
        // can be shown first. It's lower than the fallback of 500 so that they
60
        // still come first. This is all to make it easier to mix priorities.
61
    }
62
}
63