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

TableOfContentsWrapperRenderer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 82.35%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 36
ccs 14
cts 17
cp 0.8235
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getXmlAttributes() 0 3 1
A render() 0 16 2
A getXmlTagName() 0 3 1
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 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
     * @throws InvalidArgumentException
29
     */
30 2
    public function render(Node $node, ChildNodeRendererInterface $childRenderer)
31
    {
32 2
        TableOfContentsWrapper::assertInstanceOf($node);
33 2
        $children = $node->children();
34 2
        if (\count($children) !== 2) {
35
            throw new InvalidArgumentException(
36
                'TableOfContentsWrapper nodes should have 2 children, found ' . \count($children)
37
            );
38
        }
39
40 2
        $attrs = $node->data->get('attributes');
41
42 2
        return new HtmlElement(
43 2
            'div',
44 2
            $attrs,
45 2
            $childRenderer->renderNodes($children)
46 2
        );
47
    }
48
49 2
    public function getXmlTagName(Node $node): string
50
    {
51 2
        return 'table_of_contents_wrapper';
52
    }
53
54
    /**
55
     * @return array<string, scalar>
56
     */
57 2
    public function getXmlAttributes(Node $node): array
58
    {
59 2
        return [];
60
    }
61
}
62