Completed
Branch TemplateNormalizations (ae42e4)
by Josh
33:16
created

OptimizeConditionalValueOf::normalizeElement()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 2
nop 1
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 DOMElement;
11
12
/**
13
* Remove unnecessary <xsl:if> tests around <xsl:value-of>
14
*
15
* NOTE: should be performed before attributes are inlined for maximum effect
16
*/
17
class OptimizeConditionalValueOf extends AbstractNormalization
18
{
19
	/**
20
	* {@inheritdoc}
21
	*/
22
	protected $queries = ['//xsl:if[count(descendant::node()) = 1]/xsl:value-of'];
23
24
	/**
25
	* {@inheritdoc}
26
	*/
27
	protected function normalizeElement(DOMElement $element)
28
	{
29
		$if     = $element->parentNode;
30
		$test   = $if->getAttribute('test');
31
		$select = $element->getAttribute('select');
32
33
		// Ensure that the expressions match, and that they select one single attribute
34
		if ($select !== $test || !preg_match('#^@[-\\w]+$#D', $select))
35
		{
36
			return;
37
		}
38
39
		// Replace the xsl:if element with the xsl:value-of element
40
		$if->parentNode->replaceChild($if->removeChild($element), $if);
41
	}
42
}