Completed
Push — master ( 032337...4a3f84 )
by Colin
03:00
created

src/Converter/PreformattedConverter.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 = '';
0 ignored issues
show
$markdown is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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
        // Empty lines are a special case
37 3
        if ($pre_content === '') {
38 3
            return "```\n```\n";
39
        }
40
41
        // Normalizing new lines
42 3
        $pre_content = preg_replace('/\r\n|\r|\n/', PHP_EOL, $pre_content);
43
44
        // Is it a single line?
45 3
        if (strpos($pre_content, PHP_EOL) === false) {
46
            // One line of code, wrapping it on one backtick.
47 3
            return '`' . $pre_content . '`';
48
        }
49
50
        // Ensure there's a newline at the end
51 3
        if (strrpos($pre_content, PHP_EOL) !== strlen($pre_content) - 1) {
52 3
            $pre_content .= PHP_EOL;
53 2
        }
54
55
        // Use three backticks
56 3
        return "```\n" . $pre_content . "```\n";
57
    }
58
59
    /**
60
     * @return string[]
61
     */
62 84
    public function getSupportedTags()
63
    {
64 84
        return array('pre');
65
    }
66
}
67