StaggeredDelimiterProcessor::getDelimiterUse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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