Completed
Pull Request — master (#8)
by
unknown
08:47
created

AttributesInlineParser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 11.11%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 8
dl 0
loc 35
ccs 2
cts 18
cp 0.1111
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCharacters() 0 4 1
B parse() 0 27 6
1
<?php
2
3
/*
4
 * This is part of the webuni/commonmark-attributes-extension package.
5
 *
6
 * (c) Martin Hasoň <[email protected]>
7
 * (c) Webuni s.r.o. <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Webuni\CommonMark\AttributesExtension;
14
15
use League\CommonMark\Delimiter\Delimiter;
16
use League\CommonMark\Inline\Parser\AbstractInlineParser;
17
use League\CommonMark\InlineParserContext;
18
19
class AttributesInlineParser extends AbstractInlineParser
20
{
21 5
    public function getCharacters()
22
    {
23 5
        return [' ', '{'];
24
    }
25
26
    public function parse(InlineParserContext $inlineContext)
27
    {
28
        $cursor = $inlineContext->getCursor();
29
        if ($cursor->getNextNonSpaceCharacter() !== '{') {
30
            return false;
31
        }
32
33
        $char = $cursor->getCharacter();
34
        if ('{' === $char) {
35
            $char = (string) $cursor->getCharacter($cursor->getPosition() - 1);
36
        }
37
38
        $attributes = AttributesUtils::parse($cursor);
39
        if (empty($attributes)) {
40
            return false;
41
        }
42
43
        if ('' === $char) {
44
            $cursor->advanceToNextNonSpaceOrNewline();
45
        }
46
47
        $node = new InlineAttributes($attributes, ' ' === $char || '' === $char);
48
        $inlineContext->getContainer()->appendChild($node);
49
        $inlineContext->getDelimiterStack()->push(new Delimiter('attributes', 1, $node, false, false));
50
51
        return true;
52
    }
53
}
54