Completed
Pull Request — master (#102)
by
unknown
22:51
created

PreformattedConverter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 13.21 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 5
c 3
b 0
f 0
lcom 0
cbo 1
dl 7
loc 53
rs 10
ccs 31
cts 31
cp 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
B convert() 7 37 4
A getSupportedTags() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 15
    public function convert(ElementInterface $element)
15
    {
16
        $markdown = '';
17
18 15
        $pre_content = html_entity_decode($element->getChildrenAsString());
19
        $pre_content = str_replace(array('<pre>', '</pre>'), '', $pre_content);
20 15
21 15
        /*
22 15
         * 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 15
         * which use backticks, and if those backticks are at the beginning and at the end of the string it means
25 15
         * there's no more information to convert.
26
         */
27
28 15
        $firstBacktick = strpos(trim($pre_content), '`');
29
        $lastBacktick = strrpos(trim($pre_content), '`');
30 9
        if ($firstBacktick === 0 && $lastBacktick === strlen(trim($pre_content)) - 1) {
31 9
            return $pre_content;
32 9
        }
33 9
34
        // If the execution reaches this point it means it's just a pre tag, with no code tag nested
35 9
36 3
        // Normalizing new lines
37 3
        $pre_content = preg_replace('/\r\n|\r|\n/', PHP_EOL, $pre_content);
38
39 9
        // Checking if the string has multiple lines
40 6
        $lines = preg_split('/\r\n|\r|\n/', $pre_content);
41 6 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 9
            $markdown .= '```' . "\n" . $pre_content . "\n" . '```';
44 9
        } else {
45 9
            // One line of code, wrapping it on one backtick.
46 9
            $markdown .= '`' . $pre_content . '`';
47
        }
48 9
49 6
        return $markdown;
50 6
    }
51 9
52 9
    /**
53 9
     * @return string[]
54 9
     */
55
    public function getSupportedTags()
56 9
    {
57
        return array('pre');
58
    }
59
}
60