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\Element\Text; |
||
18 | use League\CommonMark\Node\Node; |
||
19 | use League\CommonMark\Reference\ReferenceMap; |
||
20 | |||
21 | class InlineParserEngine |
||
22 | { |
||
23 | protected $environment; |
||
24 | |||
25 | 1956 | public function __construct(Environment $environment) |
|
26 | { |
||
27 | 1956 | $this->environment = $environment; |
|
28 | 1956 | } |
|
29 | |||
30 | /** |
||
31 | * @param Node $container |
||
32 | * @param ReferenceMap $referenceMap |
||
33 | */ |
||
34 | 1662 | public function parse(Node $container, ReferenceMap $referenceMap) |
|
35 | { |
||
36 | 1662 | $inlineParserContext = new InlineParserContext($container, $referenceMap); |
|
0 ignored issues
–
show
|
|||
37 | 1662 | while (($character = $inlineParserContext->getCursor()->getCharacter()) !== null) { |
|
38 | 1659 | if (!$this->parseCharacter($character, $inlineParserContext)) { |
|
39 | 1542 | $this->addPlainText($character, $container, $inlineParserContext); |
|
40 | 514 | } |
|
41 | 553 | } |
|
42 | |||
43 | 1662 | $this->processInlines($inlineParserContext); |
|
44 | 1662 | } |
|
45 | |||
46 | /** |
||
47 | * @param string $character |
||
48 | * @param InlineParserContext $inlineParserContext |
||
49 | * |
||
50 | * @return bool Whether we successfully parsed a character at that position |
||
51 | */ |
||
52 | 1659 | protected function parseCharacter($character, InlineParserContext $inlineParserContext) |
|
53 | 1 | { |
|
54 | 1659 | foreach ($this->environment->getInlineParsersForCharacter($character) as $parser) { |
|
55 | 1239 | if ($parser->parse($inlineParserContext)) { |
|
56 | 1228 | return true; |
|
57 | } |
||
58 | 525 | } |
|
59 | |||
60 | 1542 | return false; |
|
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param InlineParserContext $inlineParserContext |
||
65 | */ |
||
66 | 1662 | protected function processInlines(InlineParserContext $inlineParserContext) |
|
67 | { |
||
68 | 1662 | $delimiterStack = $inlineParserContext->getDelimiterStack(); |
|
69 | |||
70 | 1662 | foreach ($this->environment->getInlineProcessors() as $inlineProcessor) { |
|
71 | 1662 | $inlineProcessor->processInlines($delimiterStack); |
|
72 | 554 | } |
|
73 | |||
74 | // Remove all delimiters |
||
75 | 1662 | $delimiterStack->removeAll(); |
|
76 | 1662 | } |
|
77 | |||
78 | /** |
||
79 | * @param string $character |
||
80 | * @param Node $container |
||
81 | * @param InlineParserContext $inlineParserContext |
||
82 | */ |
||
83 | 1542 | private function addPlainText($character, Node $container, InlineParserContext $inlineParserContext) |
|
84 | { |
||
85 | // We reach here if none of the parsers can handle the input |
||
86 | // Attempt to match multiple non-special characters at once |
||
87 | 1542 | $text = $inlineParserContext->getCursor()->match($this->environment->getInlineParserCharacterRegex()); |
|
88 | // This might fail if we're currently at a special character which wasn't parsed; if so, just add that character |
||
89 | 1542 | if ($text === null) { |
|
90 | 171 | $inlineParserContext->getCursor()->advance(); |
|
91 | 171 | $text = $character; |
|
92 | 57 | } |
|
93 | |||
94 | 1542 | $lastInline = $container->lastChild(); |
|
95 | 1542 | if ($lastInline instanceof Text && !isset($lastInline->data['delim'])) { |
|
96 | 204 | $lastInline->append($text); |
|
97 | 68 | } else { |
|
98 | 1509 | $container->appendChild(new Text($text)); |
|
99 | } |
||
100 | 1542 | } |
|
101 | } |
||
102 |
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.