Completed
Push — 0.18.x ( c34984 )
by Colin
09:11 queued 05:19
created

InlineParserEngine::collapseAdjoiningTextElements()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.1111
c 0
b 0
f 0
cc 6
nc 5
nop 1
crap 6
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;
16
17
use League\CommonMark\Inline\AdjoiningTextCollapser;
18
use League\CommonMark\Inline\Element\Text;
19
use League\CommonMark\Node\Node;
20
use League\CommonMark\Reference\ReferenceMap;
21
22
class InlineParserEngine
23
{
24
    protected $environment;
25
26 1956
    public function __construct(Environment $environment)
27
    {
28 1956
        $this->environment = $environment;
29 1956
    }
30
31
    /**
32
     * @param Node         $container
33
     * @param ReferenceMap $referenceMap
34
     */
35 1662
    public function parse(Node $container, ReferenceMap $referenceMap)
36
    {
37 1662
        $inlineParserContext = new InlineParserContext($container, $referenceMap);
0 ignored issues
show
Compatibility introduced by
$container of type object<League\CommonMark\Node\Node> is not a sub-type of object<League\CommonMark...\Element\AbstractBlock>. It seems like you assume a child class of the class League\CommonMark\Node\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
38 1662
        while (($character = $inlineParserContext->getCursor()->getCharacter()) !== null) {
39 1659
            if (!$this->parseCharacter($character, $inlineParserContext)) {
40 1542
                $this->addPlainText($character, $container, $inlineParserContext);
41 514
            }
42 553
        }
43
44 1662
        $this->processInlines($inlineParserContext);
45
46 1662
        AdjoiningTextCollapser::collapseTextNodes($container);
47 1662
    }
48
49
    /**
50
     * @param string              $character
51
     * @param InlineParserContext $inlineParserContext
52
     *
53
     * @return bool Whether we successfully parsed a character at that position
54
     */
55 1659
    protected function parseCharacter($character, InlineParserContext $inlineParserContext)
56
    {
57 1659
        foreach ($this->environment->getInlineParsersForCharacter($character) as $parser) {
58 1239
            if ($parser->parse($inlineParserContext)) {
59 1228
                return true;
60
            }
61 525
        }
62
63 1542
        return false;
64
    }
65
66
    /**
67
     * @param InlineParserContext $inlineParserContext
68
     */
69 1662
    protected function processInlines(InlineParserContext $inlineParserContext)
70
    {
71 1662
        $delimiterStack = $inlineParserContext->getDelimiterStack();
72
73 1662
        foreach ($this->environment->getInlineProcessors() as $inlineProcessor) {
74 1662
            $inlineProcessor->processInlines($delimiterStack);
75 554
        }
76
77
        // Remove all delimiters
78 1662
        $delimiterStack->removeAll();
79 1662
    }
80
81
    /**
82
     * @param string              $character
83
     * @param Node                $container
84
     * @param InlineParserContext $inlineParserContext
85
     */
86 1542
    private function addPlainText($character, Node $container, InlineParserContext $inlineParserContext)
87
    {
88
        // We reach here if none of the parsers can handle the input
89
        // Attempt to match multiple non-special characters at once
90 1542
        $text = $inlineParserContext->getCursor()->match($this->environment->getInlineParserCharacterRegex());
91
        // This might fail if we're currently at a special character which wasn't parsed; if so, just add that character
92 1542
        if ($text === null) {
93 177
            $inlineParserContext->getCursor()->advance();
94 177
            $text = $character;
95 59
        }
96
97 1542
        $lastInline = $container->lastChild();
98 1542
        if ($lastInline instanceof Text && !isset($lastInline->data['delim'])) {
99 210
            $lastInline->append($text);
100 70
        } else {
101 1509
            $container->appendChild(new Text($text));
102
        }
103 1542
    }
104
}
105