EmphasisDelimiterProcessor::getDelimiterUse()   B
last analyzed

Complexity

Conditions 9
Paths 5

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 9

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 21
ccs 10
cts 10
cp 1
rs 8.0555
cc 9
nc 5
nop 2
crap 9
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 (https://bitly.com/commonmark-js)
11
 *  - (c) John MacFarlane
12
 *
13
 * Additional emphasis processing code based on commonmark-java (https://github.com/atlassian/commonmark-java)
14
 *  - (c) Atlassian Pty Ltd
15
 *
16
 * For the full copyright and license information, please view the LICENSE
17
 * file that was distributed with this source code.
18
 */
19
20
namespace League\CommonMark\Extension\CommonMark\Delimiter\Processor;
21
22
use League\CommonMark\Delimiter\DelimiterInterface;
23
use League\CommonMark\Delimiter\Processor\DelimiterProcessorInterface;
24
use League\CommonMark\Extension\CommonMark\Node\Inline\Emphasis;
25
use League\CommonMark\Extension\CommonMark\Node\Inline\Strong;
26
use League\CommonMark\Node\Inline\AbstractStringContainer;
27
use League\Config\ConfigurationAwareInterface;
28
use League\Config\ConfigurationInterface;
29
30
final class EmphasisDelimiterProcessor implements DelimiterProcessorInterface, ConfigurationAwareInterface
31
{
32
    /**
33
     * @var string
34
     *
35
     * @psalm-readonly
36
     */
37
    private $char;
38
39
    /**
40
     * @var ConfigurationInterface
41
     *
42
     * @psalm-readonly-allow-private-mutation
43
     */
44
    private $config;
45
46
    /**
47
     * @param string $char The emphasis character to use (typically '*' or '_')
48
     */
49 2988
    public function __construct(string $char)
50
    {
51 2988
        $this->char = $char;
52 2988
    }
53
54 2988
    public function getOpeningCharacter(): string
55
    {
56 2988
        return $this->char;
57
    }
58
59 2988
    public function getClosingCharacter(): string
60
    {
61 2988
        return $this->char;
62
    }
63
64 642
    public function getMinLength(): int
65
    {
66 642
        return 1;
67
    }
68
69 450
    public function getDelimiterUse(DelimiterInterface $opener, DelimiterInterface $closer): int
70
    {
71
        // "Multiple of 3" rule for internal delimiter runs
72 450
        if (($opener->canClose() || $closer->canOpen()) && $closer->getOriginalLength() % 3 !== 0 && ($opener->getOriginalLength() + $closer->getOriginalLength()) % 3 === 0) {
73 12
            return 0;
74
        }
75
76
        // Calculate actual number of delimiters used from this closer
77 450
        if ($opener->getLength() >= 2 && $closer->getLength() >= 2) {
78 228
            if ($this->config->get('commonmark/enable_strong')) {
79 225
                return 2;
80
            }
81
82 3
            return 0;
83
        }
84
85 312
        if ($this->config->get('commonmark/enable_em')) {
86 309
            return 1;
87
        }
88
89 3
        return 0;
90
    }
91
92 444
    public function process(AbstractStringContainer $opener, AbstractStringContainer $closer, int $delimiterUse): void
93
    {
94 444
        if ($delimiterUse === 1) {
95 309
            $emphasis = new Emphasis();
96 225
        } elseif ($delimiterUse === 2) {
97 225
            $emphasis = new Strong();
98
        } else {
99
            return;
100
        }
101
102 444
        $next = $opener->next();
103 444
        while ($next !== null && $next !== $closer) {
104 444
            $tmp = $next->next();
105 444
            $emphasis->appendChild($next);
106 444
            $next = $tmp;
107
        }
108
109 444
        $opener->insertAfter($emphasis);
110 444
    }
111
112 2988
    public function setConfiguration(ConfigurationInterface $configuration): void
113
    {
114 2988
        $this->config = $configuration;
115 2988
    }
116
}
117