Completed
Push — master ( b06b97...f73407 )
by Colin
16s
created

EmphasisProcessor::processInlines()   C

Complexity

Conditions 12
Paths 1

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 12.004

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 32
cts 33
cp 0.9697
rs 6.5333
c 0
b 0
f 0
cc 12
nc 1
nop 2
crap 12.004

How to fix   Long Method    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
 * 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 1662
    public function processInlines(DelimiterStack $delimiterStack, Delimiter $stackBottom = null)
26
    {
27
        $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 150
            } elseif ($useDelims > 1 && $openerInline->data['emphasis_config']->getConfig('enable_strong')) {
45 150
                $emph = new Strong();
46
            } 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
            }
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
            }
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
            }
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