Test Failed
Push — master ( dbca4e...c73643 )
by Jeroen De
04:56
created

SyntaxRenderer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 63
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getRenderedContent() 0 9 2
A buildSyntaxTag() 0 7 1
A getTagAttributes() 0 20 4
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
	public function __construct( callable $recursiveTagParseFunction, string $language,
22
		bool $enableLineNumbers, int $startingLineNumber, string $highlightedLines, bool $inlineSource ) {
23
24
		$this->recursiveTagParseFunction = $recursiveTagParseFunction;
25
		$this->language = $language;
26
		$this->enableLineNumbers = $enableLineNumbers;
27
		$this->startingLineNumber = $startingLineNumber;
28
		$this->highlightedLines = $highlightedLines;
29
		$this->inlineSource = $inlineSource;
30
	}
31
32
	public function getRenderedContent( string $content ): string {
33
		$parsed = ($this->recursiveTagParseFunction)( $this->buildSyntaxTag( $content ) );
34
35
		if ( is_string( $parsed ) ) {
36
			return $parsed;
37
		}
38
39
		return '';
40
	}
41
42
	private function buildSyntaxTag( $content ): string {
43
		return \Html::rawElement(
44
			'syntaxhighlight',
45
			$this->getTagAttributes(),
46
			$content
47
		);
48
	}
49
50
	private function getTagAttributes(): array {
51
		$attributes = [
52
			'lang' => $this->language,
53
			'start' => $this->startingLineNumber,
54
		];
55
56
		if ( $this->enableLineNumbers ) {
57
			$attributes['line'] = true;
58
		}
59
60
		if ( $this->highlightedLines !== '' ) {
61
			$attributes['highlight'] = $this->highlightedLines;
62
		}
63
64
		if ( $this->inlineSource ) {
65
			$attributes['inline'] = true;
66
		}
67
68
		return $attributes;
69
	}
70
71
}
72