Completed
Push — master ( 09acac...ffd66f )
by Josh
14:10
created

DisallowNodeByXPath::check()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.032
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 DOMXPath;
12
use s9e\TextFormatter\Configurator\Exceptions\UnsafeTemplateException;
13
use s9e\TextFormatter\Configurator\Items\Tag;
14
use s9e\TextFormatter\Configurator\TemplateCheck;
15
16
class DisallowNodeByXPath extends TemplateCheck
17
{
18
	/**
19
	* @var string XPath query used for locating nodes
20
	*/
21
	public $query;
22
23
	/**
24
	* Constructor
25
	*
26
	* @param  string $query XPath query used for locating nodes
27
	*/
28 1
	public function __construct($query)
29
	{
30 1
		$this->query = $query;
31 1
	}
32
33
	/**
34
	* Test for the presence of an element of given name
35
	*
36
	* @param  DOMElement $template <xsl:template/> node
37
	* @param  Tag        $tag      Tag this template belongs to
38
	* @return void
39
	*/
40 1
	public function check(DOMElement $template, Tag $tag)
41
	{
42 1
		$xpath = new DOMXPath($template->ownerDocument);
43
44 1
		foreach ($xpath->query($this->query) as $node)
45
		{
46 1
			throw new UnsafeTemplateException("Node '" . $node->nodeName . "' is disallowed because it matches '" . $this->query . "'", $node);
47
		}
48
	}
49
}