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

OptimizeConditionalValueOf::normalize()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 3
nop 1
dl 0
loc 24
rs 8.6845
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A OptimizeConditionalValueOf::normalizeElement() 0 15 3
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
}