Completed
Push — master ( af2944...5c358a )
by Josh
04:17
created

DisallowUnsupportedXSL::checkXslElementElement()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 1
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 2
	protected function checkXslAttributeElement(DOMElement $attribute): void
33
	{
34 2
		$this->requireAttribute($attribute, 'name');
35
36 1
		$attrName = $attribute->getAttribute('name');
37 1
		if (!preg_match('(^(?:\\{[^\\}]++\\}|[-.\\pL])++$)Du', $attrName))
38
		{
39 1
			throw new RuntimeException("Unsupported xsl:attribute name '" . $attrName . "'");
40
		}
41
	}
42
43 3
	protected function checkXslElementElement(DOMElement $element): void
44
	{
45 3
		$this->requireAttribute($element, 'name');
46
47 2
		$elName = $element->getAttribute('name');
48 2
		if (!preg_match('(^(?:\\{[^\\}]++\\}|[-.\\pL])++(?::(?:\\{[^\\}]++\\}|[-.\\pL])++)?$)Du', $elName))
49
		{
50 1
			throw new RuntimeException("Unsupported xsl:element name '" . $elName . "'");
51
		}
52
	}
53
54 1
	protected function checkXslIfElement(DOMElement $if): void
55
	{
56 1
		$this->requireAttribute($if, 'test');
57
	}
58
59 2
	protected function checkXslValueOfElement(DOMElement $valueOf): void
60
	{
61 2
		$this->requireAttribute($valueOf, 'select');
62
	}
63
64 1
	protected function checkXslVariableElement(DOMElement $variable): void
65
	{
66 1
		$this->requireAttribute($variable, 'name');
67
	}
68
69 1
	protected function checkXslWhenElement(DOMElement $when): void
70
	{
71 1
		$this->requireAttribute($when, 'test');
72
	}
73
74 11
	protected function requireAttribute(DOMElement $element, string $attrName)
75
	{
76 11
		if (!$element->hasAttribute($attrName))
77
		{
78 7
			throw new RuntimeException('xsl:' . $element->localName . ' elements require a ' . $attrName . ' attribute');
79
		}
80
	}
81
}