Completed
Push — master ( 0b6d3a...3ff1a5 )
by Colin
31:14 queued 11s
created

ReferenceMap::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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 288
    private $normalizer;
32
33
    /**
34 288
     * @var array<string, ReferenceInterface>
35
     *
36 288
     * @psalm-readonly-allow-private-mutation
37 288
     */
38
    private $references = [];
39 246
40
    public function __construct()
41 246
    {
42
        $this->normalizer = new TextNormalizer();
43 246
    }
44
45
    public function add(ReferenceInterface $reference): void
46 339
    {
47
        // Normalize the key
48 339
        $key = $this->normalizer->normalize($reference->getLabel());
49
        // Store the reference
50 339
        $this->references[$key] = $reference;
51 117
    }
52
53
    public function contains(string $label): bool
54 243
    {
55
        $label = $this->normalizer->normalize($label);
56
57
        return isset($this->references[$label]);
58
    }
59
60 3
    public function get(string $label): ?ReferenceInterface
61
    {
62 3
        $label = $this->normalizer->normalize($label);
63 3
64
        if (! isset($this->references[$label])) {
65 3
            return null;
66
        }
67 6
68
        return $this->references[$label];
69 6
    }
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
    public function getIterator(): \Traversable
75
    {
76
        foreach ($this->references as $normalizedLabel => $reference) {
77
            yield $normalizedLabel => $reference;
78
        }
79
    }
80
81
    public function count(): int
82
    {
83
        return \count($this->references);
84
    }
85
}
86