Passed
Branch major/TemplateNormalizationsSw... (4de178)
by Josh
03:40
created

AbstractNormalization::createTextNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2023 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 DOMComment;
12
use DOMNode;
13
use s9e\SweetDOM\Document;
14
use s9e\SweetDOM\Element;
15
16
abstract class AbstractNormalization
17
{
18
	/**
19
	* XSL namespace
20
	*/
21
	const XMLNS_XSL = 'http://www.w3.org/1999/XSL/Transform';
22
23
	/**
24
	* @var ?Document Document that holds the template being normalized
25
	*/
26
	protected ?Document $ownerDocument = null;
27
28
	/**
29
	* @var string[] XPath queries used to retrieve nodes of interest
30
	*/
31
	protected array $queries = [];
32
33
	/**
34
	* Apply this normalization rule to given template
35
	*
36
	* @param Element $template <xsl:template/> node
37
	*/
38
	public function normalize(Element $template): void
39
	{
40
		$this->ownerDocument = $template->ownerDocument;
41
		foreach ($this->getNodes() as $node)
42
		{
43
			$this->normalizeNode($node);
44
		}
45
		$this->reset();
46
	}
47
48
	/**
49
	* Create an xsl:text element or a text node in current template
50
	*
51
	* @param  string  $content
52
	* @return DOMNode
53
	*/
54
	protected function createText(string $content): DOMNode
55
	{
56
		return (trim($content) === '')
57
		     ? $this->ownerDocument->createXslText($content)
58
		     : $this->ownerDocument->createTextNode($content);
59
	}
60
61
	/**
62
	* Create a text node in current template
63
	*
64
	* @param  string  $content
65
	* @return DOMText
66
	*/
67
	protected function createTextNode($content): DOMText
0 ignored issues
show
Bug introduced by
The type s9e\TextFormatter\Config...eNormalizations\DOMText was not found. Did you mean DOMText? If so, make sure to prefix the type with \.
Loading history...
68
	{
69
		return $this->ownerDocument->createTextNode($content);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->ownerDocum...reateTextNode($content) returns the type DOMText which is incompatible with the type-hinted return s9e\TextFormatter\Config...eNormalizations\DOMText.
Loading history...
70
	}
71
72
	/**
73
	* Query and return a list of nodes of interest
74
	*
75
	* @return DOMNode[]
76
	*/
77
	protected function getNodes(): array
78
	{
79
		$query = implode(' | ', $this->queries);
80
81
		return ($query === '') ? [] : iterator_to_array($this->ownerDocument->query($query));
82
	}
83
84
	/**
85
	* Test whether given node is an XSL element
86
	*
87
	* @param  DOMNode $node
88
	* @param  string  $localName
89
	* @return bool
90
	*/
91
	protected function isXsl(DOMNode $node, $localName = null)
92
	{
93
		return ($node->namespaceURI === self::XMLNS_XSL && (!isset($localName) || $localName === $node->localName));
94
	}
95
96
	/**
97
	* Make an ASCII string lowercase
98
	*
99
	* @param  string $str Original string
100
	* @return string      Lowercased string
101
	*/
102
	protected function lowercase($str)
103
	{
104
		return strtr($str, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
105
	}
106
107
	/**
108
	* Normalize given attribute
109
	*
110
	* @param  DOMAttr $attribute
111
	* @return void
112
	*/
113
	protected function normalizeAttribute(DOMAttr $attribute)
0 ignored issues
show
Unused Code introduced by
The parameter $attribute is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

113
	protected function normalizeAttribute(/** @scrutinizer ignore-unused */ DOMAttr $attribute)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
114
	{
115
	}
116
117
	/**
118
	* Normalize given element
119
	*/
120
	protected function normalizeElement(Element $element): void
1 ignored issue
show
Unused Code introduced by
The parameter $element is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

120
	protected function normalizeElement(/** @scrutinizer ignore-unused */ Element $element): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
121
	{
122
	}
123
124
	/**
125
	* Normalize given node
126
	*
127
	* @param  DOMNode $node
128
	* @return void
129
	*/
130
	protected function normalizeNode(DOMNode $node)
131
	{
132
		if (!$node->parentNode)
133
		{
134
			// Ignore nodes that have been removed from the document
135
			return;
136
		}
137
		if ($node instanceof Element)
138
		{
139
			$this->normalizeElement($node);
140
		}
141
		elseif ($node instanceof DOMAttr)
142
		{
143
			$this->normalizeAttribute($node);
144
		}
145
	}
146
147
	/**
148
	* Reset this instance's properties after usage
149
	*
150
	* @return void
151
	*/
152
	protected function reset()
153
	{
154
		$this->ownerDocument = null;
155
	}
156
}