Completed
Push — master ( e25879...032337 )
by Colin
01:39
created

ListItemConverter::convert()   C

Complexity

Conditions 12
Paths 22

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 39
ccs 24
cts 24
cp 1
rs 5.1612
cc 12
eloc 21
nc 22
nop 1
crap 12

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace League\HTMLToMarkdown\Converter;
4
5
use League\HTMLToMarkdown\Configuration;
6
use League\HTMLToMarkdown\ConfigurationAwareInterface;
7
use League\HTMLToMarkdown\ElementInterface;
8
9
class ListItemConverter implements ConverterInterface, ConfigurationAwareInterface
10
{
11
    /**
12
     * @var Configuration
13
     */
14
    protected $config;
15
16
    /**
17
     * @var string
18
     */
19
    protected $listItemStyle;
20
21
    /**
22
     * @param Configuration $config
23
     */
24 84
    public function setConfig(Configuration $config)
25
    {
26 84
        $this->config = $config;
27 84
    }
28
29
    /**
30
     * @param ElementInterface $element
31
     *
32
     * @return string
33
     */
34 9
    public function convert(ElementInterface $element)
35
    {
36
        // If parent is an ol, use numbers, otherwise, use dashes
37 9
        $list_type = $element->getParent()->getTagName();
38
39
        // Add spaces to start for nested list items
40 9
        $level = $element->getListItemLevel($element);
41
42 9
        $prefixForParagraph = str_repeat('  ', $level + 1);
43 9
        $value = trim(implode("\n" . $prefixForParagraph, explode("\n", trim($element->getValue()))));
44
45
        // If list item is the first in a nested list, add a newline before it
46 9
        $prefix = '';
47 9
        if ($level > 0 && $element->getSiblingPosition() === 1) {
48 3
            $prefix = "\n";
49 2
        }
50
51 9
        if ($list_type === 'ul') {
52 9
            $list_item_style = $this->config->getOption('list_item_style', '-');
53 9
            $list_item_style_alternate = $this->config->getOption('list_item_style_alternate');
54 9
            if (!isset($this->listItemStyle)) {
55 9
                $this->listItemStyle = $list_item_style_alternate ? $list_item_style_alternate : $list_item_style;
56 6
            }
57
58 9
            if ($list_item_style_alternate && $level == 0 && $element->getSiblingPosition() === 1) {
59 3
                $this->listItemStyle = $this->listItemStyle == $list_item_style ? $list_item_style_alternate : $list_item_style;
60 2
            }
61
62 9
            return $prefix . $this->listItemStyle . ' ' . $value . "\n";
63
        }
64
65 6
        if ($list_type === 'ol' && $start = $element->getParent()->getAttribute('start')) {
66 3
            $number = $start + $element->getSiblingPosition() - 1;
67 2
        } else {
68 6
            $number = $element->getSiblingPosition();
69
        }
70
71 6
        return $prefix . $number . '. ' . $value . "\n";
72
    }
73
74
    /**
75
     * @return string[]
76
     */
77 84
    public function getSupportedTags()
78
    {
79 84
        return array('li');
80
    }
81
}
82