Completed
Push — master ( 9bba17...b2b071 )
by Colin
09:14 queued 05:11
created

ListItemConverter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 5
c 2
b 1
f 0
lcom 0
cbo 1
dl 0
loc 39
ccs 16
cts 16
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSupportedTags() 0 4 1
B convert() 0 23 4
1
<?php
2
3
namespace League\HTMLToMarkdown\Converter;
4
5
use League\HTMLToMarkdown\ElementInterface;
6
7
class ListItemConverter implements ConverterInterface
8
{
9
    /**
10
     * @param ElementInterface $element
11
     *
12
     * @return string
13
     */
14 6
    public function convert(ElementInterface $element)
15
    {
16
        // If parent is an ol, use numbers, otherwise, use dashes
17 6
        $list_type = $element->getParent()->getTagName();
18 6
        $value = $element->getValue();
19
20
        // Add spaces to start for nested list items
21 6
        $level = $element->getListItemLevel($element);
22 6
        $prefix = str_repeat('  ', $level);
23
        // If list item is the first in a nested list, add a newline before it
24 6
        if ($level > 0 && $element->getSiblingPosition() === 1) {
25 3
            $prefix = "\n" . $prefix;
26 3
        }
27
28 6
        if ($list_type === 'ul') {
29 6
            $markdown = $prefix . '- ' . trim($value) . "\n";
30 6
        } else {
31 6
            $number = $element->getSiblingPosition();
32 6
            $markdown = $prefix . $number . '. ' . trim($value) . "\n";
33
        }
34
35 6
        return $markdown;
36
    }
37
38
    /**
39
     * @return string[]
40
     */
41 69
    public function getSupportedTags()
42
    {
43 69
        return array('li');
44
    }
45
}
46