TableOfContentsHelper   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 10.42%

Importance

Changes 0
Metric Value
wmc 12
eloc 49
c 0
b 0
f 0
dl 0
loc 90
rs 10
ccs 5
cts 48
cp 0.1042

4 Methods

Rating   Name   Duplication   Size   Complexity  
A processDocument() 0 24 4
A getTableOfContents() 0 13 2
A computeToc() 0 44 5
A __construct() 0 2 1
1
<?php
2
3
namespace App\Markdown;
4
5
use EasySlugger\SluggerInterface;
6
use League\CommonMark\Block\Element\Document;
7
use League\CommonMark\Block\Element\Heading;
8
use League\CommonMark\DocParser;
9
use League\CommonMark\EnvironmentInterface;
10
use Psr\Cache\CacheItemPoolInterface;
11
12
class TableOfContentsHelper {
13
    public function __construct(private SluggerInterface $slugger, private CacheItemPoolInterface $cache, private EnvironmentInterface $environment)
14
    {
15
    }
16
17 14
    public function getTableOfContents(string $markdown) {
18 14
        $hash = hash('sha512', $markdown);
19 14
        $key = sprintf('markdown.toc.%s', $hash);
20 14
21 14
        $item = $this->cache->getItem($key);
22
23
        if(!$item->isHit()) {
24
            $toc = $this->computeToc($markdown);
25
26
            $item->set(serialize($toc));
27
        }
28
29
        return unserialize($item->get());
30
    }
31
32
    private function computeToc(string $markdown): array {
33
        $parser = new DocParser($this->environment);
34
        $document = $parser->parse($markdown);
35
        $toc = $this->processDocument($document);
36
37
        $counter = [
38
            2 => 0,
39
            3 => 0,
40
            4 => 0,
41
            5 => 0,
42
            6 => 0
43
        ];
44
45
        $items = [ ];
46
        $lastLevel = 2;
47
48
        foreach($toc as $value) {
49
            $level = $value['level'];
50
51
            if($level < $lastLevel) {
52
                // reset counters
53
                for($i = $lastLevel; $i < count($counter); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
54
                    $counter[$i] = 0;
55
                }
56
            }
57
58
            $counter[$level]++;
59
60
            $currentLevel = '';
61
            for($i = 2; $i <= $level; $i++) {
62
                $currentLevel .= $counter[$i] . '.';
63
            }
64
            $currentLevel = substr($currentLevel, 0, -1);
65
66
            $items[] = [
67
                'level' => $currentLevel,
68
                'id' => $value['id'],
69
                'text' => $value['text']
70
            ];
71
72
            $lastLevel = $level;
73
        }
74
75
        return $items;
76
    }
77
78
    private function processDocument(Document $document): array {
79
        $toc = [ ];
80
        $walker = $document->walker();
81
82
        while($event = $walker->next()) {
83
            $node = $event->getNode();
84
85
            if(!($node instanceof Heading) || !$event->isEntering()) {
86
                continue;
87
            }
88
89
            $heading = $node->getStringContent();
90
            $slug = $this->slugger->slugify($heading);
91
92
            $level = min($node->getLevel() + 1, 6);
93
94
            $toc[] = [
95
                'id' => $slug,
96
                'level' => $level,
97
                'text' => $heading
98
            ];
99
        }
100
101
        return $toc;
102
    }
103
}