Completed
Push — master ( 990cee...3d5add )
by Colin
13s queued 11s
created

addStaggeredDelimiterProcessorForChar()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2.0116
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
 * Additional emphasis processing code based on commonmark-java (https://github.com/atlassian/commonmark-java)
12
 *  - (c) Atlassian Pty Ltd
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace League\CommonMark\Delimiter\Processor;
19
20
final class DelimiterProcessorCollection implements DelimiterProcessorCollectionInterface
21
{
22
    /** @var array<string,DelimiterProcessorInterface>|DelimiterProcessorInterface[] */
23
    private $processorsByChar = [];
24
25 2079
    public function add(DelimiterProcessorInterface $processor)
26
    {
27 2079
        $opening = $processor->getOpeningCharacter();
28 2079
        $closing = $processor->getClosingCharacter();
29
30 2079
        if ($opening === $closing) {
31 2073
            $old = $this->processorsByChar[$opening] ?? null;
32 2073
            if ($old !== null && $old->getOpeningCharacter() === $old->getClosingCharacter()) {
33 6
                $this->addStaggeredDelimiterProcessorForChar($opening, $old, $processor);
34
            } else {
35 2073
                $this->addDelimiterProcessorForChar($opening, $processor);
36
            }
37
        } else {
38 39
            $this->addDelimiterProcessorForChar($opening, $processor);
39 39
            $this->addDelimiterProcessorForChar($closing, $processor);
40
        }
41 2079
    }
42
43 1860
    public function getDelimiterProcessor(string $char): ?DelimiterProcessorInterface
44
    {
45 1860
        return $this->processorsByChar[$char] ?? null;
46
    }
47
48 2148
    public function getDelimiterCharacters(): array
49
    {
50 2148
        return array_keys($this->processorsByChar);
51
    }
52
53 2079
    private function addDelimiterProcessorForChar(string $delimiterChar, DelimiterProcessorInterface $processor)
54
    {
55 2079
        if (isset($this->processorsByChar[$delimiterChar])) {
56 3
            throw new \InvalidArgumentException(sprintf('Delim processor for character "%s" already exists', $processor->getOpeningCharacter()));
57
        }
58
59 2079
        $this->processorsByChar[$delimiterChar] = $processor;
60 2079
    }
61
62 6
    private function addStaggeredDelimiterProcessorForChar(string $opening, DelimiterProcessorInterface $old, DelimiterProcessorInterface $new): void
63
    {
64 6
        if ($old instanceof StaggeredDelimiterProcessor) {
65
            $s = $old;
66
        } else {
67 6
            $s = new StaggeredDelimiterProcessor($opening, $old);
68
        }
69
70 6
        $s->add($new);
71 3
        $this->processorsByChar[$opening] = $s;
72 3
    }
73
}
74