Completed
Push — master ( da54d7...33d164 )
by Colin
25s queued 10s
created

EmphasisDelimiterProcessor::getCharacter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
9
 *  - (c) John MacFarlane
10
 *
11
 * Additional emphasis processing code based on commonmark-java (https://github.com/atlassian/commonmark-java)
12
 *  - (c) Atlassian Pty Ltd
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace League\CommonMark\Delimiter\Processor;
19
20
use League\CommonMark\Delimiter\Delimiter;
21
use League\CommonMark\Inline\Element\Emphasis;
22
use League\CommonMark\Inline\Element\Strong;
23
use League\CommonMark\Inline\Element\Text;
24
use League\CommonMark\Util\Configuration;
25
26
final class EmphasisDelimiterProcessor implements DelimiterProcessorInterface
27
{
28
    private $char;
29
30
    /**
31
     * @param string $char The emphasis character to use (typically '*' or '_')
32
     */
33 2028
    public function __construct(string $char)
34
    {
35 2028
        $this->char = $char;
36 2028
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 2028
    public function getCharacter(): string
42
    {
43 2028
        return $this->char;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 351
    public function getDelimiterUse(Delimiter $opener, Delimiter $closer): int
50
    {
51
        // "Multiple of 3" rule for internal delimiter runs
52 351
        if (($opener->canClose() || $closer->canOpen()) && $closer->getOrigDelims() % 3 !== 0 && ($opener->getOrigDelims() + $closer->getOrigDelims()) % 3 === 0) {
53 12
            return 0;
54
        }
55
56
        // Calculate actual number of delimiters used from this closer
57 351
        if ($opener->getNumDelims() >= 2 && $closer->getNumDelims() >= 2) {
58 156
            return 2;
59
        }
60
61 267
        return 1;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 351
    public function process(Text $opener, Text $closer, int $delimiterUse)
68
    {
69
        /** @var Configuration|null $emphasisConfig */
70 351
        $emphasisConfig = $opener->getData('emphasis_config');
71
72 351
        if ($delimiterUse === 1 && ($emphasisConfig === null || $emphasisConfig->getConfig('enable_em', true))) {
73 267
            $emphasis = new Emphasis();
74 156
        } elseif ($delimiterUse === 2 && ($emphasisConfig === null || $emphasisConfig->getConfig('enable_strong', true))) {
75 156
            $emphasis = new Strong();
76
        } else {
77
            return;
78
        }
79
80 351
        $tmp = $opener->next();
81 351
        while ($tmp !== null && $tmp !== $closer) {
82 351
            $next = $tmp->next();
83 351
            $emphasis->appendChild($tmp);
84 351
            $tmp = $next;
85
        }
86
87 351
        $opener->insertAfter($emphasis);
88 351
    }
89
}
90