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

InlineElements   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 39
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B normalizeElement() 0 28 4
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
use DOMException;
12
13
/**
14
* Inline the elements declarations of a template
15
*
16
* Will replace
17
*     <xsl:element name="div"><xsl:apply-templates/></xsl:element>
18
* with
19
*     <div><xsl:apply-templates/></div>
20
*/
21
class InlineElements extends AbstractNormalization
22
{
23
	/**
24
	* {@inheritdoc}
25
	*/
26
	protected $queries = ['//xsl:element'];
27
28
	/**
29
	* {@inheritdoc}
30
	*/
31
	protected function normalizeElement(DOMElement $element)
32
	{
33
		$elName = $element->getAttribute('name');
34
		$dom    = $this->ownerDocument;
35
36
		try
37
		{
38
			// Create the new static element
39
			$newElement = ($element->hasAttribute('namespace'))
40
						? $dom->createElementNS($element->getAttribute('namespace'), $elName)
41
						: $dom->createElement($elName);
42
		}
43
		catch (DOMException $e)
44
		{
45
			// Ignore this element if an exception got thrown
46
			return;
47
		}
48
49
		// Replace the old <xsl:element/> with it. We do it now so that libxml doesn't have to
50
		// redeclare the XSL namespace
51
		$element->parentNode->replaceChild($newElement, $element);
52
53
		// One by one and in order, we move the nodes from the old element to the new one
54
		while ($element->firstChild)
55
		{
56
			$newElement->appendChild($element->removeChild($element->firstChild));
57
		}
58
	}
59
}