Completed
Pull Request — master (#2)
by Colin
02:24
created

StrikethroughParser   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 7
dl 0
loc 45
c 0
b 0
f 0
ccs 20
cts 20
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCharacters() 0 4 1
A parse() 0 31 4
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark-ext-strikethrough package.
5
 *
6
 * (c) Colin O'Dell <[email protected]> and uAfrica.com (http://uafrica.com)
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace League\CommonMark\Ext\Strikethrough;
13
14
use League\CommonMark\Delimiter\Delimiter;
15
use League\CommonMark\Inline\Element\Text;
16
use League\CommonMark\Inline\Parser\EmphasisParser;
17
use League\CommonMark\Inline\Parser\InlineParserInterface;
18
use League\CommonMark\InlineParserContext;
19
20
final class StrikethroughParser implements InlineParserInterface
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25 51
    public function getCharacters(): array
26
    {
27 51
        return ['~'];
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 48
    public function parse(InlineParserContext $inlineContext): bool
34
    {
35 48
        $cursor = $inlineContext->getCursor();
36 48
        $charBefore = $cursor->peek(-1);
37 48
        if ($charBefore === null) {
38 6
            $charBefore = "\n";
39
        }
40
41 48
        $tildes = $cursor->match('/^~~+/');
42 48
        if ($tildes === null) {
43 6
            return false;
44
        }
45
46 42
        $charAfter = $cursor->getCharacter();
47 42
        if ($charAfter === null) {
48 12
            $charAfter = "\n";
49
        }
50
51 42
        list($canOpen, $canClose) = EmphasisParser::determineCanOpenOrClose($charBefore, $charAfter, '~');
52
53 42
        $node = new Text($cursor->getPreviousText(), [
54 42
            'delim' => true,
55
        ]);
56 42
        $inlineContext->getContainer()->appendChild($node);
57
58
        // Add entry to stack to this opener
59 42
        $delimiter = new Delimiter('~', \mb_strlen($tildes, $cursor->getEncoding()), $node, $canOpen, $canClose);
60 42
        $inlineContext->getDelimiterStack()->push($delimiter);
61
62 42
        return true;
63
    }
64
}
65