Completed
Push — 1.5 ( f40f61...09e907 )
by Colin
02:28
created

FootnoteRenderer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 40
ccs 16
cts 17
cp 0.9412
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 22 3
A setConfiguration() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 * (c) Rezo Zero / Ambroise Maupate
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace League\CommonMark\Extension\Footnote\Renderer;
16
17
use League\CommonMark\Block\Element\AbstractBlock;
18
use League\CommonMark\Block\Renderer\BlockRendererInterface;
19
use League\CommonMark\ElementRendererInterface;
20
use League\CommonMark\Extension\Footnote\Node\Footnote;
21
use League\CommonMark\HtmlElement;
22
use League\CommonMark\Util\ConfigurationAwareInterface;
23
use League\CommonMark\Util\ConfigurationInterface;
24
25
final class FootnoteRenderer implements BlockRendererInterface, ConfigurationAwareInterface
26
{
27
    /** @var ConfigurationInterface */
28
    private $config;
29
30
    /**
31
     * @param Footnote                 $block
32
     * @param ElementRendererInterface $htmlRenderer
33
     * @param bool                     $inTightList
34
     *
35
     * @return HtmlElement
36
     */
37 48
    public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, bool $inTightList = false)
38
    {
39 48
        if (!($block instanceof Footnote)) {
40
            throw new \InvalidArgumentException('Incompatible block type: ' . \get_class($block));
41
        }
42
43 48
        $attrs = $block->getData('attributes', []);
44 48
        $attrs['class'] = $attrs['class'] ?? $this->config->get('footnote/footnote_class', 'footnote');
45 48
        $attrs['id'] = $this->config->get('footnote/footnote_id_prefix', 'fn:') . \mb_strtolower($block->getReference()->getLabel());
46 48
        $attrs['role'] = 'doc-endnote';
47
48 48
        foreach ($block->getBackrefs() as $backref) {
49 36
            $block->lastChild()->appendChild($backref);
50
        }
51
52 48
        return new HtmlElement(
53 48
            'li',
54 16
            $attrs,
55 48
            $htmlRenderer->renderBlocks($block->children()),
56 48
            true
57
        );
58
    }
59
60 54
    public function setConfiguration(ConfigurationInterface $configuration)
61
    {
62 54
        $this->config = $configuration;
63 54
    }
64
}
65