RawHtmlTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 32.14 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 0
dl 18
loc 56
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A identifyRawHtml() 0 4 2
A consumeRawHtml() 18 18 3
A renderRawHtml() 0 8 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
 * @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