Completed
Push — master ( 14b45d...d8ec9e )
by smiley
04:40
created

NodeTrait::remove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * Trait NodeTrait
4
 *
5
 * @filesource   NodeTrait.php
6
 * @created      11.05.2017
7
 * @package      chillerlan\PrototypeDOM\Traits
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\PrototypeDOM\Traits;
14
15
use chillerlan\PrototypeDOM\Node\PrototypeNode;
16
use chillerlan\PrototypeDOM\NodeList;
17
18
/**
19
 * @property string $nodeName
20
 * @property string $nodeValue
21
 * @property int $nodeType
22
 * @property \chillerlan\PrototypeDOM\Node\Element $parentNode
23
 * @property \DOMNodeList $childNodes
24
 * @property \chillerlan\PrototypeDOM\Node\Element $firstChild
25
 * @property \chillerlan\PrototypeDOM\Node\Element $lastChild
26
 * @property \chillerlan\PrototypeDOM\Node\Element $previousSibling
27
 * @property \chillerlan\PrototypeDOM\Node\Element $nextSibling
28
 * @property \DOMNamedNodeMap $attributes
29
 * @property \chillerlan\PrototypeDOM\Document $ownerDocument
30
 * @property string $namespaceURI
31
 * @property string $prefix
32
 * @property string $localName
33
 * @property string $baseURI
34
 * @property string $textContent
35
 */
36
trait NodeTrait{
37
38
	/**
39
	 * @link http://api.prototypejs.org/dom/Element/recursivelyCollect/
40
	 *
41
	 * @param string $property
42
	 * @param int    $maxLength
43
	 * @param int    $nodeType
44
	 *
45
	 * @return \chillerlan\PrototypeDOM\NodeList
46
	 */
47 2
	public function recursivelyCollect(string $property, int $maxLength = -1, int $nodeType = XML_ELEMENT_NODE):NodeList{
48 2
		return $this->ownerDocument->recursivelyCollect($this, $property, $maxLength, $nodeType);
49
	}
50
51
	/**
52
	 * @link http://api.prototypejs.org/dom/Element/empty/
53
	 *
54
	 * @return bool
55
	 */
56 2
	public function empty():bool{
57 2
		return empty(trim($this->nodeValue));
58
	}
59
60
	/**
61
	 * @link http://api.prototypejs.org/dom/Element/inspect/
62
	 *
63
	 *
64
	 * @todo: fixme!
65
	 *
66
	 * @param bool $xml
67
	 *
68
	 * @return string
69
	 */
70 3
	public function inspect($xml = false):string{
71 3
		return $this->ownerDocument->inspect($this, $xml);
72
	}
73
74
	/**
75
	 * @link http://api.prototypejs.org/dom/Element/remove/
76
	 *
77
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeNode
78
	 */
79 6
	public function remove():PrototypeNode{
80
81 6
		if(!$this->parentNode){
82 1
			return $this;
83
		}
84
85 6
		return $this->parentNode->removeChild($this);
86
	}
87
88
	/**
89
	 * @link http://api.prototypejs.org/dom/Element/replace/
90
	 *
91
	 * @param \chillerlan\PrototypeDOM\Node\PrototypeNode $newnode
92
	 *
93
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeNode
94
	 */
95 2
	public function replace(PrototypeNode $newnode):PrototypeNode{
96
97 2
		if(!$this->parentNode){
98 1
			return $this;
99
		}
100
101 2
		return $this->parentNode->replaceChild($this->_importNode($newnode), $this);
102
	}
103
104
	/**
105
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeNode
106
	 */
107 7
	public function purge():PrototypeNode{
108
109 7
		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...
110 3
			$this->firstChild->remove();
111
		}
112
113 7
		return $this;
114
	}
115
116
	/**
117
	 * @link http://api.prototypejs.org/dom/Element/cleanWhitespace/
118
	 *
119
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeNode
120
	 */
121 1
	public function cleanWhitespace():PrototypeNode{
122 1
		$node = $this->firstChild;
123
124 1
		while($node){
125 1
			$nextNode = $node->nextSibling;
126
127 1
			if($node->nodeType === XML_TEXT_NODE && $node->empty()){
128 1
				$node->remove();
129
			}
130
131 1
			$node = $nextNode;
132
		}
133
134 1
		return $this;
135
	}
136
137
	/**
138
	 * @param \chillerlan\PrototypeDOM\Node\PrototypeNode $newNode
139
	 *
140
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeNode
141
	 */
142 6
	public function _importNode(PrototypeNode $newNode):PrototypeNode{
143 6
		return $this->ownerDocument->importNode($newNode, true);
144
	}
145
146
}
147