CodeTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 45 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 18
loc 40
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A identifyCode() 0 4 1
A consumeCode() 18 18 3
A renderCode() 0 4 1

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 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