EmphasisDelimiterProcessor   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 21

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A process() 0 18 5
A getOpeningCharacter() 0 3 1
B getDelimiterUse() 0 21 9
A getClosingCharacter() 0 3 1
A getMinLength() 0 3 1
A setConfiguration() 0 3 1
A getCacheKey() 0 8 2
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\CacheableDelimiterProcessorInterface;
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 CacheableDelimiterProcessorInterface, ConfigurationAwareInterface
31
{
32
    /** @psalm-readonly */
33
    private string $char;
34
35
    /** @psalm-readonly-allow-private-mutation */
36
    private ConfigurationInterface $config;
37
38
    /**
39
     * @param string $char The emphasis character to use (typically '*' or '_')
40
     */
41
    public function __construct(string $char)
42
    {
43
        $this->char = $char;
44
    }
45
46
    public function getOpeningCharacter(): string
47
    {
48
        return $this->char;
49
    }
50
51
    public function getClosingCharacter(): string
52
    {
53
        return $this->char;
54
    }
55
56
    public function getMinLength(): int
57
    {
58
        return 1;
59
    }
60
61
    public function getDelimiterUse(DelimiterInterface $opener, DelimiterInterface $closer): int
62
    {
63
        // "Multiple of 3" rule for internal delimiter runs
64
        if (($opener->canClose() || $closer->canOpen()) && $closer->getOriginalLength() % 3 !== 0 && ($opener->getOriginalLength() + $closer->getOriginalLength()) % 3 === 0) {
65
            return 0;
66
        }
67
68
        // Calculate actual number of delimiters used from this closer
69
        if ($opener->getLength() >= 2 && $closer->getLength() >= 2) {
70
            if ($this->config->get('commonmark/enable_strong')) {
71
                return 2;
72
            }
73
74
            return 0;
75
        }
76
77
        if ($this->config->get('commonmark/enable_em')) {
78
            return 1;
79
        }
80
81
        return 0;
82
    }
83
84
    public function process(AbstractStringContainer $opener, AbstractStringContainer $closer, int $delimiterUse): void
85
    {
86
        if ($delimiterUse === 1) {
87
            $emphasis = new Emphasis($this->char);
88
        } elseif ($delimiterUse === 2) {
89
            $emphasis = new Strong($this->char . $this->char);
90
        } else {
91
            return;
92
        }
93
94
        $next = $opener->next();
95
        while ($next !== null && $next !== $closer) {
96
            $tmp = $next->next();
97
            $emphasis->appendChild($next);
98
            $next = $tmp;
99
        }
100
101
        $opener->insertAfter($emphasis);
102
    }
103
104
    public function setConfiguration(ConfigurationInterface $configuration): void
105
    {
106
        $this->config = $configuration;
107
    }
108
109
    public function getCacheKey(DelimiterInterface $closer): string
110
    {
111
        return \sprintf(
112
            '%s-%s-%d-%d',
113
            $this->char,
114
            $closer->canOpen() ? 'canOpen' : 'cannotOpen',
115
            $closer->getOriginalLength() % 3,
116
            $closer->getLength(),
117
        );
118
    }
119
}
120