Completed
Push — master ( 4105bb...af2944 )
by Josh
06:49
created

DisallowUnsupportedXSL::requireAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php declare(strict_types=1);
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2020 The s9e authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Configurator\TemplateChecks;
9
10
use DOMElement;
11
use RuntimeException;
12
13
class DisallowUnsupportedXSL extends AbstractXSLSupportCheck
14
{
15
	protected $supportedElements = ['apply-templates', 'attribute', 'choose', 'comment', 'copy-of', 'element', 'for-each', 'if', 'number', 'otherwise', 'processing-instruction', 'sort', 'text', 'value-of', 'variable', 'when'];
16
17
	protected $supportedFunctions = ['boolean', 'ceiling', 'concat', 'contains', 'count', 'current', 'document', 'element-available', 'false', 'floor', 'format-number', 'function-available', 'generate-id', 'id', 'key', 'lang', 'last', 'local-name', 'name', 'namespace-uri', 'normalize-space', 'not', 'number', 'position', 'round', 'starts-with', 'string', 'string-length', 'substring', 'substring-after', 'substring-before', 'sum', 'system-property', 'translate', 'true', 'unparsed-entity-uri'];
18
19 1
	protected function checkXslApplyTemplatesElement(DOMElement $applyTemplates): void
20
	{
21 1
		if ($applyTemplates->hasAttribute('mode'))
22
		{
23 1
			throw new RuntimeException('xsl:apply-templates elements do not support the mode attribute');
24
		}
25
	}
26
27 1
	protected function checkXslCopyOfElement(DOMElement $copyOf): void
28
	{
29 1
		$this->requireAttribute($copyOf, 'select');
30
	}
31
32 1
	protected function checkXslIfElement(DOMElement $if): void
33
	{
34 1
		$this->requireAttribute($if, 'test');
35
	}
36
37 2
	protected function checkXslValueOfElement(DOMElement $valueOf): void
38
	{
39 2
		$this->requireAttribute($valueOf, 'select');
40
	}
41
42 1
	protected function checkXslVariableElement(DOMElement $variable): void
43
	{
44 1
		$this->requireAttribute($variable, 'name');
45
	}
46
47 1
	protected function checkXslWhenElement(DOMElement $when): void
48
	{
49 1
		$this->requireAttribute($when, 'test');
50
	}
51
52 6
	protected function requireAttribute(DOMElement $element, string $attrName)
53
	{
54 6
		if (!$element->hasAttribute($attrName))
55
		{
56 5
			throw new RuntimeException('xsl:' . $element->localName . ' elements require a ' . $attrName . ' attribute');
57
		}
58
	}
59
}