Passed
Branch 3.0/master (0530e2)
by Josh
13:42
created

AbstractNormalization::createElement()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 20
ccs 7
cts 7
cp 1
rs 9.9666
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A AbstractNormalization::getNodes() 0 5 2
A AbstractNormalization::isXsl() 0 3 3
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 DOMNode;
11
use s9e\SweetDOM\Attr;
12
use s9e\SweetDOM\Comment;
13
use s9e\SweetDOM\CdataSection;
14
use s9e\SweetDOM\Document;
15
use s9e\SweetDOM\Element;
16
use s9e\SweetDOM\Text;
17
18
abstract class AbstractNormalization
19
{
20
	/**
21
	* XSL namespace
22
	*/
23
	const XMLNS_XSL = 'http://www.w3.org/1999/XSL/Transform';
24
25
	/**
26
	* @var Document Document that holds the template being normalized
27
	*/
28
	protected Document $ownerDocument;
29
30
	/**
31
	* @var string[] XPath queries used to retrieve nodes of interest
32
	*/
33
	protected array $queries = [];
34
35
	/**
36
	* Apply this normalization rule to given template
37
	*
38
	* @param Element $template <xsl:template/> node
39
	*/
40
	public function normalize(Element $template): void
41
	{
42
		$this->ownerDocument = $template->ownerDocument;
43
		foreach ($this->getNodes() as $node)
44
		{
45
			// Ignore nodes that have been removed from the document
46
			if ($node->parentNode)
47
			{
48
				$this->normalizeNode($node);
49
			}
50
		}
51
		$this->reset();
52
	}
53
54
	/**
55
	* Create an xsl:text element or a text node in current template
56
	*/
57
	protected function createPolymorphicText(string $textContent): Element|Text
58
	{
59
		return (trim($textContent) === '')
60
		     ? $this->ownerDocument->nodeCreator->createXslText($textContent)
61
		     : $this->ownerDocument->createTextNode($textContent);
62
	}
63
64
	/**
65
	* Query and return a list of nodes of interest
66
	*
67
	* @return DOMNode[]
68
	*/
69
	protected function getNodes(): array
70
	{
71
		$query = implode(' | ', $this->queries);
72
73
		return ($query === '') ? [] : iterator_to_array($this->ownerDocument->query($query));
74
	}
75
76
	/**
77
	* Test whether given node is an XSL element
78
	*
79
	* @param  DOMNode $node
80
	* @param  string  $localName
81
	* @return bool
82
	*/
83
	protected function isXsl(DOMNode $node, $localName = null): bool
84
	{
85
		return ($node->namespaceURI === self::XMLNS_XSL && (!isset($localName) || $localName === $node->localName));
86
	}
87
88
	/**
89
	* Make an ASCII string lowercase
90
	*
91
	* @param  string $str Original string
92
	* @return string      Lowercased string
93
	*/
94
	protected function lowercase($str)
95
	{
96
		return strtr($str, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
97
	}
98
99
	protected function normalizeAttribute(Attr $attribute): void
1 ignored issue
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

99
	protected function normalizeAttribute(/** @scrutinizer ignore-unused */ Attr $attribute): 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...
100
	{
101
	}
102
103
	protected function normalizeCdataSection(CdataSection $comment): void
104
	{
105
	}
106
107
	protected function normalizeComment(Comment $comment): void
1 ignored issue
show
Unused Code introduced by
The parameter $comment 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

107
	protected function normalizeComment(/** @scrutinizer ignore-unused */ Comment $comment): 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...
108
	{
109
	}
110
111
	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

111
	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...
112
	{
113
	}
114
115
	protected function normalizeNode(DOMNode $node): void
116
	{
117
		if ($node instanceof Element)
118
		{
119
			$this->normalizeElement($node);
120
		}
121
		elseif ($node instanceof Attr)
122
		{
123
			$this->normalizeAttribute($node);
124
		}
125
		elseif ($node instanceof Text)
126
		{
127
			$this->normalizeText($node);
128
		}
129
		elseif ($node instanceof Comment)
130
		{
131
			$this->normalizeComment($node);
132
		}
133
		elseif ($node instanceof CdataSection)
134
		{
135
			$this->normalizeCdataSection($node);
136
		}
137
	}
138
139
	protected function normalizeText(Text $node): void
140
	{
141
	}
142
143
	/**
144
	* Reset this instance's properties after usage
145
	*/
146
	protected function reset(): void
147
	{
148
		unset($this->ownerDocument);
149
	}
150
}