ListItemRenderer::render()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 9.6
c 0
b 0
f 0
cc 4
nc 5
nop 3
crap 4
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\Block\Renderer;
16
17
use League\CommonMark\Block\Element\AbstractBlock;
18
use League\CommonMark\Block\Element\ListItem;
19
use League\CommonMark\ElementRendererInterface;
20
use League\CommonMark\HtmlElement;
21
22
final class ListItemRenderer implements BlockRendererInterface
23
{
24
    /**
25
     * @param ListItem                 $block
26
     * @param ElementRendererInterface $htmlRenderer
27
     * @param bool                     $inTightList
28
     *
29
     * @return string
30
     */
31 252
    public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, bool $inTightList = false)
32
    {
33 252
        if (!($block instanceof ListItem)) {
34 3
            throw new \InvalidArgumentException('Incompatible block type: ' . \get_class($block));
35
        }
36
37 249
        $contents = $htmlRenderer->renderBlocks($block->children(), $inTightList);
38 249
        if (\substr($contents, 0, 1) === '<') {
39 132
            $contents = "\n" . $contents;
40
        }
41 249
        if (\substr($contents, -1, 1) === '>') {
42 150
            $contents .= "\n";
43
        }
44
45 249
        $attrs = $block->getData('attributes', []);
46
47 249
        $li = new HtmlElement('li', $attrs, $contents);
48
49 249
        return $li;
50
    }
51
}
52