RawHtmlTrait::identifyRawHtml()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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 raw html blocks
12
 */
13
trait RawHtmlTrait
14
{
15
    /**
16
     * @var bool whether to support raw html blocks.
17
     * Defaults to `false`.
18
     */
19
    public $useRawHtml = false;
20
21
    /**
22
     * @var callable output filter
23
     * Defaults to null.
24
     */
25
    public $rawHtmlFilter = null;
26
27
    /**
28
     * identify a line as the beginning of a raw html block.
29
     */
30 23
    protected function identifyRawHtml($line)
31
    {
32 23
        return $this->useRawHtml && (strcmp(rtrim($line), '<<<') === 0);
33
    }
34
35
    /**
36
     * Consume lines for a raw html block
37
     */
38 1 View Code Duplication
    protected function consumeRawHtml($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...
39
    {
40
        // consume until >>>
41 1
        $content = [];
42 1
        for ($i = $current + 1, $count = count($lines); $i < $count; $i++) {
43 1
            $line = rtrim($lines[$i]);
44 1
            if (strcmp($line, '>>>') !== 0) {
45 1
                $content[] = $line;
46
            } else {
47 1
                break;
48
            }
49
        }
50
        $block = [
51 1
            'rawHtml',
52 1
            'content' => implode("\n", $content),
53
        ];
54 1
        return [$block, $i];
55
    }
56
57
    /**
58
     * Renders a raw html block
59
     */
60 1
    protected function renderRawHtml($block)
61
    {
62 1
        $output = $block['content'];
63 1
        if (is_callable($this->rawHtmlFilter, true)) {
64 1
            $output = call_user_func($this->rawHtmlFilter, $output);
65
        }
66 1
        return $output . "\n";
67
    }
68
}
69