Passed
Push — 2.6 ( d150f9 )
by Colin
09:39
created

EmphasisDelimiterProcessor   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 97.44%

Importance

Changes 0
Metric Value
eloc 35
c 0
b 0
f 0
dl 0
loc 87
ccs 38
cts 39
cp 0.9744
rs 10
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 2310
    public function __construct(string $char)
42
    {
43 2310
        $this->char = $char;
44
    }
45
46 2310
    public function getOpeningCharacter(): string
47
    {
48 2310
        return $this->char;
49
    }
50
51 2310
    public function getClosingCharacter(): string
52
    {
53 2310
        return $this->char;
54
    }
55
56 488
    public function getMinLength(): int
57
    {
58 488
        return 1;
59
    }
60
61 350
    public function getDelimiterUse(DelimiterInterface $opener, DelimiterInterface $closer): int
62
    {
63
        // "Multiple of 3" rule for internal delimiter runs
64 350
        if (($opener->canClose() || $closer->canOpen()) && $closer->getOriginalLength() % 3 !== 0 && ($opener->getOriginalLength() + $closer->getOriginalLength()) % 3 === 0) {
65 16
            return 0;
66
        }
67
68
        // Calculate actual number of delimiters used from this closer
69 350
        if ($opener->getLength() >= 2 && $closer->getLength() >= 2) {
70 188
            if ($this->config->get('commonmark/enable_strong')) {
71 186
                return 2;
72
            }
73
74 2
            return 0;
75
        }
76
77 248
        if ($this->config->get('commonmark/enable_em')) {
78 246
            return 1;
79
        }
80
81 2
        return 0;
82
    }
83
84 346
    public function process(AbstractStringContainer $opener, AbstractStringContainer $closer, int $delimiterUse): void
85
    {
86 346
        if ($delimiterUse === 1) {
87 246
            $emphasis = new Emphasis($this->char);
88 186
        } elseif ($delimiterUse === 2) {
89 186
            $emphasis = new Strong($this->char . $this->char);
90
        } else {
91
            return;
92
        }
93
94 346
        $next = $opener->next();
95 346
        while ($next !== null && $next !== $closer) {
96 346
            $tmp = $next->next();
97 346
            $emphasis->appendChild($next);
98 346
            $next = $tmp;
99
        }
100
101 346
        $opener->insertAfter($emphasis);
102
    }
103
104 2310
    public function setConfiguration(ConfigurationInterface $configuration): void
105
    {
106 2310
        $this->config = $configuration;
107
    }
108
109 404
    public function getCacheKey(DelimiterInterface $closer): string
110
    {
111 404
        return \sprintf(
112 404
            '%s-%s-%d-%d',
113 404
            $this->char,
114 404
            $closer->canOpen() ? 'canOpen' : 'cannotOpen',
115 404
            $closer->getOriginalLength() % 3,
116 404
            $closer->getLength(),
117 404
        );
118
    }
119
}
120