ReferenceMap   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
eloc 15
dl 0
loc 60
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 9 2
A __construct() 0 3 1
A count() 0 3 1
A getIterator() 0 4 2
A contains() 0 5 1
A add() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
11
 *  - (c) John MacFarlane
12
 *
13
 * For the full copyright and license information, please view the LICENSE
14
 * file that was distributed with this source code.
15
 */
16
17
namespace League\CommonMark\Reference;
18
19
use League\CommonMark\Normalizer\TextNormalizer;
20
21
/**
22
 * A collection of references, indexed by label
23
 */
24
final class ReferenceMap implements ReferenceMapInterface
25
{
26
    /**
27
     * @var TextNormalizer
28
     *
29
     * @psalm-readonly
30
     */
31
    private $normalizer;
32
33
    /**
34
     * @var array<string, ReferenceInterface>
35
     *
36
     * @psalm-readonly-allow-private-mutation
37
     */
38
    private $references = [];
39
40 3099
    public function __construct()
41
    {
42 3099
        $this->normalizer = new TextNormalizer();
43 3099
    }
44
45 348
    public function add(ReferenceInterface $reference): void
46
    {
47
        // Normalize the key
48 348
        $key = $this->normalizer->normalize($reference->getLabel());
49
        // Store the reference
50 348
        $this->references[$key] = $reference;
51 348
    }
52
53 246
    public function contains(string $label): bool
54
    {
55 246
        $label = $this->normalizer->normalize($label);
56
57 246
        return isset($this->references[$label]);
58
    }
59
60 405
    public function get(string $label): ?ReferenceInterface
61
    {
62 405
        $label = $this->normalizer->normalize($label);
63
64 405
        if (! isset($this->references[$label])) {
65 126
            return null;
66
        }
67
68 303
        return $this->references[$label];
69
    }
70
71
    /**
72
     * @return \Traversable<string, ReferenceInterface>
73
     */
74 3
    public function getIterator(): \Traversable
75
    {
76 3
        foreach ($this->references as $normalizedLabel => $reference) {
77 3
            yield $normalizedLabel => $reference;
78
        }
79 3
    }
80
81 6
    public function count(): int
82
    {
83 6
        return \count($this->references);
84
    }
85
}
86