Passed
Push — 2.3 ( c49358...0d523d )
by Colin
12:44
created

LinkParserHelper::parseLinkDestination()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 1
dl 0
loc 20
ccs 11
cts 12
cp 0.9167
crap 4.0092
rs 9.9332
c 0
b 0
f 0
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\Util;
18
19
use League\CommonMark\Parser\Cursor;
20
21
/**
22
 * @psalm-immutable
23
 */
24
final class LinkParserHelper
25
{
26
    /**
27
     * Attempt to parse link destination
28
     *
29
     * @return string|null The string, or null if no match
30
     */
31 382
    public static function parseLinkDestination(Cursor $cursor): ?string
32
    {
33 382
        if ($res = $cursor->match(RegexHelper::REGEX_LINK_DESTINATION_BRACES)) {
34
            // Chop off surrounding <..>:
35 26
            return UrlEncoder::unescapeAndEncode(
36 26
                RegexHelper::unescape(\substr($res, 1, -1))
37 26
            );
38
        }
39
40 358
        if ($cursor->getCurrentCharacter() === '<') {
41 10
            return null;
42
        }
43
44 350
        $destination = self::manuallyParseLinkDestination($cursor);
45 350
        if ($destination === null) {
46
            return null;
47
        }
48
49 350
        return UrlEncoder::unescapeAndEncode(
50 350
            RegexHelper::unescape($destination)
51 350
        );
52
    }
53
54 222
    public static function parseLinkLabel(Cursor $cursor): int
55
    {
56 222
        $match = $cursor->match('/^\[(?:[^\\\\\[\]]|\\\\.){0,1000}\]/');
57 222
        if ($match === null) {
58 170
            return 0;
59
        }
60
61 60
        $length = \mb_strlen($match, 'UTF-8');
62
63 60
        if ($length > 1001) {
64
            return 0;
65
        }
66
67 60
        return $length;
68
    }
69
70 328
    public static function parsePartialLinkLabel(Cursor $cursor): ?string
71
    {
72 328
        return $cursor->match('/^(?:[^\\\\\[\]]+|\\\\.?)*/');
73
    }
74
75
    /**
76
     * Attempt to parse link title (sans quotes)
77
     *
78
     * @return string|null The string, or null if no match
79
     */
80 46
    public static function parseLinkTitle(Cursor $cursor): ?string
81
    {
82 46
        if ($title = $cursor->match('/' . RegexHelper::PARTIAL_LINK_TITLE . '/')) {
83
            // Chop off quotes from title and unescape
84 22
            return RegexHelper::unescape(\substr($title, 1, -1));
85
        }
86
87 24
        return null;
88
    }
89
90 78
    public static function parsePartialLinkTitle(Cursor $cursor, string $endDelimiter): ?string
91
    {
92 78
        $endDelimiter = \preg_quote($endDelimiter, '/');
93 78
        $regex        = \sprintf('/(%s|[^%s\x00])*(?:%s)?/', RegexHelper::PARTIAL_ESCAPED_CHAR, $endDelimiter, $endDelimiter);
94 78
        if (($partialTitle = $cursor->match($regex)) === null) {
95
            return null;
96
        }
97
98 78
        return RegexHelper::unescape($partialTitle);
99
    }
100
101 350
    private static function manuallyParseLinkDestination(Cursor $cursor): ?string
102
    {
103 350
        $oldPosition = $cursor->getPosition();
104 350
        $oldState    = $cursor->saveState();
105
106 350
        $openParens = 0;
107 350
        while (($c = $cursor->getCurrentCharacter()) !== null) {
108 350
            if ($c === '\\' && ($peek = $cursor->peek()) !== null && RegexHelper::isEscapable($peek)) {
109 12
                $cursor->advanceBy(2);
110 350
            } elseif ($c === '(') {
111 12
                $cursor->advanceBy(1);
112 12
                $openParens++;
113 350
            } elseif ($c === ')') {
114 146
                if ($openParens < 1) {
115 142
                    break;
116
                }
117
118 12
                $cursor->advanceBy(1);
119 12
                $openParens--;
120 346
            } elseif (\preg_match(RegexHelper::REGEX_WHITESPACE_CHAR, $c)) {
121 124
                break;
122
            } else {
123 346
                $cursor->advanceBy(1);
124
            }
125
        }
126
127 350
        if ($openParens !== 0) {
128
            return null;
129
        }
130
131 350
        if ($cursor->getPosition() === $oldPosition && (! isset($c) || $c !== ')')) {
132
            return null;
133
        }
134
135 350
        $newPos = $cursor->getPosition();
136 350
        $cursor->restoreState($oldState);
137
138 350
        $cursor->advanceBy($newPos - $cursor->getPosition());
139
140 350
        return $cursor->getPreviousText();
141
    }
142
}
143