Completed
Push — master ( 6b5dee...7f9818 )
by Colin
02:34
created

PreformattedConverter::convert()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 37
Code Lines 15

Duplication

Lines 7
Ratio 18.92 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 7
loc 37
ccs 15
cts 15
cp 1
rs 8.5806
cc 4
eloc 15
nc 3
nop 1
crap 4
1
<?php
2
3
namespace League\HTMLToMarkdown\Converter;
4
5
use League\HTMLToMarkdown\ElementInterface;
6
7
class PreformattedConverter implements ConverterInterface
8
{
9
    /**
10
     * @param ElementInterface $element
11
     *
12
     * @return string
13
     */
14 9
    public function convert(ElementInterface $element)
15
    {
16 9
        $markdown = '';
17
18 9
        $pre_content = html_entity_decode($element->getChildrenAsString());
19 9
        $pre_content = str_replace(array('<pre>', '</pre>'), '', $pre_content);
20
21
        /*
22
         * Checking for the code tag.
23
         * Usually pre tags are used along with code tags. This conditional will check for already converted code tags,
24
         * which use backticks, and if those backticks are at the beginning and at the end of the string it means
25
         * there's no more information to convert.
26
         */
27
28 9
        $firstBacktick = strpos(trim($pre_content), '`');
29 9
        $lastBacktick = strrpos(trim($pre_content), '`');
30 9
        if ($firstBacktick === 0 && $lastBacktick === strlen(trim($pre_content)) - 1) {
31 6
            return $pre_content;
32
        }
33
34
        // If the execution reaches this point it means it's just a pre tag, with no code tag nested
35
36
        // Normalizing new lines
37 3
        $pre_content = preg_replace('/\r\n|\r|\n/', PHP_EOL, $pre_content);
38
39
        // Checking if the string has multiple lines
40 3
        $lines = preg_split('/\r\n|\r|\n/', $pre_content);
41 3 View Code Duplication
        if (count($lines) > 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
42
            // Multiple lines detected, adding three backticks and newlines
43 3
            $markdown .= '```' . "\n" . $pre_content . "\n" . '```';
44 3
        } else {
45
            // One line of code, wrapping it on one backtick.
46 3
            $markdown .= '`' . $pre_content . '`';
47
        }
48
49 3
        return $markdown;
50
    }
51
52
    /**
53
     * @return string[]
54
     */
55 78
    public function getSupportedTags()
56
    {
57 78
        return array('pre');
58
    }
59
}
60