Completed
Push — master ( f7b88b...3755ed )
by Josh
14:40
created

MinifyXPathExpressions::normalize()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 4
nop 1
dl 0
loc 34
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2017 The s9e Authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Configurator\TemplateNormalizations;
9
10
use DOMAttr;
11
use s9e\TextFormatter\Configurator\Helpers\AVTHelper;
12
use s9e\TextFormatter\Configurator\Helpers\XPathHelper;
13
14
class MinifyXPathExpressions extends AbstractNormalization
15
{
16
	/**
17
	* {@inheritdoc}
18
	*/
19
	protected $queries = ['//@*[contains(., " ")]'];
20
21
	/**
22
	* {@inheritdoc}
23
	*/
24
	protected function normalizeAttribute(DOMAttr $attribute)
25
	{
26
		$element = $attribute->parentNode;
27
		if (!$this->isXsl($element))
28
		{
29
			// Replace XPath expressions in non-XSL elements
30
			$this->replaceAVT($attribute);
31
		}
32
		elseif (in_array($attribute->nodeName, ['match', 'select', 'test'], true))
33
		{
34
			// Replace the content of match, select and test attributes of an XSL element
35
			$expr = XPathHelper::minify($attribute->nodeValue);
36
			$element->setAttribute($attribute->nodeName, $expr);
37
		}
38
	}
39
40
	/**
41
	* Minify XPath expressions in given attribute
42
	*
43
	* @param  DOMAttr $attribute
44
	* @return void
45
	*/
46
	protected function replaceAVT(DOMAttr $attribute)
47
	{
48
		AVTHelper::replace(
49
			$attribute,
50
			function ($token)
51
			{
52
				if ($token[0] === 'expression')
53
				{
54
					$token[1] = XPathHelper::minify($token[1]);
55
				}
56
57
				return $token;
58
			}
59
		);
60
	}
61
}