Passed
Pull Request — main (#1074)
by
unknown
02:27
created

TableOfContentsWrapperRenderer::render()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.2098

Importance

Changes 5
Bugs 2 Features 0
Metric Value
cc 3
eloc 12
c 5
b 2
f 0
nc 4
nop 2
dl 0
loc 21
ccs 10
cts 14
cp 0.7143
crap 3.2098
rs 9.8666
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\TableOfContents;
15
16
use League\CommonMark\Exception\InvalidArgumentException;
17
use League\CommonMark\Extension\TableOfContents\Node\TableOfContentsWrapper;
18
use League\CommonMark\Node\Node;
19
use League\CommonMark\Renderer\ChildNodeRendererInterface;
20
use League\CommonMark\Renderer\NodeRendererInterface;
21
use League\CommonMark\Util\HtmlElement;
22
use League\CommonMark\Xml\XmlNodeRendererInterface;
23
24
final class TableOfContentsWrapperRenderer implements NodeRendererInterface, XmlNodeRendererInterface
25
{
26
    /**
27
     * {@inheritDoc}
28
     */
29 2
    public function render(Node $node, ChildNodeRendererInterface $childRenderer)
30
    {
31 2
        TableOfContentsWrapper::assertInstanceOf($node);
32 2
        $children = $node->children();
33 2
        if (! \is_array($children)) {
0 ignored issues
show
introduced by
The condition is_array($children) is always true.
Loading history...
34
            /** @psalm-suppress NoValue */
35
            $children = \iterator_to_array($children);
36
        }
37
38 2
        if (\count($children) !== 2) {
39
            throw new InvalidArgumentException(
40
                'TableOfContentsWrapper nodes should have 2 children, found ' . \count($children)
41
            );
42
        }
43
44 2
        $attrs = $node->data->get('attributes');
45
46 2
        return new HtmlElement(
47 2
            'div',
48 2
            $attrs,
49 2
            $childRenderer->renderNodes($children)
50 2
        );
51
    }
52
53 2
    public function getXmlTagName(Node $node): string
54
    {
55 2
        return 'table_of_contents_wrapper';
56
    }
57
58
    /**
59
     * @return array<string, scalar>
60
     */
61 2
    public function getXmlAttributes(Node $node): array
62
    {
63 2
        return [];
64
    }
65
}
66