Completed
Push — master ( 4475f0...d6425f )
by Colin
102:20 queued 70:50
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 1947
26
    public function __construct(EnvironmentInterface $environment)
27 1947
    {
28 1947
        $this->environment = $environment;
29
    }
30
31
    /**
32
     * @param Node         $container
33
     * @param ReferenceMap $referenceMap
34 1662
     */
35
    public function parse(Node $container, ReferenceMap $referenceMap)
36 1662
    {
37 1662
        $inlineParserContext = new InlineParserContext($container, $referenceMap);
0 ignored issues
show
Compatibility introduced by Sergey Protko
$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 1659
        while (($character = $inlineParserContext->getCursor()->getCharacter()) !== null) {
39 1542
            if (!$this->parseCharacter($character, $inlineParserContext)) {
40
                $this->addPlainText($character, $container, $inlineParserContext);
41
            }
42
        }
43 1662
44
        $this->processInlines($inlineParserContext);
45 1662
46 1662
        AdjoiningTextCollapser::collapseTextNodes($container);
47
    }
48
49
    /**
50
     * @param string              $character
51
     * @param InlineParserContext $inlineParserContext
52
     *
53
     * @return bool Whether we successfully parsed a character at that position
54 1659
     */
55
    protected function parseCharacter(string $character, InlineParserContext $inlineParserContext): bool
56 1659
    {
57 1239
        foreach ($this->environment->getInlineParsersForCharacter($character) as $parser) {
58 1217
            if ($parser->parse($inlineParserContext)) {
59
                return true;
60
            }
61
        }
62 1542
63
        return false;
64
    }
65
66
    /**
67
     * @param InlineParserContext $inlineParserContext
68 1662
     */
69
    protected function processInlines(InlineParserContext $inlineParserContext)
70 1662
    {
71
        $delimiterStack = $inlineParserContext->getDelimiterStack();
72 1662
73 1662
        foreach ($this->environment->getInlineProcessors() as $inlineProcessor) {
74
            $inlineProcessor->processInlines($delimiterStack);
75
        }
76
77 1662
        // Remove all delimiters
78 1662
        $delimiterStack->removeAll();
79
    }
80
81
    /**
82
     * @param string              $character
83
     * @param Node                $container
84
     * @param InlineParserContext $inlineParserContext
85 1542
     */
86
    private function addPlainText(string $character, Node $container, InlineParserContext $inlineParserContext)
87
    {
88
        // We reach here if none of the parsers can handle the input
89 1542
        // Attempt to match multiple non-special characters at once
90
        $text = $inlineParserContext->getCursor()->match($this->environment->getInlineParserCharacterRegex());
91 1542
        // This might fail if we're currently at a special character which wasn't parsed; if so, just add that character
92 177
        if ($text === null) {
93 177
            $inlineParserContext->getCursor()->advance();
94
            $text = $character;
95
        }
96 1542
97 1542
        $lastInline = $container->lastChild();
98 210
        if ($lastInline instanceof Text && !isset($lastInline->data['delim'])) {
99
            $lastInline->append($text);
100 1509
        } else {
101
            $container->appendChild(new Text($text));
102 1542
        }
103
    }
104
}
105