Passed
Push — 2.3 ( 32a49e )
by Colin
03:51 queued 01:09
created

EmbedStartParser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 30
ccs 11
cts 12
cp 0.9167
rs 10
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B tryStart() 0 28 7
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
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace League\CommonMark\Extension\Embed;
15
16
use League\CommonMark\Node\Block\Document;
17
use League\CommonMark\Parser\Block\BlockStart;
18
use League\CommonMark\Parser\Block\BlockStartParserInterface;
19
use League\CommonMark\Parser\Cursor;
20
use League\CommonMark\Parser\MarkdownParserStateInterface;
21
use League\CommonMark\Util\LinkParserHelper;
22
23
class EmbedStartParser implements BlockStartParserInterface
24
{
25 8
    public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart
26
    {
27 8
        if ($cursor->isIndented() || $parserState->getParagraphContent() !== null || ! ($parserState->getLastMatchedBlockParser()->getBlock() instanceof Document)) {
28 2
            return BlockStart::none();
29
        }
30
31
        // 0-3 leading spaces are okay
32 8
        $cursor->advanceToNextNonSpaceOrTab();
33
34
        // The line must begin with "https://"
35 8
        if (! str_starts_with($cursor->getRemainder(), 'https://')) {
36 8
            return BlockStart::none();
37
        }
38
39
        // A valid link must be found next
40 8
        if (($dest = LinkParserHelper::parseLinkDestination($cursor)) === null) {
41
            return BlockStart::none();
42
        }
43
44
        // Skip any trailing whitespace
45 8
        $cursor->advanceToNextNonSpaceOrTab();
46
47
        // We must be at the end of the line; otherwise, this link was not by itself
48 8
        if (! $cursor->isAtEnd()) {
49 2
            return BlockStart::none();
50
        }
51
52 8
        return BlockStart::of(new EmbedParser($dest))->at($cursor);
53
    }
54
}
55