EmphasisDelimiterProcessor::getDelimiterUse()   B
last analyzed

Complexity

Conditions 11
Paths 5

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 11.968

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 21
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
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 2832
    public function __construct(string $char)
50
    {
51 2832
        $this->char = $char;
52 2832
    }
53
54 2832
    public function getOpeningCharacter(): string
55
    {
56 2832
        return $this->char;
57
    }
58
59 2832
    public function getClosingCharacter(): string
60
    {
61 2832
        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 2832
    public function setConfiguration(ConfigurationInterface $configuration): void
113
    {
114 2832
        $this->config = $configuration;
115 2832
    }
116
}
117