DelimiterStack   A
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Test Coverage

Coverage 98.94%

Importance

Changes 0
Metric Value
wmc 37
eloc 83
dl 0
loc 192
ccs 93
cts 94
cp 0.9894
rs 9.44
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A removeEarlierMatches() 0 9 3
C processDelimiters() 0 90 15
A removeDelimiterAndNode() 0 4 1
A searchByCharacter() 0 16 4
A removeAll() 0 4 3
A removeDelimitersBetween() 0 7 3
A findEarliest() 0 8 3
A push() 0 9 2
A removeDelimiter() 0 13 3
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
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
11
 *  - (c) John MacFarlane
12
 *
13
 * Additional emphasis processing code based on commonmark-java (https://github.com/atlassian/commonmark-java)
14
 *  - (c) Atlassian Pty Ltd
15
 *
16
 * For the full copyright and license information, please view the LICENSE
17
 * file that was distributed with this source code.
18
 */
19
20
namespace League\CommonMark\Delimiter;
21
22
use League\CommonMark\Delimiter\Processor\DelimiterProcessorCollection;
23
use League\CommonMark\Node\Inline\AdjacentTextMerger;
24
25
final class DelimiterStack
26
{
27
    /**
28
     * @var DelimiterInterface|null
29
     *
30
     * @psalm-readonly-allow-private-mutation
31
     */
32
    private $top;
33
34 1056
    public function push(DelimiterInterface $newDelimiter): void
35
    {
36 1056
        $newDelimiter->setPrevious($this->top);
37
38 1056
        if ($this->top !== null) {
39 585
            $this->top->setNext($newDelimiter);
40
        }
41
42 1056
        $this->top = $newDelimiter;
43 1056
    }
44
45 2535
    private function findEarliest(?DelimiterInterface $stackBottom = null): ?DelimiterInterface
46
    {
47 2535
        $delimiter = $this->top;
48 2535
        while ($delimiter !== null && $delimiter->getPrevious() !== $stackBottom) {
49 543
            $delimiter = $delimiter->getPrevious();
50
        }
51
52 2535
        return $delimiter;
53
    }
54
55 1056
    public function removeDelimiter(DelimiterInterface $delimiter): void
56
    {
57 1056
        if ($delimiter->getPrevious() !== null) {
58
            /** @psalm-suppress PossiblyNullReference */
59 315
            $delimiter->getPrevious()->setNext($delimiter->getNext());
60
        }
61
62 1056
        if ($delimiter->getNext() === null) {
63
            // top of stack
64 1056
            $this->top = $delimiter->getPrevious();
65
        } else {
66
            /** @psalm-suppress PossiblyNullReference */
67 471
            $delimiter->getNext()->setPrevious($delimiter->getPrevious());
68
        }
69 1056
    }
70
71 480
    private function removeDelimiterAndNode(DelimiterInterface $delimiter): void
72
    {
73 480
        $delimiter->getInlineNode()->detach();
74 480
        $this->removeDelimiter($delimiter);
75 480
    }
76
77 480
    private function removeDelimitersBetween(DelimiterInterface $opener, DelimiterInterface $closer): void
78
    {
79 480
        $delimiter = $closer->getPrevious();
80 480
        while ($delimiter !== null && $delimiter !== $opener) {
81 24
            $previous = $delimiter->getPrevious();
82 24
            $this->removeDelimiter($delimiter);
83 24
            $delimiter = $previous;
84
        }
85 480
    }
86
87 2535
    public function removeAll(?DelimiterInterface $stackBottom = null): void
88
    {
89 2535
        while ($this->top && $this->top !== $stackBottom) {
90 543
            $this->removeDelimiter($this->top);
91
        }
92 2535
    }
93
94 330
    public function removeEarlierMatches(string $character): void
95
    {
96 330
        $opener = $this->top;
97 330
        while ($opener !== null) {
98 60
            if ($opener->getChar() === $character) {
99 24
                $opener->setActive(false);
100
            }
101
102 60
            $opener = $opener->getPrevious();
103
        }
104 330
    }
105
106
    /**
107
     * @param string|string[] $characters
108
     */
109 486
    public function searchByCharacter($characters): ?DelimiterInterface
110
    {
111 486
        if (! \is_array($characters)) {
112
            $characters = [$characters];
113
        }
114
115 486
        $opener = $this->top;
116 486
        while ($opener !== null) {
117 480
            if (\in_array($opener->getChar(), $characters, true)) {
118 477
                break;
119
            }
120
121 72
            $opener = $opener->getPrevious();
122
        }
123
124 486
        return $opener;
125
    }
126
127 2535
    public function processDelimiters(?DelimiterInterface $stackBottom, DelimiterProcessorCollection $processors): void
128
    {
129 2535
        $openersBottom = [];
130
131
        // Find first closer above stackBottom
132 2535
        $closer = $this->findEarliest($stackBottom);
133
134
        // Move forward, looking for closers, and handling each
135 2535
        while ($closer !== null) {
136 975
            $delimiterChar = $closer->getChar();
137
138 975
            $delimiterProcessor = $processors->getDelimiterProcessor($delimiterChar);
139 975
            if (! $closer->canClose() || $delimiterProcessor === null) {
140 888
                $closer = $closer->getNext();
141 888
                continue;
142
            }
143
144 576
            $openingDelimiterChar = $delimiterProcessor->getOpeningCharacter();
145
146 576
            $useDelims            = 0;
147 576
            $openerFound          = false;
148 576
            $potentialOpenerFound = false;
149 576
            $opener               = $closer->getPrevious();
150 576
            while ($opener !== null && $opener !== $stackBottom && $opener !== ($openersBottom[$delimiterChar] ?? null)) {
151 504
                if ($opener->canOpen() && $opener->getChar() === $openingDelimiterChar) {
152 483
                    $potentialOpenerFound = true;
153 483
                    $useDelims            = $delimiterProcessor->getDelimiterUse($opener, $closer);
154 483
                    if ($useDelims > 0) {
155 480
                        $openerFound = true;
156 480
                        break;
157
                    }
158
                }
159
160 57
                $opener = $opener->getPrevious();
161
            }
162
163 576
            if (! $openerFound) {
164 177
                if (! $potentialOpenerFound) {
165
                    // Only do this when we didn't even have a potential
166
                    // opener (one that matches the character and can open).
167
                    // If an opener was rejected because of the number of
168
                    // delimiters (e.g. because of the "multiple of 3"
169
                    // Set lower bound for future searches for openersrule),
170
                    // we want to consider it next time because the number
171
                    // of delimiters can change as we continue processing.
172 162
                    $openersBottom[$delimiterChar] = $closer->getPrevious();
173 162
                    if (! $closer->canOpen()) {
174
                        // We can remove a closer that can't be an opener,
175
                        // once we've seen there's no matching opener.
176 120
                        $this->removeDelimiter($closer);
177
                    }
178
                }
179
180 177
                $closer = $closer->getNext();
181 177
                continue;
182
            }
183
184
            \assert($opener !== null);
185
186 480
            $openerNode = $opener->getInlineNode();
187 480
            $closerNode = $closer->getInlineNode();
188
189
            // Remove number of used delimiters from stack and inline nodes.
190 480
            $opener->setLength($opener->getLength() - $useDelims);
191 480
            $closer->setLength($closer->getLength() - $useDelims);
192
193 480
            $openerNode->setLiteral(\substr($openerNode->getLiteral(), 0, -$useDelims));
194 480
            $closerNode->setLiteral(\substr($closerNode->getLiteral(), 0, -$useDelims));
195
196 480
            $this->removeDelimitersBetween($opener, $closer);
197
            // The delimiter processor can re-parent the nodes between opener and closer,
198
            // so make sure they're contiguous already. Exclusive because we want to keep opener/closer themselves.
199 480
            AdjacentTextMerger::mergeTextNodesBetweenExclusive($openerNode, $closerNode);
200 480
            $delimiterProcessor->process($openerNode, $closerNode, $useDelims);
201
202
            // No delimiter characters left to process, so we can remove delimiter and the now empty node.
203 480
            if ($opener->getLength() === 0) {
204 453
                $this->removeDelimiterAndNode($opener);
205
            }
206
207
            // phpcs:disable SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed
208 480
            if ($closer->getLength() === 0) {
209 453
                $next = $closer->getNext();
210 453
                $this->removeDelimiterAndNode($closer);
211 453
                $closer = $next;
212
            }
213
        }
214
215
        // Remove all delimiters
216 2535
        $this->removeAll($stackBottom);
217 2535
    }
218
}
219