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

CodeConverter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 12.5 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 0
cbo 1
dl 7
loc 56
ccs 23
cts 23
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B convert() 7 40 5
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 CodeConverter implements ConverterInterface
8
{
9
    /**
10
     * @param ElementInterface $element
11
     *
12
     * @return string
13
     */
14 12
    public function convert(ElementInterface $element)
15
    {
16 12
        $language = null;
17
18
        // Checking for language class on the code block
19 12
        $classes = $element->getAttribute('class');
20
21 12
        if ($classes) {
22
            // Since tags can have more than one class, we need to find the one that starts with 'language-'
23 3
            $classes = explode(' ', $classes);
24 3
            foreach ($classes as $class) {
25 3
                if (strpos($class, 'language-') !== false) {
26
                    // Found one, save it as the selected language and stop looping over the classes.
27
                    // The space after the language avoids gluing the actual code with the language tag
28 3
                    $language = str_replace('language-', '', $class) . ' ';
29 3
                    break;
30
                }
31 3
            }
32 3
        }
33
34 12
        $markdown = '';
35 12
        $code = html_entity_decode($element->getChildrenAsString());
36
37
        // In order to remove the code tags we need to search for them and, in the case of the opening tag
38
        // use a regular expression to find the tag and the other attributes it might have
39 12
        $code = preg_replace('/<code\b[^>]*>/', '', $code);
40 12
        $code = str_replace('</code>', '', $code);
41
42
        // Checking if the code has multiple lines
43 12
        $lines = preg_split('/\r\n|\r|\n/', $code);
44 12 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...
45
            // Multiple lines detected, adding three backticks and newlines
46 3
            $markdown .= '```' . $language . "\n" . $code . "\n" . '```';
47 3
        } else {
48
            // One line of code, wrapping it on one backtick.
49 12
            $markdown .= '`' . $language . $code . '`';
50
        }
51
52 12
        return $markdown;
53
    }
54
55
    /**
56
     * @return string[]
57
     */
58 78
    public function getSupportedTags()
59
    {
60 78
        return array('code');
61
    }
62
}
63