DelimiterParser   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 97.73%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
eloc 42
c 1
b 0
f 0
dl 0
loc 84
ccs 43
cts 44
cp 0.9773
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
C determineCanOpenOrClose() 0 19 14
A getMatchDefinition() 0 3 1
B parse() 0 45 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace League\CommonMark\Delimiter;
6
7
use League\CommonMark\Delimiter\Processor\DelimiterProcessorCollection;
8
use League\CommonMark\Delimiter\Processor\DelimiterProcessorInterface;
9
use League\CommonMark\Node\Inline\Text;
10
use League\CommonMark\Parser\Inline\InlineParserInterface;
11
use League\CommonMark\Parser\Inline\InlineParserMatch;
12
use League\CommonMark\Parser\InlineParserContext;
13
use League\CommonMark\Util\RegexHelper;
14
15
/**
16
 * Delimiter parsing is implemented as an Inline Parser with the lowest-possible priority
17
 *
18
 * @internal
19
 */
20
final class DelimiterParser implements InlineParserInterface
21
{
22
    /** @var DelimiterProcessorCollection */
23
    private $collection;
24
25 2994
    public function __construct(DelimiterProcessorCollection $collection)
26
    {
27 2994
        $this->collection = $collection;
28 2994
    }
29
30 2982
    public function getMatchDefinition(): InlineParserMatch
31
    {
32 2982
        return InlineParserMatch::oneOf(...$this->collection->getDelimiterCharacters());
33
    }
34
35 729
    public function parse(InlineParserContext $inlineContext): bool
36
    {
37 729
        $character = $inlineContext->getFullMatch();
38 729
        $numDelims = 0;
39 729
        $cursor    = $inlineContext->getCursor();
40 729
        $processor = $this->collection->getDelimiterProcessor($character);
41
42 729
        if ($processor === null) {
43
            throw new \LogicException('Delimiter processor should never be null here');
44
        }
45
46 729
        $charBefore = $cursor->peek(-1);
47 729
        if ($charBefore === null) {
48 444
            $charBefore = "\n";
49
        }
50
51 729
        while ($cursor->peek($numDelims) === $character) {
52 729
            ++$numDelims;
53
        }
54
55 729
        if ($numDelims < $processor->getMinLength()) {
56 6
            return false;
57
        }
58
59 723
        $cursor->advanceBy($numDelims);
60
61 723
        $charAfter = $cursor->getCharacter();
62 723
        if ($charAfter === null) {
63 456
            $charAfter = "\n";
64
        }
65
66 723
        [$canOpen, $canClose] = self::determineCanOpenOrClose($charBefore, $charAfter, $character, $processor);
67
68 723
        $node = new Text(\str_repeat($character, $numDelims), [
69 723
            'delim' => true,
70
        ]);
71 723
        $inlineContext->getContainer()->appendChild($node);
72
73
        // Add entry to stack to this opener
74 723
        if ($canOpen || $canClose) {
75 669
            $delimiter = new Delimiter($character, $numDelims, $node, $canOpen, $canClose);
76 669
            $inlineContext->getDelimiterStack()->push($delimiter);
77
        }
78
79 723
        return true;
80
    }
81
82
    /**
83
     * @return bool[]
84
     */
85 723
    private static function determineCanOpenOrClose(string $charBefore, string $charAfter, string $character, DelimiterProcessorInterface $delimiterProcessor): array
86
    {
87 723
        $afterIsWhitespace   = \preg_match(RegexHelper::REGEX_UNICODE_WHITESPACE_CHAR, $charAfter);
88 723
        $afterIsPunctuation  = \preg_match(RegexHelper::REGEX_PUNCTUATION, $charAfter);
89 723
        $beforeIsWhitespace  = \preg_match(RegexHelper::REGEX_UNICODE_WHITESPACE_CHAR, $charBefore);
90 723
        $beforeIsPunctuation = \preg_match(RegexHelper::REGEX_PUNCTUATION, $charBefore);
91
92 723
        $leftFlanking  = ! $afterIsWhitespace && (! $afterIsPunctuation || $beforeIsWhitespace || $beforeIsPunctuation);
93 723
        $rightFlanking = ! $beforeIsWhitespace && (! $beforeIsPunctuation || $afterIsWhitespace || $afterIsPunctuation);
94
95 723
        if ($character === '_') {
96 240
            $canOpen  = $leftFlanking && (! $rightFlanking || $beforeIsPunctuation);
97 240
            $canClose = $rightFlanking && (! $leftFlanking || $afterIsPunctuation);
98
        } else {
99 522
            $canOpen  = $leftFlanking && $character === $delimiterProcessor->getOpeningCharacter();
100 522
            $canClose = $rightFlanking && $character === $delimiterProcessor->getClosingCharacter();
101
        }
102
103 723
        return [$canOpen, $canClose];
104
    }
105
}
106