1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * This file is part of the league/commonmark package. |
||
7 | * |
||
8 | * (c) Colin O'Dell <[email protected]> |
||
9 | * |
||
10 | * For the full copyright and license information, please view the LICENSE |
||
11 | * file that was distributed with this source code. |
||
12 | */ |
||
13 | |||
14 | namespace League\CommonMark\Extension\CommonMark\Parser\Block; |
||
15 | |||
16 | use League\CommonMark\Configuration\ConfigurationAwareInterface; |
||
17 | use League\CommonMark\Configuration\ConfigurationInterface; |
||
18 | use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock; |
||
19 | use League\CommonMark\Extension\CommonMark\Node\Block\ListData; |
||
20 | use League\CommonMark\Parser\Block\BlockStart; |
||
21 | use League\CommonMark\Parser\Block\BlockStartParserInterface; |
||
22 | use League\CommonMark\Parser\Cursor; |
||
23 | use League\CommonMark\Parser\MarkdownParserStateInterface; |
||
24 | use League\CommonMark\Util\RegexHelper; |
||
25 | |||
26 | final class ListBlockStartParser implements BlockStartParserInterface, ConfigurationAwareInterface |
||
27 | { |
||
28 | /** |
||
29 | * @var ConfigurationInterface|null |
||
30 | * |
||
31 | * @psalm-readonly-allow-private-mutation |
||
32 | */ |
||
33 | private $config; |
||
34 | |||
35 | /** |
||
36 | * @var string|null |
||
37 | * |
||
38 | * @psalm-readonly-allow-private-mutation |
||
39 | */ |
||
40 | private $listMarkerRegex; |
||
41 | |||
42 | 2901 | public function setConfiguration(ConfigurationInterface $configuration): void |
|
43 | { |
||
44 | 2901 | $this->config = $configuration; |
|
45 | 2901 | } |
|
46 | |||
47 | 1710 | public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart |
|
48 | { |
||
49 | 1710 | if ($cursor->isIndented()) { |
|
50 | 186 | return BlockStart::none(); |
|
1 ignored issue
–
show
|
|||
51 | } |
||
52 | |||
53 | 1590 | $listData = $this->parseList($cursor, $parserState->getParagraphContent() !== null); |
|
54 | 1587 | if ($listData === null) { |
|
55 | 1323 | return BlockStart::none(); |
|
1 ignored issue
–
show
Are you sure the usage of
League\CommonMark\Parser\Block\BlockStart::none() targeting League\CommonMark\Parser\Block\BlockStart::none() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.
Loading history...
|
|||
56 | } |
||
57 | |||
58 | 288 | $listItemParser = new ListItemParser($listData); |
|
59 | |||
60 | // prepend the list block if needed |
||
61 | 288 | $matched = $parserState->getLastMatchedBlockParser(); |
|
62 | 288 | if (! ($matched instanceof ListBlockParser) || ! $listData->equals($matched->getBlock()->getListData())) { |
|
63 | 288 | $listBlockParser = new ListBlockParser($listData); |
|
64 | // We start out with assuming a list is tight. If we find a blank line, we set it to loose later. |
||
65 | 288 | $listBlockParser->getBlock()->setTight(true); |
|
66 | |||
67 | 288 | return BlockStart::of($listBlockParser, $listItemParser)->at($cursor); |
|
68 | } |
||
69 | |||
70 | 102 | return BlockStart::of($listItemParser)->at($cursor); |
|
71 | } |
||
72 | |||
73 | 1590 | private function parseList(Cursor $cursor, bool $inParagraph): ?ListData |
|
74 | { |
||
75 | 1590 | $indent = $cursor->getIndent(); |
|
76 | |||
77 | 1590 | $tmpCursor = clone $cursor; |
|
78 | 1590 | $tmpCursor->advanceToNextNonSpaceOrTab(); |
|
79 | 1590 | $rest = $tmpCursor->getRemainder(); |
|
80 | |||
81 | 1590 | if (\preg_match($this->listMarkerRegex ?? $this->generateListMarkerRegex(), $rest) === 1) { |
|
82 | 483 | $data = new ListData(); |
|
83 | 483 | $data->markerOffset = $indent; |
|
84 | 483 | $data->type = ListBlock::TYPE_BULLET; |
|
85 | 483 | $data->delimiter = null; |
|
86 | 483 | $data->bulletChar = $rest[0]; |
|
87 | 483 | $markerLength = 1; |
|
88 | 1173 | } elseif (($matches = RegexHelper::matchFirst('/^(\d{1,9})([.)])/', $rest)) && (! $inParagraph || $matches[1] === '1')) { |
|
89 | 96 | $data = new ListData(); |
|
90 | 96 | $data->markerOffset = $indent; |
|
91 | 96 | $data->type = ListBlock::TYPE_ORDERED; |
|
92 | 96 | $data->start = (int) $matches[1]; |
|
93 | 96 | $data->delimiter = $matches[2]; |
|
94 | 96 | $data->bulletChar = null; |
|
95 | 96 | $markerLength = \strlen($matches[0]); |
|
96 | } else { |
||
97 | 1080 | return null; |
|
98 | } |
||
99 | |||
100 | // Make sure we have spaces after |
||
101 | 555 | $nextChar = $tmpCursor->peek($markerLength); |
|
102 | 555 | if (! ($nextChar === null || $nextChar === "\t" || $nextChar === ' ')) { |
|
103 | 270 | return null; |
|
104 | } |
||
105 | |||
106 | // If it interrupts paragraph, make sure first line isn't blank |
||
107 | 294 | if ($inParagraph && ! RegexHelper::matchAt(RegexHelper::REGEX_NON_SPACE, $rest, $markerLength)) { |
|
0 ignored issues
–
show
The expression
League\CommonMark\Util\R..., $rest, $markerLength) of type integer|null is loosely compared to false ; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.
In PHP, under loose comparison (like For 0 == false // true
0 == null // true
123 == false // false
123 == null // false
// It is often better to use strict comparison
0 === false // false
0 === null // false
Loading history...
|
|||
108 | 6 | return null; |
|
109 | } |
||
110 | |||
111 | 288 | $cursor->advanceToNextNonSpaceOrTab(); // to start of marker |
|
112 | 288 | $cursor->advanceBy($markerLength, true); // to end of marker |
|
113 | 288 | $data->padding = self::calculateListMarkerPadding($cursor, $markerLength); |
|
114 | |||
115 | 288 | return $data; |
|
116 | } |
||
117 | |||
118 | 288 | private static function calculateListMarkerPadding(Cursor $cursor, int $markerLength): int |
|
119 | { |
||
120 | 288 | $start = $cursor->saveState(); |
|
121 | 288 | $spacesStartCol = $cursor->getColumn(); |
|
122 | |||
123 | 288 | while ($cursor->getColumn() - $spacesStartCol < 5) { |
|
124 | 288 | if (! $cursor->advanceBySpaceOrTab()) { |
|
125 | 279 | break; |
|
126 | } |
||
127 | } |
||
128 | |||
129 | 288 | $blankItem = $cursor->peek() === null; |
|
130 | 288 | $spacesAfterMarker = $cursor->getColumn() - $spacesStartCol; |
|
131 | |||
132 | 288 | if ($spacesAfterMarker >= 5 || $spacesAfterMarker < 1 || $blankItem) { |
|
133 | 81 | $cursor->restoreState($start); |
|
134 | 81 | $cursor->advanceBySpaceOrTab(); |
|
135 | |||
136 | 81 | return $markerLength + 1; |
|
137 | } |
||
138 | |||
139 | 225 | return $markerLength + $spacesAfterMarker; |
|
140 | } |
||
141 | |||
142 | 1590 | private function generateListMarkerRegex(): string |
|
143 | { |
||
144 | // No configuration given - use the defaults |
||
145 | 1590 | if ($this->config === null) { |
|
146 | 15 | return $this->listMarkerRegex = '/^[*+-]/'; |
|
147 | } |
||
148 | |||
149 | 1575 | $markers = $this->config->get('unordered_list_markers', ['*', '+', '-']); |
|
150 | |||
151 | 1575 | if (! \is_array($markers)) { |
|
152 | 3 | throw new \RuntimeException('Invalid configuration option "unordered_list_markers": value must be an array of strings'); |
|
153 | } |
||
154 | |||
155 | 1572 | return $this->listMarkerRegex = '/^[' . \preg_quote(\implode('', $markers), '/') . ']/'; |
|
156 | } |
||
157 | } |
||
158 |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.