Skip to content

Commit

Permalink
Improve handling of empty lists
Browse files Browse the repository at this point in the history
With this change users will get a psr/log warning instead of the PHP
Warning they were getting before.
  • Loading branch information
greg0ire committed Jul 4, 2023
1 parent 86aa1e7 commit 94b4c31
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use phpDocumentor\Guides\RestructuredText\Parser\Buffer;
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\LinesIterator;
use Psr\Log\LoggerInterface;

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

public function __construct(private readonly RuleContainer $productions)
{
public function __construct(
private readonly RuleContainer $productions,
private readonly LoggerInterface $logger,
) {
}

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

if (!isset($nodes[0])) {
$this->logger->warning('List item without content');

return $listItem;
}

// the list item offset is determined by the offset of the first text
if ($nodes[0] instanceof ParagraphNode) {
return new ListItemNode($listConfig['marker'], false, $nodes[0]->getChildren());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use phpDocumentor\Guides\Nodes\ListNode;
use phpDocumentor\Guides\Nodes\RawNode;
use PHPUnit\Framework\Attributes\DataProvider;
use Psr\Log\NullLogger;

final class ListRuleTest extends RuleTestCase
{
Expand All @@ -16,7 +17,7 @@ final class ListRuleTest extends RuleTestCase
protected function setUp(): void
{
$ruleContainer = $this->givenCollectAllRuleContainer();
$this->rule = new ListRule($ruleContainer);
$this->rule = new ListRule($ruleContainer, new NullLogger());
}

#[DataProvider('startChars')]
Expand Down
3 changes: 3 additions & 0 deletions tests/Functional/tests/empty-list/empty-list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<ul>
<li></li>
</ul>
1 change: 1 addition & 0 deletions tests/Functional/tests/empty-list/empty-list.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
WARNING: List item without content
1 change: 1 addition & 0 deletions tests/Functional/tests/empty-list/empty-list.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*

0 comments on commit 94b4c31

Please sign in to comment.