Completed
Pull Request — master (#100)
by
unknown
21:51 queued 19:53
created

PreformattedConverter::convert()   C

Complexity

Conditions 8
Paths 10

Size

Total Lines 51
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 8.013

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 51
ccs 32
cts 34
cp 0.9412
rs 6.5978
c 2
b 1
f 0
cc 8
eloc 29
nc 10
nop 1
crap 8.013

How to fix   Long Method   

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\ElementInterface;
6
7
class PreformattedConverter implements ConverterInterface
8
{
9
    /**
10
     * @param ElementInterface $element
11
     *
12
     * @return string
13
     */
14 12
    public function convert(ElementInterface $element)
15
    {
16
        // Store the content of the code block in an array, one entry for each line
17
18 12
        $markdown = '';
19
20 12
        $code_content = html_entity_decode($element->getChildrenAsString());
21 12
        $code_content = str_replace(array('<code>', '</code>'), '', $code_content);
22 12
        $code_content = str_replace(array('<pre>', '</pre>'), '', $code_content);
23
24 12
        $lines = preg_split('/\r\n|\r|\n/', $code_content);
25 12
        $total = count($lines);
26
27
        // If there's more than one line of code, prepend each line with four spaces and no backticks.
28 12
        if ($total > 1 || $element->getTagName() === 'pre') {
29
            // Remove the first and last line if they're empty
30 6
            $first_line = trim($lines[0]);
31 6
            $last_line = trim($lines[$total - 1]);
32 6
            $first_line = trim($first_line, '&#xD;'); //trim XML style carriage returns too
33 6
            $last_line = trim($last_line, '&#xD;');
34
35 6
            if (empty($first_line)) {
36
                array_shift($lines);
37
            }
38
39 6
            if (empty($last_line)) {
40 3
                array_pop($lines);
41 3
            }
42
43 6
            $count = 1;
44 6
            foreach ($lines as $line) {
45 6
                $line = str_replace('&#xD;', '', $line);
46 6
                $markdown .= '    ' . $line;
47
                // Add newlines, except final line of the code
48 6
                if ($count !== $total) {
49 3
                    $markdown .= "\n";
50 3
                }
51 6
                $count++;
52 6
            }
53 6
            $markdown .= "\n";
54 6
        } else {
55
            // There's only one line of code. It's a code span, not a block. Just wrap it with backticks.
56 6
            $markdown .= '`' . $lines[0] . '`';
57
        }
58
59 12
        if ($element->getTagName() === 'pre') {
60 3
            $markdown = "\n" . $markdown . "\n";
61 3
        }
62
63 12
        return $markdown;
64
    }
65
66
    /**
67
     * @return string[]
68
     */
69 78
    public function getSupportedTags()
70
    {
71 78
        return array('pre', 'code');
72
    }
73
}
74