SyntaxRenderer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 96.3%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 63
ccs 26
cts 27
cp 0.963
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getRenderedContent() 0 9 2
A getTagAttributes() 0 20 4
A buildSyntaxTag() 0 7 1
1
<?php
2
3
namespace GitHub;
4
5
/**
6
 * @licence GNU GPL v2+
7
 * @author Jeroen De Dauw < [email protected] >
8
 */
9
class SyntaxRenderer {
10
11
	private $recursiveTagParseFunction;
12
13
	// Parameters for SyntaxHighlight extension (formerly SyntaxHighlight_GeSHi)
14
	// https://www.mediawiki.org/wiki/Extension:SyntaxHighlight
15
	private $language;
16
	private $enableLineNumbers;
17
	private $startingLineNumber;
18
	private $highlightedLines;
19
	private $inlineSource;
20
21 8
	public function __construct( callable $recursiveTagParseFunction, string $language,
22
		bool $enableLineNumbers, int $startingLineNumber, string $highlightedLines, bool $inlineSource ) {
23
24 8
		$this->recursiveTagParseFunction = $recursiveTagParseFunction;
25 8
		$this->language = $language;
26 8
		$this->enableLineNumbers = $enableLineNumbers;
27 8
		$this->startingLineNumber = $startingLineNumber;
28 8
		$this->highlightedLines = $highlightedLines;
29 8
		$this->inlineSource = $inlineSource;
30 8
	}
31
32 8
	public function getRenderedContent( string $content ): string {
33 8
		$parsed = ($this->recursiveTagParseFunction)( $this->buildSyntaxTag( $content ) );
34
35 8
		if ( is_string( $parsed ) ) {
36 8
			return $parsed;
37
		}
38
39
		return '';
40
	}
41
42 8
	private function buildSyntaxTag( $content ): string {
43 8
		return \Html::rawElement(
44 8
			'syntaxhighlight',
45 8
			$this->getTagAttributes(),
46
			$content
47
		);
48
	}
49
50 8
	private function getTagAttributes(): array {
51
		$attributes = [
52 8
			'lang' => $this->language,
53 8
			'start' => $this->startingLineNumber,
54
		];
55
56 8
		if ( $this->enableLineNumbers ) {
57 1
			$attributes['line'] = true;
58
		}
59
60 8
		if ( $this->highlightedLines !== '' ) {
61 1
			$attributes['highlight'] = $this->highlightedLines;
62
		}
63
64 8
		if ( $this->inlineSource ) {
65 1
			$attributes['inline'] = true;
66
		}
67
68 8
		return $attributes;
69
	}
70
71
}
72