Passed
Push — master ( a0ff31...47a9f8 )
by Caen
03:50 queued 14s
created

FindsTitleForDocument   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 24
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 8 3
A findTitleTagInMarkdown() 0 11 3
1
<?php
2
3
namespace Hyde\Framework\Actions;
4
5
use Hyde\Framework\Hyde;
6
7
/**
8
 * Replaces @see \Hyde\Framework\Concerns\HasDynamicTitle.
9
 */
10
class FindsTitleForDocument
11
{
12
    public static function get(string $slug = '', array $matter = [], string $markdown = ''): string
13
    {
14
        if (isset($matter['title'])) {
15
            return $matter['title'];
16
        }
17
18
        return static::findTitleTagInMarkdown($markdown)
19
            ?: Hyde::makeTitle($slug);
20
    }
21
22
    /** Attempt to find the title based on the first H1 tag. */
23
    protected static function findTitleTagInMarkdown(string $markdown): string|false
24
    {
25
        $lines = explode("\n", $markdown);
26
27
        foreach ($lines as $line) {
28
            if (str_starts_with($line, '# ')) {
29
                return trim(substr($line, 2), ' ');
30
            }
31
        }
32
33
        return false;
34
    }
35
}
36