Completed
Push — master ( 742fba...7a6aee )
by Colin
14s
created

src/Util/LinkParserHelper.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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->advance();
42 21
                if ($cursor->getCharacter()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $cursor->getCharacter() of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
43 21
                    $cursor->advance();
44 14
                }
45 372
            } elseif ($c === '(') {
46 18
                $cursor->advance();
47 18
                $openParens++;
48 372
            } elseif ($c === ')') {
49 117
                if ($openParens < 1) {
50 114
                    break;
51
                } else {
52 18
                    $cursor->advance();
53 18
                    $openParens--;
54
                }
55 369
            } elseif (preg_match(RegexHelper::REGEX_WHITESPACE_CHAR, $c)) {
56 168
                break;
57
            } else {
58 369
                $cursor->advance();
59
            }
60 246
        }
61
62 375
        $newPos = $cursor->getPosition();
63 375
        $cursor->restoreState($oldState);
64
65 375
        $cursor->advanceBy($newPos - $cursor->getPosition());
66
67 375
        $res = $cursor->getPreviousText();
68
69 375
        return UrlEncoder::unescapeAndEncode(
70 375
            RegexHelper::unescape($res)
71 250
        );
72
    }
73
74
    /**
75
     * @param Cursor $cursor
76
     *
77
     * @return int
78
     */
79 393
    public static function parseLinkLabel(Cursor $cursor)
80
    {
81 393
        $escapedChar = RegexHelper::getInstance()->getPartialRegex(RegexHelper::ESCAPED_CHAR);
82 393
        $match = $cursor->match('/^\[(?:[^\\\\\[\]]|' . $escapedChar . '|\\\\)*\]/');
83 393
        $length = mb_strlen($match, 'utf-8');
84
85 393
        if ($match === null || $length > 1001 || preg_match('/[^\\\\]\\\\\]$/', $match)) {
86 198
            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 270
    public static function parseLinkTitle(Cursor $cursor)
100
    {
101 270
        if ($title = $cursor->match(RegexHelper::getInstance()->getLinkTitleRegex())) {
102
            // Chop off quotes from title and unescape
103 138
            return RegexHelper::unescape(substr($title, 1, strlen($title) - 2));
104
        }
105 135
    }
106
}
107