Completed
Push — master ( ea1f65...22f692 )
by Colin
12s
created

LinkParserHelper   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 15
c 2
b 0
f 0
lcom 0
cbo 3
dl 0
loc 84
ccs 39
cts 39
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A parseLinkLabel() 0 11 4
A parseLinkTitle() 0 7 2
D parseLinkDestination() 0 42 9
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)
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 4
            );
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 168
                break;
54
            } else {
55 369
                $cursor->advance();
56
            }
57 123
        }
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 125
        );
69
    }
70
71
    /**
72
     * @param Cursor $cursor
73
     *
74
     * @return int
75
     */
76 393
    public static function parseLinkLabel(Cursor $cursor)
77
    {
78 393
        $match = $cursor->match('/^\[(?:[^\\\\\[\]]|' . RegexHelper::PARTIAL_ESCAPED_CHAR . '|\\\\)*\]/');
79 393
        $length = mb_strlen($match, 'utf-8');
80
81 393
        if ($match === null || $length > 1001 || preg_match('/[^\\\\]\\\\\]$/', $match)) {
82 198
            return 0;
83
        }
84
85 366
        return $length;
86
    }
87
88
    /**
89
     * Attempt to parse link title (sans quotes)
90
     *
91
     * @param Cursor $cursor
92
     *
93
     * @return null|string The string, or null if no match
94
     */
95 270
    public static function parseLinkTitle(Cursor $cursor)
96
    {
97 270
        if ($title = $cursor->match('/' . RegexHelper::PARTIAL_LINK_TITLE . '/')) {
98
            // Chop off quotes from title and unescape
99 138
            return RegexHelper::unescape(substr($title, 1, -1));
100
        }
101 135
    }
102
}
103