Passed
Push — node-iterator ( da69c7...1da87a )
by Colin
40:48 queued 07:54
created

NumberFootnotesListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 27
dl 0
loc 51
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A onDocumentParsed() 0 49 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\Event\DocumentParsedEvent;
18
use League\CommonMark\Extension\Footnote\Node\FootnoteRef;
19
use League\CommonMark\Reference\Reference;
20
21
final class NumberFootnotesListener
22
{
23
    public function onDocumentParsed(DocumentParsedEvent $event): void
24
    {
25
        $document     = $event->getDocument();
26
        $nextCounter  = 1;
27
        $usedLabels   = [];
28
        $usedCounters = [];
29
30
        foreach ($document->iterator() as $node) {
31
            if (! $node instanceof FootnoteRef) {
32
                continue;
33
            }
34
35
            $existingReference   = $node->getReference();
36
            $label               = $existingReference->getLabel();
37
            $counter             = $nextCounter;
38
            $canIncrementCounter = true;
39
40
            if (\array_key_exists($label, $usedLabels)) {
41
                /*
42
                 * Reference is used again, we need to point
43
                 * to the same footnote. But with a different ID
44
                 */
45
                $counter             = $usedCounters[$label];
46
                $label              .= '__' . ++$usedLabels[$label];
47
                $canIncrementCounter = false;
48
            }
49
50
            // rewrite reference title to use a numeric link
51
            $newReference = new Reference(
52
                $label,
53
                $existingReference->getDestination(),
54
                (string) $counter
55
            );
56
57
            // Override reference with numeric link
58
            $node->setReference($newReference);
59
            $document->getReferenceMap()->add($newReference);
60
61
            /*
62
             * Store created references in document for
63
             * creating FootnoteBackrefs
64
             */
65
            $document->data->append($existingReference->getDestination(), $newReference);
66
67
            $usedLabels[$label]   = 1;
68
            $usedCounters[$label] = $nextCounter;
69
70
            if ($canIncrementCounter) {
71
                $nextCounter++;
72
            }
73
        }
74
    }
75
}
76