Completed
Push — master ( 7a6aee...e5c914 )
by Colin
52:42 queued 27:44
created

LinkParserHelper::parseLinkDestination()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 42
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 8

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 42
ccs 30
cts 30
cp 1
rs 5.3846
cc 8
eloc 27
nc 8
nop 1
crap 8
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
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::getInstance()->getLinkDestinationBracesRegex())) {
31
            // Chop off surrounding <..>:
32 12
            return UrlEncoder::unescapeAndEncode(
33 12
                RegexHelper::unescape(substr($res, 1, strlen($res) - 2))
34 8
            );
35
        }
36
37 375
        $oldState = $cursor->saveState();
38 375
        $openParens = 0;
39 375
        while (($c = $cursor->getCharacter()) !== null) {
40 372
            if ($c === '\\') {
41 21
                $cursor->advanceBy(2);
42 21
            } elseif ($c === '(') {
43 21
                $cursor->advance();
44 14
                $openParens++;
45 372
            } elseif ($c === ')') {
46 18
                if ($openParens < 1) {
47 18
                    break;
48 372
                }
49 117
50 114
                $cursor->advance();
51
                $openParens--;
52 18
            } elseif (preg_match(RegexHelper::REGEX_WHITESPACE_CHAR, $c)) {
53 18
                break;
54
            } else {
55 369
                $cursor->advance();
56 168
            }
57
        }
58 369
59
        $newPos = $cursor->getPosition();
60 246
        $cursor->restoreState($oldState);
61
62 375
        $cursor->advanceBy($newPos - $cursor->getPosition());
63 375
64
        $res = $cursor->getPreviousText();
65 375
66
        return UrlEncoder::unescapeAndEncode(
67 375
            RegexHelper::unescape($res)
68
        );
69 375
    }
70 375
71 250
    /**
72
     * @param Cursor $cursor
73
     *
74
     * @return int
75
     */
76
    public static function parseLinkLabel(Cursor $cursor)
77
    {
78
        $escapedChar = RegexHelper::getInstance()->getPartialRegex(RegexHelper::ESCAPED_CHAR);
79 393
        $match = $cursor->match('/^\[(?:[^\\\\\[\]]|' . $escapedChar . '|\\\\)*\]/');
80
        $length = mb_strlen($match, 'utf-8');
81 393
82 393
        if ($match === null || $length > 1001 || preg_match('/[^\\\\]\\\\\]$/', $match)) {
83 393
            return 0;
84
        }
85 393
86 198
        return $length;
87
    }
88
89 366
    /**
90
     * Attempt to parse link title (sans quotes)
91
     *
92
     * @param Cursor $cursor
93
     *
94
     * @return null|string The string, or null if no match
95
     */
96
    public static function parseLinkTitle(Cursor $cursor)
97
    {
98
        if ($title = $cursor->match(RegexHelper::getInstance()->getLinkTitleRegex())) {
99 270
            // Chop off quotes from title and unescape
100
            return RegexHelper::unescape(substr($title, 1, strlen($title) - 2));
101 270
        }
102
    }
103
}
104