Passed
Push — phpunit-10 ( 647fc3...2c9a19 )
by Colin
11:16
created

DelimiterParser   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 97.62%

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 81
ccs 41
cts 42
cp 0.9762
rs 10
c 0
b 0
f 0
wmc 23

4 Methods

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