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 = ''; |
|
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 | // Normalizing new lines |
||
37 | 3 | $pre_content = preg_replace('/\r\n|\r|\n/', PHP_EOL, $pre_content); |
|
38 | |||
39 | // Checking if the string has multiple lines |
||
40 | 3 | $lines = preg_split('/\r\n|\r|\n/', $pre_content); |
|
41 | 3 | View Code Duplication | if (count($lines) > 1) { |
0 ignored issues
–
show
|
|||
42 | // Multiple lines detected, adding three backticks and newlines |
||
43 | 3 | $markdown .= '```' . "\n" . $pre_content . "\n" . '```'; |
|
44 | 2 | } else { |
|
45 | // One line of code, wrapping it on one backtick. |
||
46 | 3 | $markdown .= '`' . $pre_content . '`'; |
|
47 | } |
||
48 | |||
49 | 3 | return $markdown; |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * @return string[] |
||
54 | */ |
||
55 | 84 | public function getSupportedTags() |
|
56 | { |
||
57 | 84 | return array('pre'); |
|
58 | } |
||
59 | } |
||
60 |
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.