Completed
Push — master ( fbf2a7...00045a )
by Colin
02:48
created

src/InlineParserEngine.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 2037
    public function __construct(EnvironmentInterface $environment)
27
    {
28 2037
        $this->environment = $environment;
29 2037
    }
30
31
    /**
32
     * @param Node         $container
33
     * @param ReferenceMap $referenceMap
34
     */
35 1743
    public function parse(Node $container, ReferenceMap $referenceMap)
36
    {
37 1743
        $inlineParserContext = new InlineParserContext($container, $referenceMap);
0 ignored issues
show
$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 1743
        while (($character = $inlineParserContext->getCursor()->getCharacter()) !== null) {
39 1740
            if (!$this->parseCharacter($character, $inlineParserContext)) {
40 1608
                $this->addPlainText($character, $container, $inlineParserContext);
41
            }
42
        }
43
44 1743
        $this->processInlines($inlineParserContext);
45
46 1743
        AdjoiningTextCollapser::collapseTextNodes($container);
47 1743
    }
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 1740
    protected function parseCharacter(string $character, InlineParserContext $inlineParserContext): bool
56
    {
57 1740
        foreach ($this->environment->getInlineParsersForCharacter($character) as $parser) {
58 1317
            if ($parser->parse($inlineParserContext)) {
59 1295
                return true;
60
            }
61
        }
62
63 1608
        return false;
64
    }
65
66
    /**
67
     * @param InlineParserContext $inlineParserContext
68
     */
69 1743
    protected function processInlines(InlineParserContext $inlineParserContext)
70
    {
71 1743
        $delimiterStack = $inlineParserContext->getDelimiterStack();
72
73 1743
        foreach ($this->environment->getInlineProcessors() as $inlineProcessor) {
74 1743
            $inlineProcessor->processInlines($delimiterStack);
75
        }
76
77
        // Remove all delimiters
78 1743
        $delimiterStack->removeAll();
79 1743
    }
80
81
    /**
82
     * @param string              $character
83
     * @param Node                $container
84
     * @param InlineParserContext $inlineParserContext
85
     */
86 1608
    private function addPlainText(string $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 1608
        $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 1608
        if ($text === null) {
93 192
            $inlineParserContext->getCursor()->advance();
94 192
            $text = $character;
95
        }
96
97 1608
        $lastInline = $container->lastChild();
98 1608
        if ($lastInline instanceof Text && !isset($lastInline->data['delim'])) {
99 237
            $lastInline->append($text);
100
        } else {
101 1572
            $container->appendChild(new Text($text));
102
        }
103 1608
    }
104
}
105