Completed
Push — master ( da0d4f...97e8b3 )
by Colin
03:00
created

ReferenceMap   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 58
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addReference() 0 7 1
A contains() 0 6 1
A getReference() 0 10 2
A listReferences() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
9
 *  - (c) John MacFarlane
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace League\CommonMark\Reference;
16
17
/**
18
 * A collection of references, indexed by label
19
 */
20
class ReferenceMap
21
{
22
    /**
23
     * @var Reference[]
24
     */
25
    protected $references = [];
26
27
    /**
28
     * @param Reference $reference
29
     *
30
     * @return $this
31
     */
32 243
    public function addReference(Reference $reference)
33
    {
34 243
        $key = Reference::normalizeReference($reference->getLabel());
35 243
        $this->references[$key] = $reference;
36
37 243
        return $this;
38
    }
39
40
    /**
41
     * @param string $label
42
     *
43
     * @return bool
44
     */
45 240
    public function contains(string $label): bool
46
    {
47 240
        $label = Reference::normalizeReference($label);
48
49 240
        return isset($this->references[$label]);
50
    }
51
52
    /**
53
     * @param string $label
54
     *
55
     * @return Reference|null
56
     */
57 294
    public function getReference(string $label): ?Reference
58
    {
59 294
        $label = Reference::normalizeReference($label);
60
61 294
        if (!isset($this->references[$label])) {
62 111
            return null;
63
        }
64
65 204
        return $this->references[$label];
66
    }
67
68
    /**
69
     * Lists all registered references.
70
     *
71
     * @return Reference[]
72
     */
73 6
    public function listReferences(): iterable
74
    {
75 6
        return \array_values($this->references);
76
    }
77
}
78