EmphasisDelimiterProcessor   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 91.43%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 6
dl 0
loc 97
ccs 32
cts 35
cp 0.9143
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getOpeningCharacter() 0 4 1
A getClosingCharacter() 0 4 1
A getMinLength() 0 4 1
B getDelimiterUse() 0 22 11
A process() 0 19 5
A setConfiguration() 0 4 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\DelimiterInterface;
21
use League\CommonMark\Inline\Element\AbstractStringContainer;
22
use League\CommonMark\Inline\Element\Emphasis;
23
use League\CommonMark\Inline\Element\Strong;
24
use League\CommonMark\Util\ConfigurationAwareInterface;
25
use League\CommonMark\Util\ConfigurationInterface;
26
27
final class EmphasisDelimiterProcessor implements DelimiterProcessorInterface, ConfigurationAwareInterface
28
{
29
    /** @var string */
30
    private $char;
31
32
    /** @var ConfigurationInterface|null */
33
    private $config;
34
35
    /**
36
     * @param string $char The emphasis character to use (typically '*' or '_')
37
     */
38 2064
    public function __construct(string $char)
39
    {
40 2064
        $this->char = $char;
41 2064
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 2064
    public function getOpeningCharacter(): string
47
    {
48 2064
        return $this->char;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 2064
    public function getClosingCharacter(): string
55
    {
56 2064
        return $this->char;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 579
    public function getMinLength(): int
63
    {
64 579
        return 1;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 393
    public function getDelimiterUse(DelimiterInterface $opener, DelimiterInterface $closer): int
71
    {
72
        // "Multiple of 3" rule for internal delimiter runs
73 393
        if (($opener->canClose() || $closer->canOpen()) && $closer->getOriginalLength() % 3 !== 0 && ($opener->getOriginalLength() + $closer->getOriginalLength()) % 3 === 0) {
74 12
            return 0;
75
        }
76
77
        // Calculate actual number of delimiters used from this closer
78 393
        if ($opener->getLength() >= 2 && $closer->getLength() >= 2) {
79 159
            if ($this->config && $this->config->get('enable_strong', true)) {
80 159
                return 2;
81
            }
82
83
            return 0;
84
        }
85
86 309
        if ($this->config && $this->config->get('enable_em', true)) {
87 309
            return 1;
88
        }
89
90
        return 0;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 393
    public function process(AbstractStringContainer $opener, AbstractStringContainer $closer, int $delimiterUse)
97
    {
98 393
        if ($delimiterUse === 1) {
99 309
            $emphasis = new Emphasis();
100 159
        } elseif ($delimiterUse === 2) {
101 159
            $emphasis = new Strong();
102
        } else {
103
            return;
104
        }
105
106 393
        $next = $opener->next();
107 393
        while ($next !== null && $next !== $closer) {
108 393
            $tmp = $next->next();
109 393
            $emphasis->appendChild($next);
110 393
            $next = $tmp;
111
        }
112
113 393
        $opener->insertAfter($emphasis);
114 393
    }
115
116
    /**
117
     * @param ConfigurationInterface $configuration
118
     */
119 2064
    public function setConfiguration(ConfigurationInterface $configuration)
120
    {
121 2064
        $this->config = $configuration;
122 2064
    }
123
}
124