Completed
Push — master ( 45991d...3b6473 )
by Colin
9s
created

ParagraphConverter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 23.38 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 18
loc 77
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A convert() 0 18 3
A getSupportedTags() 0 4 1
A escapeSpecialCharacters() 0 7 1
A escapeBlockquotelikeCharacters() 9 9 2
A escapeHeaderlikeCharacters() 9 9 2

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 ParagraphConverter implements ConverterInterface
8
{
9
    /**
10
     * @param ElementInterface $element
11
     *
12
     * @return string
13
     */
14 27
    public function convert(ElementInterface $element)
15
    {
16 27
        $value = $element->getValue();
17
18 27
        $markdown = '';
19
20 27
        $lines = preg_split('/\r\n|\r|\n/', $value);
21 27
        foreach ($lines as $line) {
22
            /*
23
             * Some special characters need to be escaped based on the position that they appear
24
             * The following function will deal with those special cases.
25
             */
26 27
            $markdown .= $this->escapeSpecialCharacters($line);
27 27
            $markdown .= "\n";
28 27
        }
29
30 27
        return trim($markdown) !== '' ? rtrim($markdown) . "\n\n" : '';
31
    }
32
33
    /**
34
     * @return string[]
35
     */
36 78
    public function getSupportedTags()
37
    {
38 78
        return array('p');
39
    }
40
41
    /**
42
     * @param string $line
43
     *
44
     * @return string
45
     */
46 27
    private function escapeSpecialCharacters($line)
47
    {
48 27
        $line = $this->escapeHeaderlikeCharacters($line);
49 27
        $line = $this->escapeBlockquotelikeCharacters($line);
50
51 27
        return $line;
52
    }
53
54
    /**
55
     * @param string $line
56
     *
57
     * @return string
58
     */
59 27 View Code Duplication
    private function escapeBlockquotelikeCharacters($line)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
60
    {
61 27
        if (strpos(ltrim($line), '>') === 0) {
62
            // Found a > char, escaping it
63 3
            return '\\' . ltrim($line);
64
        } else {
65 27
            return $line;
66
        }
67
    }
68
69
    /**
70
     * @param string $line
71
     *
72
     * @return string
73
     */
74 27 View Code Duplication
    private function escapeHeaderlikeCharacters($line)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
75
    {
76 27
        if (strpos(ltrim($line), '--') === 0) {
77
            // Found a -- structure, escaping it
78 3
            return '\\' . ltrim($line);
79
        } else {
80 27
            return $line;
81
        }
82
    }
83
}
84