Completed
Push — 1.5 ( c380b3...990492 )
by Colin
01:27
created

AnonymousFootnotesListener   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 12
dl 0
loc 35
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A onDocumentParsed() 0 24 5
A setConfiguration() 0 4 1
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
use League\CommonMark\Util\ConfigurationAwareInterface;
25
use League\CommonMark\Util\ConfigurationInterface;
26
27
final class AnonymousFootnotesListener implements ConfigurationAwareInterface
28
{
29
    /** @var ConfigurationInterface */
30
    private $config;
31
32 48
    public function onDocumentParsed(DocumentParsedEvent $event): void
33
    {
34 48
        $document = $event->getDocument();
35 48
        $walker = $document->walker();
36
37 48
        while ($event = $walker->next()) {
38 48
            $node = $event->getNode();
39 48
            if ($node instanceof FootnoteRef && $event->isEntering() && null !== $text = $node->getContent()) {
40
                // Anonymous footnote needs to create a footnote from its content
41 18
                $existingReference = $node->getReference();
42 18
                $reference = new Reference(
43 18
                    $existingReference->getLabel(),
44 18
                    '#' . $this->config->get('footnote/ref_id_prefix', 'fnref:') . $existingReference->getLabel(),
45 18
                    $existingReference->getTitle()
46
                );
47 18
                $footnote = new Footnote($reference);
48 18
                $footnote->addBackref(new FootnoteBackref($reference));
49 18
                $paragraph = new Paragraph();
50 18
                $paragraph->appendChild(new Text($text));
51 18
                $footnote->appendChild($paragraph);
52 18
                $document->appendChild($footnote);
53
            }
54
        }
55 48
    }
56
57 48
    public function setConfiguration(ConfigurationInterface $config): void
58
    {
59 48
        $this->config = $config;
60 48
    }
61
}
62