Completed
Push — master ( e70e95...72eb9f )
by Josh
04:30
created

DisallowUnsafeCopyOf   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 25
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A check() 0 13 3
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2019 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 s9e\TextFormatter\Configurator\Exceptions\UnsafeTemplateException;
12
use s9e\TextFormatter\Configurator\Items\Tag;
13
use s9e\TextFormatter\Configurator\TemplateCheck;
14
15
class DisallowUnsafeCopyOf extends TemplateCheck
16
{
17
	/**
18
	* Check for unsafe <xsl:copy-of/> elements
19
	*
20
	* Any select expression that is not a set of named attributes is considered unsafe
21
	*
22
	* @param  DOMElement $template <xsl:template/> node
23
	* @param  Tag        $tag      Tag this template belongs to
24
	* @return void
25
	*/
26 1
	public function check(DOMElement $template, Tag $tag)
27
	{
28 1
		$nodes = $template->getElementsByTagNameNS(self::XMLNS_XSL, 'copy-of');
29 1
		foreach ($nodes as $node)
30
		{
31 1
			$expr = $node->getAttribute('select');
32
33 1
			if (!preg_match('#^@[-\\w]*(?:\\s*\\|\\s*@[-\\w]*)*$#D', $expr))
34
			{
35 1
				throw new UnsafeTemplateException("Cannot assess the safety of '" . $node->nodeName . "' select expression '" . $expr . "'", $node);
36
			}
37
		}
38
	}
39
}