CloseBracketParser   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Test Coverage

Coverage 98.72%

Importance

Changes 0
Metric Value
wmc 23
eloc 74
dl 0
loc 174
ccs 77
cts 78
cp 0.9872
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createInline() 0 7 2
A setEnvironment() 0 3 1
B parse() 0 55 6
A tryParseLink() 0 13 3
A getCharacters() 0 3 1
A tryParseInlineLinkAndTitle() 0 38 5
A tryParseReference() 0 25 5
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
 * 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\Extension\CommonMark\Parser\Inline;
18
19
use League\CommonMark\Delimiter\DelimiterInterface;
20
use League\CommonMark\Environment\EnvironmentAwareInterface;
21
use League\CommonMark\Environment\EnvironmentInterface;
22
use League\CommonMark\Extension\CommonMark\Node\Inline\AbstractWebResource;
23
use League\CommonMark\Extension\CommonMark\Node\Inline\Image;
24
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
25
use League\CommonMark\Node\Inline\AdjacentTextMerger;
26
use League\CommonMark\Parser\Cursor;
27
use League\CommonMark\Parser\Inline\InlineParserInterface;
28
use League\CommonMark\Parser\InlineParserContext;
29
use League\CommonMark\Reference\ReferenceInterface;
30
use League\CommonMark\Reference\ReferenceMapInterface;
31
use League\CommonMark\Util\LinkParserHelper;
32
use League\CommonMark\Util\RegexHelper;
33
34
final class CloseBracketParser implements InlineParserInterface, EnvironmentAwareInterface
35
{
36
    /**
37
     * @var EnvironmentInterface
38
     *
39
     * @psalm-readonly-allow-private-mutation
40
     */
41
    private $environment;
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 2832
    public function getCharacters(): array
47
    {
48 2832
        return [']'];
49
    }
50
51 486
    public function parse(InlineParserContext $inlineContext): bool
52
    {
53
        // Look through stack of delimiters for a [ or !
54 486
        $opener = $inlineContext->getDelimiterStack()->searchByCharacter(['[', '!']);
55 486
        if ($opener === null) {
56 12
            return false;
57
        }
58
59 477
        if (! $opener->isActive()) {
60
            // no matched opener; remove from emphasis stack
61 18
            $inlineContext->getDelimiterStack()->removeDelimiter($opener);
62
63 18
            return false;
64
        }
65
66 477
        $cursor = $inlineContext->getCursor();
67
68 477
        $startPos      = $cursor->getPosition();
69 477
        $previousState = $cursor->saveState();
70
71 477
        $cursor->advanceBy(1);
72
73
        // Check to see if we have a link/image
74 477
        if (! ($link = $this->tryParseLink($cursor, $inlineContext->getReferenceMap(), $opener, $startPos))) {
75
            // No match
76 120
            $inlineContext->getDelimiterStack()->removeDelimiter($opener); // Remove this opener from stack
77 120
            $cursor->restoreState($previousState);
78
79 120
            return false;
80
        }
81
82 387
        $isImage = $opener->getChar() === '!';
83
84 387
        $inline = $this->createInline($link['url'], $link['title'], $isImage);
85 387
        $opener->getInlineNode()->replaceWith($inline);
86 387
        while (($label = $inline->next()) !== null) {
87 384
            $inline->appendChild($label);
88
        }
89
90
        // Process delimiters such as emphasis inside link/image
91 387
        $delimiterStack = $inlineContext->getDelimiterStack();
92 387
        $stackBottom    = $opener->getPrevious();
93 387
        $delimiterStack->processDelimiters($stackBottom, $this->environment->getDelimiterProcessors());
94 387
        $delimiterStack->removeAll($stackBottom);
95
96
        // Merge any adjacent Text nodes together
97 387
        AdjacentTextMerger::mergeChildNodes($inline);
98
99
        // processEmphasis will remove this and later delimiters.
100
        // Now, for a link, we also remove earlier link openers (no links in links)
101 387
        if (! $isImage) {
102 330
            $inlineContext->getDelimiterStack()->removeEarlierMatches('[');
103
        }
104
105 387
        return true;
106
    }
107
108 2832
    public function setEnvironment(EnvironmentInterface $environment): void
109
    {
110 2832
        $this->environment = $environment;
111 2832
    }
112
113
    /**
114
     * @return array<string, string>|false
115
     */
116 477
    private function tryParseLink(Cursor $cursor, ReferenceMapInterface $referenceMap, DelimiterInterface $opener, int $startPos)
117
    {
118
        // Check to see if we have a link/image
119
        // Inline link?
120 477
        if ($result = $this->tryParseInlineLinkAndTitle($cursor)) {
121 192
            return $result;
122
        }
123
124 300
        if ($link = $this->tryParseReference($cursor, $referenceMap, $opener->getIndex(), $startPos)) {
125 201
            return ['url' => $link->getDestination(), 'title' => $link->getTitle()];
126
        }
127
128 120
        return false;
129
    }
130
131
    /**
132
     * @return array<string, string>|false
133
     */
134 477
    private function tryParseInlineLinkAndTitle(Cursor $cursor)
135
    {
136 477
        if ($cursor->getCharacter() !== '(') {
137 264
            return false;
138
        }
139
140 225
        $previousState = $cursor->saveState();
141
142 225
        $cursor->advanceBy(1);
143 225
        $cursor->advanceToNextNonSpaceOrNewline();
144 225
        if (($dest = LinkParserHelper::parseLinkDestination($cursor)) === null) {
145 9
            $cursor->restoreState($previousState);
146
147 9
            return false;
148
        }
149
150 219
        $cursor->advanceToNextNonSpaceOrNewline();
151 219
        $previousCharacter = $cursor->peek(-1);
152
        // We know from previous lines that we've advanced at least one space so far, so this next call should never be null
153
        \assert(\is_string($previousCharacter));
154
155 219
        $title = '';
156
        // make sure there's a space before the title:
157 219
        if (\preg_match(RegexHelper::REGEX_WHITESPACE_CHAR, $previousCharacter)) {
158 57
            $title = LinkParserHelper::parseLinkTitle($cursor) ?? '';
159
        }
160
161 219
        $cursor->advanceToNextNonSpaceOrNewline();
162
163 219
        if ($cursor->getCharacter() !== ')') {
164 33
            $cursor->restoreState($previousState);
165
166 33
            return false;
167
        }
168
169 192
        $cursor->advanceBy(1);
170
171 192
        return ['url' => $dest, 'title' => $title];
172
    }
173
174 300
    private function tryParseReference(Cursor $cursor, ReferenceMapInterface $referenceMap, ?int $openerIndex, int $startPos): ?ReferenceInterface
175
    {
176 300
        if ($openerIndex === null) {
177
            return null;
178
        }
179
180 300
        $savePos     = $cursor->saveState();
181 300
        $beforeLabel = $cursor->getPosition();
182 300
        $n           = LinkParserHelper::parseLinkLabel($cursor);
183 300
        if ($n === 0 || $n === 2) {
184 249
            $start  = $openerIndex;
185 249
            $length = $startPos - $openerIndex;
186
        } else {
187 63
            $start  = $beforeLabel + 1;
188 63
            $length = $n - 2;
189
        }
190
191 300
        $referenceLabel = $cursor->getSubstring($start, $length);
192
193 300
        if ($n === 0) {
194
            // If shortcut reference link, rewind before spaces we skipped
195 225
            $cursor->restoreState($savePos);
196
        }
197
198 300
        return $referenceMap->get($referenceLabel);
199
    }
200
201 387
    private function createInline(string $url, string $title, bool $isImage): AbstractWebResource
202
    {
203 387
        if ($isImage) {
204 78
            return new Image($url, null, $title);
205
        }
206
207 330
        return new Link($url, null, $title);
208
    }
209
}
210