Passed
Push — master ( 9b4e67...25f483 )
by Caen
02:57 queued 12s
created

FindsTitleForPage::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 2
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Actions\Constructors;
4
5
use Hyde\Framework\Contracts\AbstractMarkdownPage;
6
use Hyde\Framework\Contracts\AbstractPage;
7
use Hyde\Framework\Hyde;
8
9
/**
10
 * @see \Hyde\Framework\Testing\Feature\PageModelConstructorTest
11
 *
12
 * @internal
13
 */
14
class FindsTitleForPage
15
{
16
    public static function run(AbstractPage $page): string
17
    {
18
        return (new static($page))->findTitleForPage();
19
    }
20
21
    protected function __construct(protected AbstractPage $page)
22
    {
23
    }
24
25
    protected function findTitleForPage(): string
26
    {
27
        return $this->page instanceof AbstractMarkdownPage
28
            ? $this->findTitleForMarkdownPage()
29
            : Hyde::makeTitle($this->page->identifier);
30
    }
31
32
    protected function findTitleForMarkdownPage(): string
33
    {
34
        return $this->page->matter('title')
35
            ?? $this->findTitleFromMarkdownHeadings()
36
            ?? Hyde::makeTitle($this->page->identifier);
37
    }
38
39
    protected function findTitleFromMarkdownHeadings(): ?string
40
    {
41
        if ($this->page instanceof AbstractMarkdownPage) {
42
            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

42
            foreach ($this->page->/** @scrutinizer ignore-call */ markdown()->toArray() as $line) {
Loading history...
43
                if (str_starts_with($line, '# ')) {
44
                    return trim(substr($line, 2), ' ');
45
                }
46
            }
47
        }
48
49
        return null;
50
    }
51
}
52