LinkParserHelper   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 97.37%

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 3
dl 0
loc 90
ccs 37
cts 38
cp 0.9737
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A parseLinkLabel() 0 15 4
B parseLinkDestination() 0 42 9
A parseLinkTitle() 0 9 2
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
9
 *  - (c) John MacFarlane
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace League\CommonMark\Util;
16
17
use League\CommonMark\Cursor;
18
19
final class LinkParserHelper
20
{
21
    /**
22
     * Attempt to parse link destination
23
     *
24
     * @param Cursor $cursor
25
     *
26
     * @return null|string The string, or null if no match
27
     */
28 387
    public static function parseLinkDestination(Cursor $cursor): ?string
29
    {
30 387
        if ($res = $cursor->match(RegexHelper::REGEX_LINK_DESTINATION_BRACES)) {
31
            // Chop off surrounding <..>:
32 12
            return UrlEncoder::unescapeAndEncode(
33 12
                RegexHelper::unescape(\substr($res, 1, -1))
34
            );
35
        }
36
37 375
        $oldState = $cursor->saveState();
38 375
        $openParens = 0;
39 375
        while (($c = $cursor->getCharacter()) !== null) {
40 372
            if ($c === '\\' && RegexHelper::isEscapable($cursor->peek())) {
41 18
                $cursor->advanceBy(2);
42 372
            } elseif ($c === '(') {
43 18
                $cursor->advance();
44 18
                $openParens++;
45 372
            } elseif ($c === ')') {
46 117
                if ($openParens < 1) {
47 114
                    break;
48
                }
49
50 18
                $cursor->advance();
51 18
                $openParens--;
52 369
            } elseif (\preg_match(RegexHelper::REGEX_WHITESPACE_CHAR, $c)) {
53 174
                break;
54
            } else {
55 369
                $cursor->advance();
56
            }
57
        }
58
59 375
        $newPos = $cursor->getPosition();
60 375
        $cursor->restoreState($oldState);
61
62 375
        $cursor->advanceBy($newPos - $cursor->getPosition());
63
64 375
        $res = $cursor->getPreviousText();
65
66 375
        return UrlEncoder::unescapeAndEncode(
67 375
            RegexHelper::unescape($res)
68
        );
69
    }
70
71
    /**
72
     * @param Cursor $cursor
73
     *
74
     * @return int
75
     */
76 393
    public static function parseLinkLabel(Cursor $cursor): int
77
    {
78 393
        $match = $cursor->match('/^\[(?:[^\\\\\[\]]|' . RegexHelper::PARTIAL_ESCAPED_CHAR . '|\\\\)*\]/');
79 393
        if ($match === null || \preg_match('/[^\\\\]\\\\\]$/', $match)) {
80 204
            return 0;
81
        }
82
83 366
        $length = \mb_strlen($match, 'utf-8');
84
85 366
        if ($length > 1001) {
86
            return 0;
87
        }
88
89 366
        return $length;
90
    }
91
92
    /**
93
     * Attempt to parse link title (sans quotes)
94
     *
95
     * @param Cursor $cursor
96
     *
97
     * @return null|string The string, or null if no match
98
     */
99 276
    public static function parseLinkTitle(Cursor $cursor): ?string
100
    {
101 276
        if ($title = $cursor->match('/' . RegexHelper::PARTIAL_LINK_TITLE . '/')) {
102
            // Chop off quotes from title and unescape
103 138
            return RegexHelper::unescape(substr($title, 1, -1));
104
        }
105
106 141
        return null;
107
    }
108
}
109