CommentConverter::convert()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 2
cts 2
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace League\HTMLToMarkdown\Converter;
6
7
use League\HTMLToMarkdown\Configuration;
8
use League\HTMLToMarkdown\ConfigurationAwareInterface;
9
use League\HTMLToMarkdown\ElementInterface;
10
11
class CommentConverter implements ConverterInterface, ConfigurationAwareInterface
12
{
13
    /** @var Configuration */
14
    protected $config;
15
16
    public function setConfig(Configuration $config): void
17
    {
18
        $this->config = $config;
19 99
    }
20
21 99
    public function convert(ElementInterface $element): string
22 99
    {
23
        if ($this->shouldPreserve($element)) {
24
            return '<!--' . $element->getValue() . '-->';
25
        }
26
27
        return '';
28
    }
29 6
30
    /**
31 6
     * @return string[]
32 3
     */
33
    public function getSupportedTags(): array
34 6
    {
35
        return ['#comment'];
36
    }
37
38
    private function shouldPreserve(ElementInterface $element): bool
39
    {
40 99
        $preserve = $this->config->getOption('preserve_comments');
41
        if ($preserve === true) {
42 99
            return true;
43
        }
44
45
        if (\is_array($preserve)) {
46
            $value = \trim($element->getValue());
47
48
            return \in_array($value, $preserve, true);
49
        }
50 6
51
        return false;
52 6
    }
53
}
54