Completed
Push — 1.6 ( bb055d )
by Colin
01:28
created

EmphasisDelimiterProcessor::enableEm()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 7
cts 8
cp 0.875
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3.0175
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 2907
    public function __construct(string $char)
39
    {
40 2907
        $this->char = $char;
41 2907
    }
42
43 2907
    public function getOpeningCharacter(): string
44
    {
45 2907
        return $this->char;
46
    }
47
48 2907
    public function getClosingCharacter(): string
49
    {
50 2907
        return $this->char;
51
    }
52
53 717
    public function getMinLength(): int
54
    {
55 717
        return 1;
56
    }
57
58 507
    public function getDelimiterUse(DelimiterInterface $opener, DelimiterInterface $closer): int
59
    {
60
        // "Multiple of 3" rule for internal delimiter runs
61 507
        if (($opener->canClose() || $closer->canOpen()) && $closer->getOriginalLength() % 3 !== 0 && ($opener->getOriginalLength() + $closer->getOriginalLength()) % 3 === 0) {
62 12
            return 0;
63
        }
64
65
        // Calculate actual number of delimiters used from this closer
66 507
        if ($opener->getLength() >= 2 && $closer->getLength() >= 2) {
67 237
            if ($this->enableStrong()) {
68 228
                return 2;
69
            }
70
71 9
            return 0;
72
        }
73
74 363
        if ($this->enableEm()) {
75 354
            return 1;
76
        }
77
78 9
        return 0;
79
    }
80
81 489
    public function process(AbstractStringContainer $opener, AbstractStringContainer $closer, int $delimiterUse)
82
    {
83 489
        if ($delimiterUse === 1) {
84 354
            $emphasis = new Emphasis();
85 228
        } elseif ($delimiterUse === 2) {
86 228
            $emphasis = new Strong();
87
        } else {
88
            return;
89
        }
90
91 489
        $next = $opener->next();
92 489
        while ($next !== null && $next !== $closer) {
93 489
            $tmp = $next->next();
94 489
            $emphasis->appendChild($next);
95 489
            $next = $tmp;
96
        }
97
98 489
        $opener->insertAfter($emphasis);
99 489
    }
100
101 2907
    public function setConfiguration(ConfigurationInterface $configuration)
102
    {
103 2907
        $this->config = $configuration;
104 2907
    }
105
106 237
    private function enableStrong(): bool
107
    {
108 237
        if ($this->config === null) {
109
            return false;
110
        }
111
112 237
        $deprecatedEnableStrong = $this->config->get('enable_strong', ConfigurationInterface::MISSING);
0 ignored issues
show
Deprecated Code introduced by
The constant League\CommonMark\Util\C...ationInterface::MISSING has been deprecated.

This class constant has been deprecated.

Loading history...
113 237
        if ($deprecatedEnableStrong !== ConfigurationInterface::MISSING) {
0 ignored issues
show
Deprecated Code introduced by
The constant League\CommonMark\Util\C...ationInterface::MISSING has been deprecated.

This class constant has been deprecated.

Loading history...
114 6
            @\trigger_error('The "enable_strong" configuration option is deprecated in league/commonmark 1.6 and will be replaced with "commonmark > enable_strong" in 2.0', \E_USER_DEPRECATED);
115
        } else {
116 231
            $deprecatedEnableStrong = true;
117
        }
118
119 237
        return $this->config->get('commonmark/enable_strong', $deprecatedEnableStrong);
120
    }
121
122 363
    private function enableEm(): bool
123
    {
124 363
        if ($this->config === null) {
125
            return false;
126
        }
127
128 363
        $deprecatedEnableEm = $this->config->get('enable_em', ConfigurationInterface::MISSING);
0 ignored issues
show
Deprecated Code introduced by
The constant League\CommonMark\Util\C...ationInterface::MISSING has been deprecated.

This class constant has been deprecated.

Loading history...
129 363
        if ($deprecatedEnableEm !== ConfigurationInterface::MISSING) {
0 ignored issues
show
Deprecated Code introduced by
The constant League\CommonMark\Util\C...ationInterface::MISSING has been deprecated.

This class constant has been deprecated.

Loading history...
130 12
            @\trigger_error('The "enable_em" configuration option is deprecated in league/commonmark 1.6 and will be replaced with "commonmark > enable_em" in 2.0', \E_USER_DEPRECATED);
131
        } else {
132 351
            $deprecatedEnableEm = true;
133
        }
134
135 363
        return $this->config->get('commonmark/enable_em', $deprecatedEnableEm);
136
    }
137
}
138