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

NumberFootnotesListener::onDocumentParsed()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 63
ccs 34
cts 34
cp 1
rs 7.8739
c 0
b 0
f 0
cc 7
nc 11
nop 1
crap 7

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
38 42
            if (!$node instanceof FootnoteRef) {
39 42
                continue;
40
            }
41
42 36
            $existingReference = $node->getReference();
43 36
            $label = $existingReference->getLabel();
44 36
            $counter = $nextCounter;
45 36
            $canIncrementCounter = true;
46
47 36
            if (\array_key_exists($label, $usedLabels)) {
48
                /*
49
                 * Reference is used again, we need to point
50
                 * to the same footnote. But with a different ID
51
                 */
52 6
                $counter = $usedCounters[$label];
53 6
                $label = $label . '__' . ++$usedLabels[$label];
54 6
                $canIncrementCounter = false;
55
            }
56
57
            // rewrite reference title to use a numeric link
58 36
            $newReference = new Reference(
59 36
                $label,
60 36
                $existingReference->getDestination(),
61 36
                (string) $counter
62
            );
63
64
            // Override reference with numeric link
65 36
            $node->setReference($newReference);
66 36
            $document->getReferenceMap()->addReference($newReference);
67
68
            /*
69
             * Store created references in document for
70
             * creating FootnoteBackrefs
71
             */
72 36
            if (false === $document->getData($existingReference->getDestination(), false)) {
73 36
                $document->data[$existingReference->getDestination()] = [];
74
            }
75
76 36
            $document->data[$existingReference->getDestination()][] = $newReference;
77
78 36
            $usedLabels[$label] = 1;
79 36
            $usedCounters[$label] = $nextCounter;
80
81 36
            if ($canIncrementCounter) {
82 36
                $nextCounter++;
83
            }
84
        }
85 42
    }
86
}
87