Passed
Push — 2.0 ( b7ed7b...934f6b )
by Colin
35:11 queued 31:18
created

DelimiterParser   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 83
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 83
ccs 43
cts 44
cp 0.9773
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B parse() 0 45 8
C determineCanOpenOrClose() 0 19 14
A getMatchDefinition() 0 3 1
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
    private DelimiterProcessorCollection $collection;
23
24 3138
    public function __construct(DelimiterProcessorCollection $collection)
25
    {
26 3138
        $this->collection = $collection;
27 3138
    }
28
29 3123
    public function getMatchDefinition(): InlineParserMatch
30
    {
31 3123
        return InlineParserMatch::oneOf(...$this->collection->getDelimiterCharacters());
32
    }
33
34 753
    public function parse(InlineParserContext $inlineContext): bool
35
    {
36 753
        $character = $inlineContext->getFullMatch();
37 753
        $numDelims = 0;
38 753
        $cursor    = $inlineContext->getCursor();
39 753
        $processor = $this->collection->getDelimiterProcessor($character);
40
41 753
        if ($processor === null) {
42
            throw new \LogicException('Delimiter processor should never be null here');
43
        }
44
45 753
        $charBefore = $cursor->peek(-1);
46 753
        if ($charBefore === null) {
47 453
            $charBefore = "\n";
48
        }
49
50 753
        while ($cursor->peek($numDelims) === $character) {
51 753
            ++$numDelims;
52
        }
53
54 753
        if ($numDelims < $processor->getMinLength()) {
55 6
            return false;
56
        }
57
58 747
        $cursor->advanceBy($numDelims);
59
60 747
        $charAfter = $cursor->getCurrentCharacter();
61 747
        if ($charAfter === null) {
62 468
            $charAfter = "\n";
63
        }
64
65 747
        [$canOpen, $canClose] = self::determineCanOpenOrClose($charBefore, $charAfter, $character, $processor);
66
67 747
        $node = new Text(\str_repeat($character, $numDelims), [
68 747
            'delim' => true,
69
        ]);
70 747
        $inlineContext->getContainer()->appendChild($node);
71
72
        // Add entry to stack to this opener
73 747
        if ($canOpen || $canClose) {
74 690
            $delimiter = new Delimiter($character, $numDelims, $node, $canOpen, $canClose);
75 690
            $inlineContext->getDelimiterStack()->push($delimiter);
76
        }
77
78 747
        return true;
79
    }
80
81
    /**
82
     * @return bool[]
83
     */
84 747
    private static function determineCanOpenOrClose(string $charBefore, string $charAfter, string $character, DelimiterProcessorInterface $delimiterProcessor): array
85
    {
86 747
        $afterIsWhitespace   = \preg_match(RegexHelper::REGEX_UNICODE_WHITESPACE_CHAR, $charAfter);
87 747
        $afterIsPunctuation  = \preg_match(RegexHelper::REGEX_PUNCTUATION, $charAfter);
88 747
        $beforeIsWhitespace  = \preg_match(RegexHelper::REGEX_UNICODE_WHITESPACE_CHAR, $charBefore);
89 747
        $beforeIsPunctuation = \preg_match(RegexHelper::REGEX_PUNCTUATION, $charBefore);
90
91 747
        $leftFlanking  = ! $afterIsWhitespace && (! $afterIsPunctuation || $beforeIsWhitespace || $beforeIsPunctuation);
92 747
        $rightFlanking = ! $beforeIsWhitespace && (! $beforeIsPunctuation || $afterIsWhitespace || $afterIsPunctuation);
93
94 747
        if ($character === '_') {
95 249
            $canOpen  = $leftFlanking && (! $rightFlanking || $beforeIsPunctuation);
96 249
            $canClose = $rightFlanking && (! $leftFlanking || $afterIsPunctuation);
97
        } else {
98 543
            $canOpen  = $leftFlanking && $character === $delimiterProcessor->getOpeningCharacter();
99 543
            $canClose = $rightFlanking && $character === $delimiterProcessor->getClosingCharacter();
100
        }
101
102 747
        return [$canOpen, $canClose];
103
    }
104
}
105