DelimiterProcessorCollection   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 96.3%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 54
ccs 26
cts 27
cp 0.963
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 17 4
A getDelimiterProcessor() 0 4 1
A getDelimiterCharacters() 0 4 1
A addDelimiterProcessorForChar() 0 8 2
A addStaggeredDelimiterProcessorForChar() 0 11 2
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 2076
    public function add(DelimiterProcessorInterface $processor)
26
    {
27 2076
        $opening = $processor->getOpeningCharacter();
28 2076
        $closing = $processor->getClosingCharacter();
29
30 2076
        if ($opening === $closing) {
31 2070
            $old = $this->processorsByChar[$opening] ?? null;
32 2070
            if ($old !== null && $old->getOpeningCharacter() === $old->getClosingCharacter()) {
33 6
                $this->addStaggeredDelimiterProcessorForChar($opening, $old, $processor);
34
            } else {
35 2070
                $this->addDelimiterProcessorForChar($opening, $processor);
36
            }
37
        } else {
38 39
            $this->addDelimiterProcessorForChar($opening, $processor);
39 39
            $this->addDelimiterProcessorForChar($closing, $processor);
40
        }
41 2076
    }
42
43 1857
    public function getDelimiterProcessor(string $char): ?DelimiterProcessorInterface
44
    {
45 1857
        return $this->processorsByChar[$char] ?? null;
46
    }
47
48 2139
    public function getDelimiterCharacters(): array
49
    {
50 2139
        return array_keys($this->processorsByChar);
51
    }
52
53 2076
    private function addDelimiterProcessorForChar(string $delimiterChar, DelimiterProcessorInterface $processor)
54
    {
55 2076
        if (isset($this->processorsByChar[$delimiterChar])) {
56 3
            throw new \InvalidArgumentException(sprintf('Delim processor for character "%s" already exists', $processor->getOpeningCharacter()));
57
        }
58
59 2076
        $this->processorsByChar[$delimiterChar] = $processor;
60 2076
    }
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