Passed
Push — main ( 989696...2152d5 )
by Colin
05:14 queued 02:09
created

ParagraphRenderer::getXmlTagName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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
 * 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\Renderer\Block;
18
19
use League\CommonMark\Node\Block\Paragraph;
20
use League\CommonMark\Node\Block\TightBlockInterface;
21
use League\CommonMark\Node\Node;
22
use League\CommonMark\Renderer\ChildNodeRendererInterface;
23
use League\CommonMark\Renderer\NodeRendererInterface;
24
use League\CommonMark\Util\HtmlElement;
25
use League\CommonMark\Xml\XmlNodeRendererInterface;
26
27
final class ParagraphRenderer implements NodeRendererInterface, XmlNodeRendererInterface
28
{
29
    /**
30
     * @param Paragraph $node
31
     *
32
     * {@inheritDoc}
33
     *
34
     * @psalm-suppress MoreSpecificImplementedParamType
35
     */
36 2481
    public function render(Node $node, ChildNodeRendererInterface $childRenderer)
37
    {
38 2481
        Paragraph::assertInstanceOf($node);
39
40 2478
        if ($this->inTightList($node)) {
41 198
            return $childRenderer->renderNodes($node->children());
42
        }
43
44 2346
        $attrs = $node->data->get('attributes');
45
46 2346
        return new HtmlElement('p', $attrs, $childRenderer->renderNodes($node->children()));
47
    }
48
49 102
    public function getXmlTagName(Node $node): string
50
    {
51 102
        return 'paragraph';
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57 102
    public function getXmlAttributes(Node $node): array
58
    {
59 102
        return [];
60
    }
61
62 2478
    private function inTightList(Paragraph $node): bool
63
    {
64
        // Only check up to two (2) levels above this for tightness
65 2478
        $i = 2;
66 2478
        while (($node = $node->parent()) && $i--) {
67 2475
            if ($node instanceof TightBlockInterface) {
68 285
                return $node->isTight();
69
            }
70
        }
71
72 2247
        return false;
73
    }
74
}
75