Completed
Push — master ( 3ff1a5...606db1 )
by Colin
28:59 queued 27:36
created

ReferenceMap::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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 2835
    public function __construct()
41
    {
42 2835
        $this->normalizer = new TextNormalizer();
43 2835
    }
44
45 288
    public function add(ReferenceInterface $reference): void
46
    {
47
        // Normalize the key
48 288
        $key = $this->normalizer->normalize($reference->getLabel());
49
        // Store the reference
50 288
        $this->references[$key] = $reference;
51 288
    }
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 339
    public function get(string $label): ?ReferenceInterface
61
    {
62 339
        $label = $this->normalizer->normalize($label);
63
64 339
        if (! isset($this->references[$label])) {
65 117
            return null;
66
        }
67
68 243
        return $this->references[$label];
69
    }
70
71
    /**
72
     * @return \Traversable<string, ReferenceInterface>
0 ignored issues
show
Documentation introduced by
The doc-type \Traversable<string, could not be parsed: Expected "|" or "end of type", but got "<" at position 12. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
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