GithubPre   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 26
ccs 9
cts 9
cp 1
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 9 3
A rule() 0 3 1
1
<?php # -*- coding: utf-8 -*-
2
declare(strict_types=1);
3
4
namespace Bueltge\Marksimple\Rule;
5
6
class GithubPre extends AbstractRegexRule
7
{
8
9
    /**
10
     * Get the regex rule to identify the fromFile for the callback.
11
     * Code blocks via 3 ` include language string (optional).
12
     *
13
     * @return string
14
     */
15 8
    public function rule(): string
16 8
    {
17 8
        return '#(^|\n)([`~]{3,})(?: *\.?([a-zA-Z0-9\-.]+))?\n+([\s\S]+?)\n+\2(\n|$)#';
18
    }
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 5
    public function render(array $content): string
24 5
    {
25
        // Define css class for each language.
26 5
        $language = !empty($content[3]) ? filter_var($content[3], FILTER_SANITIZE_STRING) : '';
27 5
        $class = !empty($language) ? sprintf(' class="%s language-%s"', $language, $language) : '';
28
        // Build one block so that we not create each paragraph.
29 5
        $content = str_replace("\n", '<br>', $content[4]);
30
31 5
        return sprintf('<pre><code%s>%s</code></pre>', $class, $content);
32
    }
33
}
34