EmphasisProcessor   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 97.3%

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 4
dl 0
loc 59
ccs 36
cts 37
cp 0.973
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C processInlines() 0 56 12
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
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace League\CommonMark\Inline\Processor;
16
17
use League\CommonMark\Delimiter\Delimiter;
18
use League\CommonMark\Delimiter\DelimiterStack;
19
use League\CommonMark\Inline\Element\Emphasis;
20
use League\CommonMark\Inline\Element\Strong;
21
use League\CommonMark\Inline\Element\Text;
22
23
class EmphasisProcessor implements InlineProcessorInterface
24
{
25
    public function processInlines(DelimiterStack $delimiterStack, Delimiter $stackBottom = null)
26
    {
27 1662
        $callback = function (Delimiter $opener, Delimiter $closer, DelimiterStack $stack) {
28
            // Calculate actual number of delimiters used from this closer
29 336
            $useDelims = ($closer->getNumDelims() >= 2 && $opener->getNumDelims() >= 2) ? 2 : 1;
30
            /** @var Text $openerInline */
31 336
            $openerInline = $opener->getInlineNode();
32
            /** @var Text $closerInline */
33 336
            $closerInline = $closer->getInlineNode();
34
35
            // Remove used delimiters from stack elts and inlines
36 336
            $opener->setNumDelims($opener->getNumDelims() - $useDelims);
37 336
            $closer->setNumDelims($closer->getNumDelims() - $useDelims);
38 336
            $openerInline->setContent(substr($openerInline->getContent(), 0, -$useDelims));
39 336
            $closerInline->setContent(substr($closerInline->getContent(), 0, -$useDelims));
40
41
            // Build contents for new emph element
42 336
            if ($useDelims === 1 && $openerInline->data['emphasis_config']->getConfig('enable_em')) {
43 255
                $emph = new Emphasis();
44 212
            } elseif ($useDelims > 1 && $openerInline->data['emphasis_config']->getConfig('enable_strong')) {
45 150
                $emph = new Strong();
46 50
            } else {
47
                return $closer->getNext();
48
            }
49
50 336
            $openerInline->insertAfter($emph);
51 336
            while (($node = $emph->next()) !== $closerInline) {
52 336
                $emph->appendChild($node);
53 112
            }
54
55
            // Remove elts btw opener and closer in delimiters stack
56 336
            $tempStack = $closer->getPrevious();
57 336
            while ($tempStack !== null && $tempStack !== $opener) {
58 24
                $nextStack = $tempStack->getPrevious();
59 24
                $stack->removeDelimiter($tempStack);
60 24
                $tempStack = $nextStack;
61 8
            }
62
            // If opener has 0 delims, remove it and the inline
63 336
            if ($opener->getNumDelims() === 0) {
64 315
                $openerInline->detach();
65 315
                $stack->removeDelimiter($opener);
66 105
            }
67 336
            if ($closer->getNumDelims() === 0) {
68 318
                $closerInline->detach();
69 318
                $tempStack = $closer->getNext();
70 318
                $stack->removeDelimiter($closer);
71
72 318
                return $tempStack;
73
            }
74
75 48
            return $closer;
76 1662
        };
77
78
        // Process the emphasis characters
79 1662
        $delimiterStack->iterateByCharacters(['_', '*'], $callback, $stackBottom);
80 1662
    }
81
}
82