CodeTrait::renderCode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2015 Nobuo Kihara
4
 * @license https://github.com/softark/creole/blob/master/LICENSE
5
 * @link https://github.com/softark/creole#readme
6
 */
7
8
namespace softark\creole\block;
9
10
/**
11
 * Adds the code blocks
12
 */
13
trait CodeTrait
14
{
15
    /**
16
     * identify a line as the beginning of a code block.
17
     */
18 23
    protected function identifyCode($line)
19
    {
20 23
        return strcmp(rtrim($line), '{{{') === 0;
21
    }
22
23
    /**
24
     * Consume lines for a code block
25
     */
26 2 View Code Duplication
    protected function consumeCode($lines, $current)
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...
27
    {
28
        // consume until }}}
29 2
        $content = [];
30 2
        for ($i = $current + 1, $count = count($lines); $i < $count; $i++) {
31 2
            $line = rtrim($lines[$i]);
32 2
            if (strcmp($line, '}}}') !== 0) {
33 2
                $content[] = $line;
34
            } else {
35 2
                break;
36
            }
37
        }
38
        $block = [
39 2
            'code',
40 2
            'content' => implode("\n", $content),
41
        ];
42 2
        return [$block, $i];
43
    }
44
45
    /**
46
     * Renders a code block
47
     */
48 2
    protected function renderCode($block)
49
    {
50 2
        return "<pre><code>" . htmlspecialchars($block['content'] . "\n", ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8') . "</code></pre>\n";
51
    }
52
}
53