Completed
Push — master ( 0b0b24...54ff37 )
by Colin
02:37
created

EmphasisDelimiterProcessor::getDelimiterUse()   B

Complexity

Conditions 11
Paths 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 11.968

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 8
cts 10
cp 0.8
rs 7.3166
c 0
b 0
f 0
cc 11
nc 5
nop 2
crap 11.968

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Extension\CommonMark\Delimiter\Processor;
19
20
use League\CommonMark\Configuration\ConfigurationAwareInterface;
21
use League\CommonMark\Configuration\ConfigurationInterface;
22
use League\CommonMark\Delimiter\DelimiterInterface;
23
use League\CommonMark\Delimiter\Processor\DelimiterProcessorInterface;
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
28
final class EmphasisDelimiterProcessor implements DelimiterProcessorInterface, ConfigurationAwareInterface
29
{
30
    /** @var string */
31
    private $char;
32
33
    /** @var ConfigurationInterface|null */
34
    private $config;
35
36
    /**
37
     * @param string $char The emphasis character to use (typically '*' or '_')
38
     */
39 2490
    public function __construct(string $char)
40
    {
41 2490
        $this->char = $char;
42 2490
    }
43
44 2490
    public function getOpeningCharacter(): string
45
    {
46 2490
        return $this->char;
47
    }
48
49 2490
    public function getClosingCharacter(): string
50
    {
51 2490
        return $this->char;
52
    }
53
54 639
    public function getMinLength(): int
55
    {
56 639
        return 1;
57
    }
58
59 429
    public function getDelimiterUse(DelimiterInterface $opener, DelimiterInterface $closer): int
60
    {
61
        // "Multiple of 3" rule for internal delimiter runs
62 429
        if (($opener->canClose() || $closer->canOpen()) && $closer->getOriginalLength() % 3 !== 0 && ($opener->getOriginalLength() + $closer->getOriginalLength()) % 3 === 0) {
63 12
            return 0;
64
        }
65
66
        // Calculate actual number of delimiters used from this closer
67 429
        if ($opener->getLength() >= 2 && $closer->getLength() >= 2) {
68 186
            if ($this->config && $this->config->get('enable_strong', true)) {
69 186
                return 2;
70
            }
71
72
            return 0;
73
        }
74
75 324
        if ($this->config && $this->config->get('enable_em', true)) {
76 324
            return 1;
77
        }
78
79
        return 0;
80
    }
81
82 429
    public function process(AbstractStringContainer $opener, AbstractStringContainer $closer, int $delimiterUse): void
83
    {
84 429
        if ($delimiterUse === 1) {
85 324
            $emphasis = new Emphasis();
86 186
        } elseif ($delimiterUse === 2) {
87 186
            $emphasis = new Strong();
88
        } else {
89
            return;
90
        }
91
92 429
        $next = $opener->next();
93 429
        while ($next !== null && $next !== $closer) {
94 429
            $tmp = $next->next();
95 429
            $emphasis->appendChild($next);
96 429
            $next = $tmp;
97
        }
98
99 429
        $opener->insertAfter($emphasis);
100 429
    }
101
102 2490
    public function setConfiguration(ConfigurationInterface $configuration): void
103
    {
104 2490
        $this->config = $configuration;
105 2490
    }
106
}
107