Completed
Push — master ( 103c69...214519 )
by Colin
35:28 queued 34:02
created

NumberFootnotesListener   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 8
dl 0
loc 65
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B onDocumentParsed() 0 62 7
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 42
    public function onDocumentParsed(DocumentParsedEvent $event): void
24
    {
25 42
        $document     = $event->getDocument();
26 42
        $walker       = $document->walker();
27 42
        $nextCounter  = 1;
28 42
        $usedLabels   = [];
29 42
        $usedCounters = [];
30
31 42
        while ($event = $walker->next()) {
32 42
            if (! $event->isEntering()) {
33 42
                continue;
34
            }
35
36 42
            $node = $event->getNode();
37 42
            if (! $node instanceof FootnoteRef) {
38 42
                continue;
39
            }
40
41 36
            $existingReference   = $node->getReference();
42 36
            $label               = $existingReference->getLabel();
43 36
            $counter             = $nextCounter;
44 36
            $canIncrementCounter = true;
45
46 36
            if (\array_key_exists($label, $usedLabels)) {
47
                /*
48
                 * Reference is used again, we need to point
49
                 * to the same footnote. But with a different ID
50
                 */
51 6
                $counter             = $usedCounters[$label];
52 6
                $label               = $label . '__' . ++$usedLabels[$label];
53 6
                $canIncrementCounter = false;
54
            }
55
56
            // rewrite reference title to use a numeric link
57 36
            $newReference = new Reference(
58
                $label,
59 36
                $existingReference->getDestination(),
60 36
                (string) $counter
61
            );
62
63
            // Override reference with numeric link
64 36
            $node->setReference($newReference);
65 36
            $document->getReferenceMap()->add($newReference);
66
67
            /*
68
             * Store created references in document for
69
             * creating FootnoteBackrefs
70
             */
71 36
            if ($document->getData($existingReference->getDestination(), false) === false) {
72 36
                $document->data[$existingReference->getDestination()] = [];
73
            }
74
75 36
            $document->data[$existingReference->getDestination()][] = $newReference;
76
77 36
            $usedLabels[$label]   = 1;
78 36
            $usedCounters[$label] = $nextCounter;
79
80 36
            if ($canIncrementCounter) {
81 36
                $nextCounter++;
82
            }
83
        }
84 42
    }
85
}
86