Passed
Push — dependabot/github_actions/ride... ( bf7823 )
by
unknown
02:50
created

DelimiterParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 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 2170
    public function __construct(DelimiterProcessorCollection $collection)
25
    {
26 2170
        $this->collection = $collection;
27
    }
28
29 2158
    public function getMatchDefinition(): InlineParserMatch
30
    {
31 2158
        return InlineParserMatch::oneOf(...$this->collection->getDelimiterCharacters());
32
    }
33
34 516
    public function parse(InlineParserContext $inlineContext): bool
35
    {
36 516
        $character = $inlineContext->getFullMatch();
37 516
        $numDelims = 0;
38 516
        $cursor    = $inlineContext->getCursor();
39 516
        $processor = $this->collection->getDelimiterProcessor($character);
40
41 516
        if ($processor === null) {
42
            throw new \LogicException('Delimiter processor should never be null here');
43
        }
44
45 516
        $charBefore = $cursor->peek(-1);
46 516
        if ($charBefore === null) {
47 306
            $charBefore = "\n";
48
        }
49
50 516
        while ($cursor->peek($numDelims) === $character) {
51 516
            ++$numDelims;
52
        }
53
54 516
        if ($numDelims < $processor->getMinLength()) {
55 4
            return false;
56
        }
57
58 512
        $cursor->advanceBy($numDelims);
59
60 512
        $charAfter = $cursor->getCurrentCharacter();
61 512
        if ($charAfter === null) {
62 318
            $charAfter = "\n";
63
        }
64
65 512
        [$canOpen, $canClose] = self::determineCanOpenOrClose($charBefore, $charAfter, $character, $processor);
66
67 512
        $node = new Text(\str_repeat($character, $numDelims), [
68
            'delim' => true,
69
        ]);
70 512
        $inlineContext->getContainer()->appendChild($node);
71
72
        // Add entry to stack to this opener
73 512
        if ($canOpen || $canClose) {
74 472
            $delimiter = new Delimiter($character, $numDelims, $node, $canOpen, $canClose);
75 472
            $inlineContext->getDelimiterStack()->push($delimiter);
76
        }
77
78 512
        return true;
79
    }
80
81
    /**
82
     * @return bool[]
83
     */
84 512
    private static function determineCanOpenOrClose(string $charBefore, string $charAfter, string $character, DelimiterProcessorInterface $delimiterProcessor): array
85
    {
86 512
        $afterIsWhitespace   = \preg_match(RegexHelper::REGEX_UNICODE_WHITESPACE_CHAR, $charAfter);
87 512
        $afterIsPunctuation  = \preg_match(RegexHelper::REGEX_PUNCTUATION, $charAfter);
88 512
        $beforeIsWhitespace  = \preg_match(RegexHelper::REGEX_UNICODE_WHITESPACE_CHAR, $charBefore);
89 512
        $beforeIsPunctuation = \preg_match(RegexHelper::REGEX_PUNCTUATION, $charBefore);
90
91 512
        $leftFlanking  = ! $afterIsWhitespace && (! $afterIsPunctuation || $beforeIsWhitespace || $beforeIsPunctuation);
92 512
        $rightFlanking = ! $beforeIsWhitespace && (! $beforeIsPunctuation || $afterIsWhitespace || $afterIsPunctuation);
93
94 512
        if ($character === '_') {
95 168
            $canOpen  = $leftFlanking && (! $rightFlanking || $beforeIsPunctuation);
96 168
            $canClose = $rightFlanking && (! $leftFlanking || $afterIsPunctuation);
97
        } else {
98 374
            $canOpen  = $leftFlanking && $character === $delimiterProcessor->getOpeningCharacter();
99 374
            $canClose = $rightFlanking && $character === $delimiterProcessor->getClosingCharacter();
100
        }
101
102 512
        return [$canOpen, $canClose];
103
    }
104
}
105