FencedCodeTrait   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 44
Duplicated Lines 52.27 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 1
dl 23
loc 44
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B identifyFencedCode() 0 9 9
A consumeFencedCode() 23 23 5

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) 2014 Carsten Brandt
4
 * @license https://github.com/cebe/markdown/blob/master/LICENSE
5
 * @link https://github.com/cebe/markdown#readme
6
 */
7
8
namespace cebe\markdown\block;
9
10
/**
11
 * Adds the fenced code blocks
12
 *
13
 * automatically included 4 space indented code blocks
14
 */
15
trait FencedCodeTrait
16
{
17
	use CodeTrait;
18
19
	/**
20
	 * identify a line as the beginning of a fenced code block.
21
	 */
22 135
	protected function identifyFencedCode($line)
23
	{
24 135
		return ($line[0] === '`' && strncmp($line, '```', 3) === 0) ||
25 135
			   ($line[0] === '~' && strncmp($line, '~~~', 3) === 0) ||
26 134
			   (isset($line[3]) && (
27 113
					($line[3] === '`' && strncmp(ltrim($line), '```', 3) === 0) ||
28 135
					($line[3] === '~' && strncmp(ltrim($line), '~~~', 3) === 0)
29
			   ));
30
	}
31
32
	/**
33
	 * Consume lines for a fenced code block
34
	 */
35 7 View Code Duplication
	protected function consumeFencedCode($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...
36
	{
37 7
		$line = ltrim($lines[$current]);
38 7
		$fence = substr($line, 0, $pos = strrpos($line, $line[0]) + 1);
39 7
		$language = rtrim(substr($line, $pos));
40
		// consume until end fence
41 7
		$content = [];
42 7
		for ($i = $current + 1, $count = count($lines); $i < $count; $i++) {
43 7
			if (($pos = strpos($line = $lines[$i], $fence)) === false || $pos > 3) {
44 7
				$content[] = $line;
45
			} else {
46 7
				break;
47
			}
48
		}
49
		$block = [
50 7
			'code',
51 7
			'content' => implode("\n", $content),
52
		];
53 7
		if (!empty($language)) {
54 2
			$block['language'] = $language;
55
		}
56 7
		return [$block, $i];
57
	}
58
}
59