ListItemRenderer   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 50
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getXmlAttributes() 0 3 1
A getXmlTagName() 0 3 1
A render() 0 19 5
A needsBlockSeparator() 0 7 3
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
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
11
 *  - (c) John MacFarlane
12
 *
13
 * For the full copyright and license information, please view the LICENSE
14
 * file that was distributed with this source code.
15
 */
16
17
namespace League\CommonMark\Extension\CommonMark\Renderer\Block;
18
19
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
20
use League\CommonMark\Node\Block\AbstractBlock;
21
use League\CommonMark\Node\Block\Paragraph;
22
use League\CommonMark\Node\Block\TightBlockInterface;
23
use League\CommonMark\Node\Node;
24
use League\CommonMark\Renderer\ChildNodeRendererInterface;
25
use League\CommonMark\Renderer\NodeRendererInterface;
26
use League\CommonMark\Util\HtmlElement;
27
use League\CommonMark\Xml\XmlNodeRendererInterface;
28
29
final class ListItemRenderer implements NodeRendererInterface, XmlNodeRendererInterface
30
{
31
    /**
32
     * @param ListItem $node
33
     *
34
     * {@inheritDoc}
35
     *
36
     * @psalm-suppress MoreSpecificImplementedParamType
37
     */
38 232
    public function render(Node $node, ChildNodeRendererInterface $childRenderer): \Stringable
39
    {
40 232
        ListItem::assertInstanceOf($node);
41
42 230
        $contents = $childRenderer->renderNodes($node->children());
43
44 230
        $inTightList = ($parent = $node->parent()) && $parent instanceof TightBlockInterface && $parent->isTight();
45
46 230
        if ($this->needsBlockSeparator($node->firstChild(), $inTightList)) {
47 102
            $contents = "\n" . $contents;
48
        }
49
50 230
        if ($this->needsBlockSeparator($node->lastChild(), $inTightList)) {
51 142
            $contents .= "\n";
52
        }
53
54 230
        $attrs = $node->data->get('attributes');
55
56 230
        return new HtmlElement('li', $attrs, $contents);
57
    }
58
59 30
    public function getXmlTagName(Node $node): string
60
    {
61 30
        return 'item';
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67 30
    public function getXmlAttributes(Node $node): array
68
    {
69 30
        return [];
70
    }
71
72 230
    private function needsBlockSeparator(?Node $child, bool $inTightList): bool
73
    {
74 230
        if ($child instanceof Paragraph && $inTightList) {
75 132
            return false;
76
        }
77
78 160
        return $child instanceof AbstractBlock;
79
    }
80
}
81