AttributesInlineParser::parse()   B
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 15
cts 15
cp 1
rs 8.8817
c 0
b 0
f 0
cc 6
nc 7
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This is part of the webuni/commonmark-attributes-extension package.
7
 *
8
 * (c) Martin Hasoň <[email protected]>
9
 * (c) Webuni s.r.o. <[email protected]>
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 Webuni\CommonMark\AttributesExtension;
16
17
use League\CommonMark\Inline\Parser\InlineParserInterface;
18
use League\CommonMark\InlineParserContext;
19
20
final class AttributesInlineParser implements InlineParserInterface
21 5
{
22
    public function getCharacters(): array
23 5
    {
24
        return [' ', '{'];
25
    }
26 4
27
    public function parse(InlineParserContext $inlineContext): bool
28 4
    {
29 4
        $cursor = $inlineContext->getCursor();
30 3
        if ('{' !== $cursor->getNextNonSpaceCharacter()) {
31
            return false;
32
        }
33 4
34 4
        $char = $cursor->getCharacter();
35 4
        if ('{' === $char) {
36
            $char = (string) $cursor->getCharacter($cursor->getPosition() - 1);
37
        }
38 4
39 4
        $attributes = AttributesUtils::parse($cursor);
40 1
        if (empty($attributes)) {
41
            return false;
42
        }
43 4
44 3
        if ('' === $char) {
45
            $cursor->advanceToNextNonSpaceOrNewline();
46
        }
47 4
48 4
        $node = new AttributesInline($attributes, ' ' === $char || '' === $char);
49 4
        $inlineContext->getContainer()->appendChild($node);
50
51 4
        return true;
52
    }
53
}
54