Passed
Push — latest ( d59ff4...6fb795 )
by Colin
08:12
created

EmphasisDelimiterProcessor   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 91.18%

Importance

Changes 0
Metric Value
wmc 21
eloc 29
dl 0
loc 85
ccs 31
cts 34
cp 0.9118
rs 10
c 0
b 0
f 0

7 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 11
A getClosingCharacter() 0 3 1
A getMinLength() 0 3 1
A setConfiguration() 0 3 1
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\Configuration\ConfigurationAwareInterface;
23
use League\CommonMark\Configuration\ConfigurationInterface;
24
use League\CommonMark\Delimiter\DelimiterInterface;
25
use League\CommonMark\Delimiter\Processor\DelimiterProcessorInterface;
26
use League\CommonMark\Extension\CommonMark\Node\Inline\Emphasis;
27
use League\CommonMark\Extension\CommonMark\Node\Inline\Strong;
28
use League\CommonMark\Node\Inline\AbstractStringContainer;
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|null
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 2781
    public function __construct(string $char)
50
    {
51 2781
        $this->char = $char;
52 2781
    }
53
54 2781
    public function getOpeningCharacter(): string
55
    {
56 2781
        return $this->char;
57
    }
58
59 2781
    public function getClosingCharacter(): string
60
    {
61 2781
        return $this->char;
62
    }
63
64 600
    public function getMinLength(): int
65
    {
66 600
        return 1;
67
    }
68
69 399
    public function getDelimiterUse(DelimiterInterface $opener, DelimiterInterface $closer): int
70
    {
71
        // "Multiple of 3" rule for internal delimiter runs
72 399
        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 399
        if ($opener->getLength() >= 2 && $closer->getLength() >= 2) {
78 192
            if ($this->config && $this->config->get('enable_strong', true)) {
79 192
                return 2;
80
            }
81
82
            return 0;
83
        }
84
85 294
        if ($this->config && $this->config->get('enable_em', true)) {
86 294
            return 1;
87
        }
88
89
        return 0;
90
    }
91
92 399
    public function process(AbstractStringContainer $opener, AbstractStringContainer $closer, int $delimiterUse): void
93
    {
94 399
        if ($delimiterUse === 1) {
95 294
            $emphasis = new Emphasis();
96 192
        } elseif ($delimiterUse === 2) {
97 192
            $emphasis = new Strong();
98
        } else {
99
            return;
100
        }
101
102 399
        $next = $opener->next();
103 399
        while ($next !== null && $next !== $closer) {
104 399
            $tmp = $next->next();
105 399
            $emphasis->appendChild($next);
106 399
            $next = $tmp;
107
        }
108
109 399
        $opener->insertAfter($emphasis);
110 399
    }
111
112 2781
    public function setConfiguration(ConfigurationInterface $configuration): void
113
    {
114 2781
        $this->config = $configuration;
115 2781
    }
116
}
117