Passed
Push — 2.0 ( b7ed7b...934f6b )
by Colin
35:11 queued 31:18
created

CloseBracketParser   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Test Coverage

Coverage 98.73%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 22
eloc 78
c 1
b 0
f 0
dl 0
loc 162
ccs 78
cts 79
cp 0.9873
rs 10

6 Methods

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