Passed
Branch decouple-schema-constructors (7e448c)
by Caen
02:54
created

DocumentationPageSchemaConstructor   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 31
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A findPriorityInConfig() 0 9 2
A getDocumentationPageCategory() 0 9 2
A constructDocumentationPageSchema() 0 7 1
1
<?php
2
3
namespace Hyde\Framework\Concerns\FrontMatter\Schemas\Constructors;
4
5
use Hyde\Framework\Hyde;
6
use Illuminate\Support\Str;
7
8
trait DocumentationPageSchemaConstructor
9
{
10
    protected function constructDocumentationPageSchema(): void
11
    {
12
        $this->category = $this->getDocumentationPageCategory();
0 ignored issues
show
Bug Best Practice introduced by
The property category does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
13
14
        $this->label = $this->matter('label', Hyde::makeTitle(basename($this->identifier)));
0 ignored issues
show
Bug Best Practice introduced by
The property label does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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

14
        /** @scrutinizer ignore-call */ 
15
        $this->label = $this->matter('label', Hyde::makeTitle(basename($this->identifier)));
Loading history...
15
        $this->hidden = $this->matter('hidden', $this->identifier === 'index');
0 ignored issues
show
Bug Best Practice introduced by
The property hidden does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
16
        $this->priority = $this->matter('priority', $this->findPriorityInConfig());
0 ignored issues
show
Bug Best Practice introduced by
The property priority does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
17
    }
18
19
    protected function getDocumentationPageCategory(): ?string
20
    {
21
        // If the documentation page is in a subdirectory,
22
        // then we can use that as the category name.
23
        // Otherwise, we look in the front matter.
24
25
        return str_contains($this->identifier, '/')
26
            ? Str::before($this->identifier, '/')
27
            : $this->matter('category', 'other');
28
    }
29
30
    protected function findPriorityInConfig(): int
31
    {
32
        $orderIndexArray = config('docs.sidebar_order', []);
33
34
        if (! in_array($this->identifier, $orderIndexArray)) {
35
            return 500;
36
        }
37
38
        return array_search($this->identifier, $orderIndexArray) + 250;
39
40
        // Adding 250 makes so that pages with a front matter priority that is lower
41
        // can be shown first. It's lower than the fallback of 500 so that they
42
        // still come first. This is all to make it easier to mix priorities.
43
    }
44
}
45