Skip to content

Commit 2ad5df9

Browse files
authored
Merge pull request #451 from greg0ire/avoid-warning
Avoid PHP Warning when parsing empty list item
2 parents 062488f + a661a18 commit 2ad5df9

File tree

5 files changed

+18
-3
lines changed

5 files changed

+18
-3
lines changed

packages/guides-restructured-text/src/RestructuredText/Parser/Productions/ListRule.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use phpDocumentor\Guides\RestructuredText\Parser\Buffer;
2323
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext;
2424
use phpDocumentor\Guides\RestructuredText\Parser\LinesIterator;
25+
use Psr\Log\LoggerInterface;
2526

2627
use function count;
2728
use function ltrim;
@@ -55,8 +56,10 @@ final class ListRule implements Rule
5556
# (or eol, if text starts on a new line)
5657
/ux';
5758

58-
public function __construct(private readonly RuleContainer $productions)
59-
{
59+
public function __construct(
60+
private readonly RuleContainer $productions,
61+
private readonly LoggerInterface $logger,
62+
) {
6063
}
6164

6265
public function applies(DocumentParserContext $documentParser): bool
@@ -168,6 +171,12 @@ private function parseListItem(array $listConfig, Buffer $buffer, DocumentParser
168171
return $listItem;
169172
}
170173

174+
if (!isset($nodes[0])) {
175+
$this->logger->warning('List item without content');
176+
177+
return $listItem;
178+
}
179+
171180
// the list item offset is determined by the offset of the first text
172181
if ($nodes[0] instanceof ParagraphNode) {
173182
return new ListItemNode($listConfig['marker'], false, $nodes[0]->getChildren());

packages/guides-restructured-text/tests/unit/Parser/Productions/ListRuleTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use phpDocumentor\Guides\Nodes\ListNode;
99
use phpDocumentor\Guides\Nodes\RawNode;
1010
use PHPUnit\Framework\Attributes\DataProvider;
11+
use Psr\Log\NullLogger;
1112

1213
final class ListRuleTest extends RuleTestCase
1314
{
@@ -16,7 +17,7 @@ final class ListRuleTest extends RuleTestCase
1617
protected function setUp(): void
1718
{
1819
$ruleContainer = $this->givenCollectAllRuleContainer();
19-
$this->rule = new ListRule($ruleContainer);
20+
$this->rule = new ListRule($ruleContainer, new NullLogger());
2021
}
2122

2223
#[DataProvider('startChars')]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<ul>
2+
<li></li>
3+
</ul>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
WARNING: List item without content
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*

0 commit comments

Comments
 (0)