Completed
Push — 1.5 ( f40f61...09e907 )
by Colin
02:28
created

AnonymousFootnotesListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 11

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 11
dl 0
loc 27
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A onDocumentParsed() 0 24 5
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 * (c) Rezo Zero / Ambroise Maupate
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace League\CommonMark\Extension\Footnote\Event;
16
17
use League\CommonMark\Block\Element\Paragraph;
18
use League\CommonMark\Event\DocumentParsedEvent;
19
use League\CommonMark\Extension\Footnote\Node\Footnote;
20
use League\CommonMark\Extension\Footnote\Node\FootnoteBackref;
21
use League\CommonMark\Extension\Footnote\Node\FootnoteRef;
22
use League\CommonMark\Inline\Element\Text;
23
use League\CommonMark\Reference\Reference;
24
25
final class AnonymousFootnotesListener
26
{
27 42
    public function onDocumentParsed(DocumentParsedEvent $event): void
28
    {
29 42
        $document = $event->getDocument();
30 42
        $walker = $document->walker();
31
32 42
        while ($event = $walker->next()) {
33 42
            $node = $event->getNode();
34 42
            if ($node instanceof FootnoteRef && $event->isEntering() && null !== $text = $node->getContent()) {
35
                // Anonymous footnote needs to create a footnote from its content
36 18
                $existingReference = $node->getReference();
37 18
                $reference = new Reference(
38 18
                    $existingReference->getLabel(),
39 18
                    '#fnref:' . $existingReference->getLabel(),
40 18
                    $existingReference->getTitle()
41
                );
42 18
                $footnote = new Footnote($reference);
43 18
                $footnote->addBackref(new FootnoteBackref($reference));
44 18
                $paragraph = new Paragraph();
45 18
                $paragraph->appendChild(new Text($text));
46 18
                $footnote->appendChild($paragraph);
47 18
                $document->appendChild($footnote);
48
            }
49
        }
50 42
    }
51
}
52