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

SyntaxRenderer::getTagAttributes()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
rs 9.2
cc 4
eloc 11
nc 8
nop 0
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