StaggeredDelimiterProcessor   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 90
ccs 26
cts 28
cp 0.9286
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getOpeningCharacter() 0 4 1
A getClosingCharacter() 0 4 1
A getMinLength() 0 4 1
A add() 0 13 2
A getDelimiterUse() 0 4 1
A process() 0 4 1
A findProcessor() 0 15 3
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * Additional emphasis processing code based on commonmark-java (https://github.com/atlassian/commonmark-java)
9
 *  - (c) Atlassian Pty Ltd
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\Delimiter\Processor;
16
17
use League\CommonMark\Delimiter\DelimiterInterface;
18
use League\CommonMark\Inline\Element\AbstractStringContainer;
19
20
/**
21
 * An implementation of DelimiterProcessorInterface that dispatches all calls to two or more other DelimiterProcessors
22
 * depending on the length of the delimiter run. All child DelimiterProcessors must have different minimum
23
 * lengths. A given delimiter run is dispatched to the child with the largest acceptable minimum length. If no
24
 * child is applicable, the one with the largest minimum length is chosen.
25
 *
26
 * @internal
27
 */
28
final class StaggeredDelimiterProcessor implements DelimiterProcessorInterface
29
{
30
    private $delimiterChar;
31
32
    private $minLength = 0;
33
34
    /** @var array<int, DelimiterProcessorInterface>|DelimiterProcessorInterface[] */
35
    private $processors = []; // keyed by minLength in reverse order
36
37 6
    public function __construct(string $char, DelimiterProcessorInterface $processor)
38
    {
39 6
        $this->delimiterChar = $char;
40 6
        $this->add($processor);
41 6
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 3
    public function getOpeningCharacter(): string
47
    {
48 3
        return $this->delimiterChar;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 3
    public function getClosingCharacter(): string
55
    {
56 3
        return $this->delimiterChar;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 3
    public function getMinLength(): int
63
    {
64 3
        return $this->minLength;
65
    }
66
67
    /**
68
     * Adds the given processor to this staggered delimiter processor
69
     *
70
     * @param DelimiterProcessorInterface $processor
71
     */
72 6
    public function add(DelimiterProcessorInterface $processor)
73
    {
74 6
        $len = $processor->getMinLength();
75
76 6
        if (isset($this->processors[$len])) {
77 3
            throw new \InvalidArgumentException(sprintf('Cannot add two delimiter processors for char "%s" and minimum length %d', $this->delimiterChar, $len));
78
        }
79
80 6
        $this->processors[$len] = $processor;
81 6
        krsort($this->processors);
82
83 6
        $this->minLength = min($this->minLength, $len);
84 6
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 3
    public function getDelimiterUse(DelimiterInterface $opener, DelimiterInterface $closer): int
90
    {
91 3
        return $this->findProcessor($opener->getLength())->getDelimiterUse($opener, $closer);
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 3
    public function process(AbstractStringContainer $opener, AbstractStringContainer $closer, int $delimiterUse)
98
    {
99 3
        return $this->findProcessor($delimiterUse)->process($opener, $closer, $delimiterUse);
100
    }
101
102 3
    private function findProcessor(int $len): DelimiterProcessorInterface
103
    {
104
        // Find the "longest" processor which can handle this length
105 3
        foreach ($this->processors as $processor) {
106 3
            if ($processor->getMinLength() <= $len) {
107 3
                return $processor;
108
            }
109
        }
110
111
        // Just use the first one in our list
112
        /** @var DelimiterProcessorInterface $first */
113
        $first = reset($this->processors);
114
115
        return $first;
116
    }
117
}
118