Passed
Push — master ( 421bfc...8e1cd9 )
by Caen
03:38 queued 13s
created

getDocumentationPageCategory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
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 2
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Hyde\Framework\Actions;
4
5
use Hyde\Framework\Contracts\AbstractMarkdownPage;
6
use Hyde\Framework\Contracts\AbstractPage;
7
use Hyde\Framework\Hyde;
8
use Hyde\Framework\Models\Pages\BladePage;
9
use Hyde\Framework\Models\Pages\DocumentationPage;
10
use Illuminate\Support\Str;
11
12
/**
13
 * Dynamically constructs data for a page model.
14
 *
15
 * @see \Hyde\Framework\Testing\Feature\PageModelConstructorTest
16
 */
17
class PageModelConstructor
18
{
19
    /**
20
     * @var AbstractPage|AbstractMarkdownPage|BladePage
21
     */
22
    protected AbstractPage|AbstractMarkdownPage|BladePage $page;
23
24
    public static function run(AbstractPage $page): AbstractPage
25
    {
26
        return (new static($page))->get();
27
    }
28
29
    protected function __construct(AbstractPage $page)
30
    {
31
        $this->page = $page;
32
        $this->constructDynamicData();
33
    }
34
35
    protected function constructDynamicData(): void
36
    {
37
        $this->page->title = static::findTitleForPage();
0 ignored issues
show
Bug introduced by
The property title does not seem to exist on Hyde\Framework\Models\Pages\BladePage.
Loading history...
Bug Best Practice introduced by
The method Hyde\Framework\Actions\P...tor::findTitleForPage() 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

37
        /** @scrutinizer ignore-call */ 
38
        $this->page->title = static::findTitleForPage();
Loading history...
38
39
        if ($this->page instanceof DocumentationPage) {
40
            $this->page->category = static::getDocumentationPageCategory();
0 ignored issues
show
Bug introduced by
The property category does not seem to exist on Hyde\Framework\Models\Pages\BladePage.
Loading history...
Bug Best Practice introduced by
The method Hyde\Framework\Actions\P...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

40
            /** @scrutinizer ignore-call */ 
41
            $this->page->category = static::getDocumentationPageCategory();
Loading history...
41
        }
42
    }
43
44
    protected function get(): AbstractPage
45
    {
46
        return $this->page;
47
    }
48
49
    protected function getDocumentationPageCategory(): ?string
50
    {
51
        // If the documentation page is in a subdirectory,
52
        // then we can use that as the category name.
53
        // Otherwise, we look in the front matter.
54
55
        return str_contains($this->page->identifier, '/')
56
            ? Str::before($this->page->identifier, '/')
57
            : $this->page->matter('category');
0 ignored issues
show
Bug introduced by
The method matter() does not exist on Hyde\Framework\Contracts\AbstractPage. It seems like you code against a sub-type of Hyde\Framework\Contracts\AbstractPage such as Hyde\Framework\Contracts\AbstractMarkdownPage. ( Ignorable by Annotation )

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

57
            : $this->page->/** @scrutinizer ignore-call */ matter('category');
Loading history...
Bug introduced by
The method matter() does not exist on Hyde\Framework\Models\Pages\BladePage. ( Ignorable by Annotation )

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

57
            : $this->page->/** @scrutinizer ignore-call */ matter('category');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
    }
59
60
    protected function findTitleForPage(): string
61
    {
62
        return $this->page instanceof AbstractMarkdownPage
63
            ? $this->findTitleForMarkdownPage()
64
            : Hyde::makeTitle($this->page->identifier);
65
    }
66
67
    protected function findTitleForMarkdownPage(): string
68
    {
69
        return $this->page->matter('title')
70
            ?? static::findTitleFromMarkdownHeadings()
0 ignored issues
show
Bug Best Practice introduced by
The method Hyde\Framework\Actions\P...eFromMarkdownHeadings() 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

70
            ?? static::/** @scrutinizer ignore-call */ findTitleFromMarkdownHeadings()
Loading history...
71
            ?? Hyde::makeTitle($this->page->identifier);
72
    }
73
74
    protected function findTitleFromMarkdownHeadings(): ?string
75
    {
76
        foreach ($this->page->markdown()->toArray() as $line) {
0 ignored issues
show
Bug introduced by
The method markdown() does not exist on Hyde\Framework\Contracts\AbstractPage. It seems like you code against a sub-type of Hyde\Framework\Contracts\AbstractPage such as Hyde\Framework\Contracts\AbstractMarkdownPage. ( Ignorable by Annotation )

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

76
        foreach ($this->page->/** @scrutinizer ignore-call */ markdown()->toArray() as $line) {
Loading history...
Bug introduced by
The method markdown() does not exist on Hyde\Framework\Models\Pages\BladePage. ( Ignorable by Annotation )

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

76
        foreach ($this->page->/** @scrutinizer ignore-call */ markdown()->toArray() as $line) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
            if (str_starts_with($line, '# ')) {
78
                return trim(substr($line, 2), ' ');
79
            }
80
        }
81
82
        return null;
83
    }
84
}
85