Passed
Push — strikethrough ( f7d777 )
by Colin
02:43
created

DelimiterParser::determineCanOpenOrClose()   C

Complexity

Conditions 14
Paths 208

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 14

Importance

Changes 0
Metric Value
cc 14
eloc 13
c 0
b 0
f 0
nc 208
nop 4
dl 0
loc 19
ccs 13
cts 13
cp 1
crap 14
rs 5.3333

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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