Completed
Push — master ( 29802b...e46319 )
by Martin
17:22
created

TwigRenderer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 22.99 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 20
loc 87
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getOption() 0 4 1
A renderInline() 10 10 1
A renderInlines() 0 9 2
A renderBlock() 10 10 1
A renderBlocks() 0 11 2
A render() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This is part of the webuni/commonmark-twig-renderer package.
5
 *
6
 * (c) Martin Hasoň <[email protected]>
7
 * (c) Webuni s.r.o. <[email protected]>
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
namespace Webuni\CommonMark\TwigRenderer;
14
15
use League\CommonMark\Block\Element\AbstractBlock;
16
use League\CommonMark\ElementRendererInterface;
17
use League\CommonMark\Environment;
18
use League\CommonMark\Inline\Element\AbstractInline;
19
use Twig\Environment as Twig;
20
21
class TwigRenderer implements ElementRendererInterface
22
{
23
    private $environment;
24
    private $twig;
25
    private $templateName;
26
    private $defaultTemplate;
27
28
    public function __construct(Environment $environment, Twig $twig, $template = 'commonmark.html.twig')
29
    {
30
        $this->environment = $environment;
31
        $this->twig = $twig;
32
        $this->defaultTemplate = $template;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getOption(string $option, $default = null)
39
    {
40
        return $this->environment->getConfig('renderer/' . $option, $default);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 View Code Duplication
    public function renderInline(AbstractInline $inline): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        $options = $this->environment->getConfig('renderer', []);
49
50
        return $this->render([
51
            'node' => $inline,
52
            'in_tight_list' => false,
53
            'options' => $options,
54
        ]);
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function renderInlines(iterable $inlines): string
61
    {
62
        $result = [];
63
        foreach ($inlines as $inline) {
64
            $result[] = $this->renderInline($inline);
65
        }
66
67
        return \implode('', $result);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 View Code Duplication
    public function renderBlock(AbstractBlock $block, $inTightList = false): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
    {
75
        $options = $this->environment->getConfig('renderer', []);
76
77
        return $this->render([
78
            'node' => $block,
79
            'in_tight_list' => $inTightList,
80
            'options' => $options,
81
        ]);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function renderBlocks(iterable $blocks, bool $inTightList = false): string
88
    {
89
        $result = [];
90
        foreach ($blocks as $block) {
91
            $result[] = $this->renderBlock($block, $inTightList);
92
        }
93
94
        $separator = $this->getOption('block_separator', "\n");
95
96
        return \implode($separator, $result);
97
    }
98
99
    private function render(array $context): string
100
    {
101
        if (null === $this->templateName) {
102
            $this->templateName = $this->getOption('twig_template', $this->defaultTemplate);
103
        }
104
105
        return $this->twig->render($this->templateName, $context);
106
    }
107
}
108