Completed
Push — 2.3 ( 32891d...5d77bc )
by Colin
16s queued 14s
created

QuoteParser   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 97.06%

Importance

Changes 0
Metric Value
wmc 15
eloc 39
dl 0
loc 79
ccs 33
cts 34
cp 0.9706
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getMatchDefinition() 0 3 1
A getNormalizedQuoteCharacter() 0 11 3
A parse() 0 30 4
B determineFlanking() 0 18 7
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * Original code based on the CommonMark JS reference parser (http://bitly.com/commonmark-js)
11
 *  - (c) John MacFarlane
12
 *
13
 * For the full copyright and license information, please view the LICENSE
14
 * file that was distributed with this source code.
15
 */
16
17
namespace League\CommonMark\Extension\SmartPunct;
18
19
use League\CommonMark\Delimiter\Delimiter;
20
use League\CommonMark\Parser\Inline\InlineParserInterface;
21
use League\CommonMark\Parser\Inline\InlineParserMatch;
22
use League\CommonMark\Parser\InlineParserContext;
23
use League\CommonMark\Util\RegexHelper;
24
25
final class QuoteParser implements InlineParserInterface
26
{
27
    public const DOUBLE_QUOTES = [Quote::DOUBLE_QUOTE, Quote::DOUBLE_QUOTE_OPENER, Quote::DOUBLE_QUOTE_CLOSER];
28
    public const SINGLE_QUOTES = [Quote::SINGLE_QUOTE, Quote::SINGLE_QUOTE_OPENER, Quote::SINGLE_QUOTE_CLOSER];
29
30 36
    public function getMatchDefinition(): InlineParserMatch
31
    {
32 36
        return InlineParserMatch::oneOf(...\array_merge(self::DOUBLE_QUOTES, self::SINGLE_QUOTES));
33
    }
34
35
    /**
36
     * Normalizes any quote characters found and manually adds them to the delimiter stack
37
     */
38 24
    public function parse(InlineParserContext $inlineContext): bool
39
    {
40 24
        $char   = $inlineContext->getFullMatch();
41 24
        $cursor = $inlineContext->getCursor();
42
43 24
        $normalizedCharacter = $this->getNormalizedQuoteCharacter($char);
44
45 24
        $charBefore = $cursor->peek(-1);
46 24
        if ($charBefore === null) {
47 20
            $charBefore = "\n";
48
        }
49
50 24
        $cursor->advance();
51
52 24
        $charAfter = $cursor->getCurrentCharacter();
53 24
        if ($charAfter === null) {
54 14
            $charAfter = "\n";
55
        }
56
57 24
        [$leftFlanking, $rightFlanking] = $this->determineFlanking($charBefore, $charAfter);
58 24
        $canOpen                        = $leftFlanking && ! $rightFlanking;
59 24
        $canClose                       = $rightFlanking;
60
61 24
        $node = new Quote($normalizedCharacter, ['delim' => true]);
62 24
        $inlineContext->getContainer()->appendChild($node);
63
64
        // Add entry to stack to this opener
65 24
        $inlineContext->getDelimiterStack()->push(new Delimiter($normalizedCharacter, 1, $node, $canOpen, $canClose));
66
67 24
        return true;
68
    }
69
70 24
    private function getNormalizedQuoteCharacter(string $character): string
71
    {
72 24
        if (\in_array($character, self::DOUBLE_QUOTES, true)) {
73 14
            return Quote::DOUBLE_QUOTE;
74
        }
75
76 22
        if (\in_array($character, self::SINGLE_QUOTES, true)) {
77 22
            return Quote::SINGLE_QUOTE;
78
        }
79
80
        return $character;
81
    }
82
83
    /**
84
     * @return bool[]
85
     */
86 24
    private function determineFlanking(string $charBefore, string $charAfter): array
87
    {
88 24
        $afterIsWhitespace   = \preg_match('/\pZ|\s/u', $charAfter);
89 24
        $afterIsPunctuation  = \preg_match(RegexHelper::REGEX_PUNCTUATION, $charAfter);
90 24
        $beforeIsWhitespace  = \preg_match('/\pZ|\s/u', $charBefore);
91 24
        $beforeIsPunctuation = \preg_match(RegexHelper::REGEX_PUNCTUATION, $charBefore);
92
93 24
        $leftFlanking = ! $afterIsWhitespace &&
94
            ! ($afterIsPunctuation &&
95
                ! $beforeIsWhitespace &&
96
                ! $beforeIsPunctuation);
97
98 24
        $rightFlanking = ! $beforeIsWhitespace &&
99
            ! ($beforeIsPunctuation &&
100
                ! $afterIsWhitespace &&
101
                ! $afterIsPunctuation);
102
103 24
        return [$leftFlanking, $rightFlanking];
104
    }
105
}
106