Completed
Push — master ( 9ffe58...cd3227 )
by Carsten
03:21 queued 01:21
created

CodeTrait::consumeCode()   C

Complexity

Conditions 13
Paths 7

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 13.169

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 18
cts 20
cp 0.9
rs 5.1234
c 0
b 0
f 0
cc 13
eloc 18
nc 7
nop 2
crap 13.169

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 4 space indented code blocks
12
 */
13
trait CodeTrait
14
{
15
	/**
16
	 * identify a line as the beginning of a code block.
17
	 */
18 203
	protected function identifyCode($line)
19
	{
20
		// indentation >= 4 or one tab is code
21 203
		return ($l = $line[0]) === ' ' && $line[1] === ' ' && $line[2] === ' ' && $line[3] === ' ' || $l === "\t";
22
	}
23
24
	/**
25
	 * Consume lines for a code block element
26
	 */
27 48
	protected function consumeCode($lines, $current)
28
	{
29
		// consume until newline
30
31 48
		$content = [];
32 48
		for ($i = $current, $count = count($lines); $i < $count; $i++) {
33 48
			$line = $lines[$i];
34
35
			// a line is considered to belong to this code block as long as it is intended by 4 spaces or a tab
36 48
			if (isset($line[0]) && ($line[0] === "\t" || strncmp($line, '    ', 4) === 0)) {
37 48
				$line = $line[0] === "\t" ? substr($line, 1) : substr($line, 4);
38 48
				$content[] = $line;
39
			// but also if it is empty and the next line is intended by 4 spaces or a tab
40 48
			} elseif (($line === '' || rtrim($line) === '') && isset($lines[$i + 1][0]) &&
41 42
				      ($lines[$i + 1][0] === "\t" || strncmp($lines[$i + 1], '    ', 4) === 0)) {
42 12
				if ($line !== '') {
43
					$line = $line[0] === "\t" ? substr($line, 1) : substr($line, 4);
44
				}
45 12
				$content[] = $line;
46 12
			} else {
47 42
				break;
48
			}
49 48
		}
50
51
		$block = [
52 48
			'code',
53 48
			'content' => implode("\n", $content),
54 48
		];
55 48
		return [$block, --$i];
56
	}
57
58
	/**
59
	 * Renders a code block
60
	 */
61 38
	protected function renderCode($block)
62
	{
63 38
		$class = isset($block['language']) ? ' class="language-' . $block['language'] . '"' : '';
64 38
		return "<pre><code$class>" . htmlspecialchars($block['content'] . "\n", ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8') . "</code></pre>\n";
65
	}
66
}
67