Test Failed
Push — master ( 9cf31c...ad793d )
by smiley
03:41
created

NodeTrait   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 0

Importance

Changes 0
Metric Value
wmc 14
lcom 3
cbo 0
dl 0
loc 108
c 0
b 0
f 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A recursivelyCollect() 0 3 1
A empty() 0 3 1
A inspect() 0 3 1
A remove() 0 8 2
A replace() 0 8 2
A purge() 0 8 2
A cleanWhitespace() 0 15 4
A importNode() 0 3 1
1
<?php
2
/**
3
 * Trait NodeTrait
4
 *
5
 * @filesource   NodeTrait.php
6
 * @created      11.05.2017
7
 * @package      chillerlan\PrototypeDOM\Node
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\PrototypeDOM\Node;
14
15
use chillerlan\PrototypeDOM\NodeList;
16
17
/**
18
 * @property string                                $nodeName
19
 * @property string                                $nodeValue
20
 * @property int                                   $nodeType
21
 * @property \chillerlan\PrototypeDOM\Node\Element $parentNode
22
 * @property \DOMNodeList                          $childNodes
23
 * @property \chillerlan\PrototypeDOM\Node\Element $firstChild
24
 * @property \chillerlan\PrototypeDOM\Node\Element $lastChild
25
 * @property \chillerlan\PrototypeDOM\Node\Element $previousSibling
26
 * @property \chillerlan\PrototypeDOM\Node\Element $nextSibling
27
 * @property \DOMNamedNodeMap                      $attributes
28
 * @property \chillerlan\PrototypeDOM\Document     $ownerDocument
29
 * @property string                                $namespaceURI
30
 * @property string                                $prefix
31
 * @property string                                $localName
32
 * @property string                                $baseURI
33
 * @property string                                $textContent
34
 */
35
trait NodeTrait{
36
37
	/**
38
	 * @link http://api.prototypejs.org/dom/Element/recursivelyCollect/
39
	 *
40
	 * @param string $property
41
	 * @param int    $maxLength
42
	 * @param int    $nodeType https://secure.php.net/manual/dom.constants.php
43
	 *
44
	 * @return \chillerlan\PrototypeDOM\NodeList
45
	 */
46
	public function recursivelyCollect(string $property, int $maxLength = -1, int $nodeType = \XML_ELEMENT_NODE):NodeList{
47
		return $this->ownerDocument->recursivelyCollect($this, $property, $maxLength, $nodeType);
48
	}
49
50
	/**
51
	 * @link http://api.prototypejs.org/dom/Element/empty/
52
	 *
53
	 * @return bool
54
	 */
55
	public function empty():bool{
56
		return empty(\trim($this->nodeValue));
57
	}
58
59
	/**
60
	 * @link http://api.prototypejs.org/dom/Element/inspect/
61
	 *
62
	 * @param bool $xml
63
	 *
64
	 * @return string
65
	 */
66
	public function inspect(bool $xml = false):string{
67
		return $this->ownerDocument->inspect($this, $xml);
68
	}
69
70
	/**
71
	 * @link http://api.prototypejs.org/dom/Element/remove/
72
	 *
73
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeNode
74
	 */
75
	public function remove():PrototypeNode{
76
77
		if(!$this->parentNode){
78
			return $this;
79
		}
80
81
		return $this->parentNode->removeChild($this);
82
	}
83
84
	/**
85
	 * @link http://api.prototypejs.org/dom/Element/replace/
86
	 *
87
	 * @param \chillerlan\PrototypeDOM\Node\PrototypeNode $newnode
88
	 *
89
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeNode
90
	 */
91
	public function replace(PrototypeNode $newnode):PrototypeNode{
92
93
		if(!$this->parentNode){
94
			return $this;
95
		}
96
97
		return $this->parentNode->replaceChild($this->importNode($newnode), $this);
98
	}
99
100
	/**
101
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeNode
102
	 */
103
	public function purge():PrototypeNode{
104
105
		while($this->hasChildNodes()){
0 ignored issues
show
Bug introduced by
It seems like hasChildNodes() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
106
			$this->firstChild->remove();
107
		}
108
109
		return $this;
110
	}
111
112
	/**
113
	 * @link http://api.prototypejs.org/dom/Element/cleanWhitespace/
114
	 *
115
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeNode
116
	 */
117
	public function cleanWhitespace():PrototypeNode{
118
		$node = $this->firstChild;
119
120
		while($node){
121
			$nextNode = $node->nextSibling;
122
123
			if($node->nodeType === \XML_TEXT_NODE && $node->empty()){
124
				$node->remove();
125
			}
126
127
			$node = $nextNode;
128
		}
129
130
		return $this;
131
	}
132
133
	/**
134
	 * @param \chillerlan\PrototypeDOM\Node\PrototypeNode $newNode
135
	 *
136
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeNode
137
	 */
138
	public function importNode(PrototypeNode $newNode):PrototypeNode{
139
		return $this->ownerDocument->importNode($newNode, true);
140
	}
141
142
}
143