PrototypeNodeTrait::match()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Trait PrototypeNodeTrait
4
 *
5
 * @filesource   PrototypeNodeTrait.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
 * @noinspection PhpParamsInspection
13
 * @noinspection PhpIncompatibleReturnTypeInspection
14
 */
15
16
namespace chillerlan\PrototypeDOM\Node;
17
18
use chillerlan\PrototypeDOM\NodeList;
19
20
use function trim;
21
22
use const XML_ELEMENT_NODE, XML_TEXT_NODE;
23
24
/**
25
 * @implements \chillerlan\PrototypeDOM\Node\PrototypeNode
26
 *
27
 * @property string                                $nodeName
28
 * @property string                                $nodeValue
29
 * @property int                                   $nodeType
30
 * @property \chillerlan\PrototypeDOM\Node\Element $parentNode
31
 * @property \DOMNodeList                          $childNodes
32
 * @property \chillerlan\PrototypeDOM\Node\Element $firstChild
33
 * @property \chillerlan\PrototypeDOM\Node\Element $lastChild
34
 * @property \chillerlan\PrototypeDOM\Node\Element $previousSibling
35
 * @property \chillerlan\PrototypeDOM\Node\Element $nextSibling
36
 * @property \DOMNamedNodeMap                      $attributes
37
 * @property \chillerlan\PrototypeDOM\Document     $ownerDocument
38
 * @property string                                $namespaceURI
39
 * @property string                                $prefix
40
 * @property string                                $localName
41
 * @property string                                $baseURI
42
 * @property string                                $textContent
43
 */
44
trait PrototypeNodeTrait{
45
46
	/**
47
	 * @inheritDoc
48
	 */
49
	public function recursivelyCollect(string $property, int $maxLength = null, int $nodeType = null):NodeList{
50
		return $this->ownerDocument->recursivelyCollect($this, $property, $maxLength ?? -1, $nodeType ?? XML_ELEMENT_NODE);
0 ignored issues
show
Bug introduced by
$this of type chillerlan\PrototypeDOM\Node\PrototypeNodeTrait is incompatible with the type DOMNode expected by parameter $element of chillerlan\PrototypeDOM\...t::recursivelyCollect(). ( Ignorable by Annotation )

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

50
		return $this->ownerDocument->recursivelyCollect(/** @scrutinizer ignore-type */ $this, $property, $maxLength ?? -1, $nodeType ?? XML_ELEMENT_NODE);
Loading history...
51
	}
52
53
	/**
54
	 * @inheritDoc
55
	 */
56
	public function empty():bool{
57
		return empty(trim($this->nodeValue));
58
	}
59
60
	/**
61
	 * @inheritDoc
62
	 */
63
	public function inspect(bool $xml = null):string{
64
		return $this->ownerDocument->inspect($this, $xml);
0 ignored issues
show
Bug introduced by
$this of type chillerlan\PrototypeDOM\Node\PrototypeNodeTrait is incompatible with the type DOMNode|null expected by parameter $context of chillerlan\PrototypeDOM\Document::inspect(). ( Ignorable by Annotation )

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

64
		return $this->ownerDocument->inspect(/** @scrutinizer ignore-type */ $this, $xml);
Loading history...
65
	}
66
67
	/**
68
	 * @inheritDoc
69
	 */
70
	public function removeNode():PrototypeNode{
71
72
		if(!$this->parentNode){
73
			return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type chillerlan\PrototypeDOM\Node\PrototypeNodeTrait which is incompatible with the type-hinted return chillerlan\PrototypeDOM\Node\PrototypeNode.
Loading history...
74
		}
75
76
		return $this->parentNode->removeChild($this);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->parentNode->removeChild($this) returns the type DOMNode which is incompatible with the type-hinted return chillerlan\PrototypeDOM\Node\PrototypeNode.
Loading history...
Bug introduced by
$this of type chillerlan\PrototypeDOM\Node\PrototypeNodeTrait is incompatible with the type DOMNode expected by parameter $child of DOMNode::removeChild(). ( Ignorable by Annotation )

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

76
		return $this->parentNode->removeChild(/** @scrutinizer ignore-type */ $this);
Loading history...
77
	}
78
79
	/**
80
	 * @inheritDoc
81
	 */
82
	public function replace(PrototypeNode $newNode):PrototypeNode{
83
84
		if(!$this->parentNode){
85
			return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type chillerlan\PrototypeDOM\Node\PrototypeNodeTrait which is incompatible with the type-hinted return chillerlan\PrototypeDOM\Node\PrototypeNode.
Loading history...
86
		}
87
88
		return $this->parentNode->replaceChild($this->importNode($newNode), $this);
0 ignored issues
show
Bug introduced by
$this of type chillerlan\PrototypeDOM\Node\PrototypeNodeTrait is incompatible with the type DOMNode expected by parameter $child of DOMNode::replaceChild(). ( Ignorable by Annotation )

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

88
		return $this->parentNode->replaceChild($this->importNode($newNode), /** @scrutinizer ignore-type */ $this);
Loading history...
Bug Best Practice introduced by
The expression return $this->parentNode...tNode($newNode), $this) returns the type DOMNode which is incompatible with the type-hinted return chillerlan\PrototypeDOM\Node\PrototypeNode.
Loading history...
89
	}
90
91
	/**
92
	 * @inheritDoc
93
	 */
94
	public function cleanWhitespace():PrototypeNode{
95
		$node = $this->firstChild;
96
97
		while($node){
98
			$nextNode = $node->nextSibling;
99
100
			if($node->nodeType === XML_TEXT_NODE && $node->empty()){
101
				$node->removeNode();
102
			}
103
104
			$node = $nextNode;
105
		}
106
107
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type chillerlan\PrototypeDOM\Node\PrototypeNodeTrait which is incompatible with the type-hinted return chillerlan\PrototypeDOM\Node\PrototypeNode.
Loading history...
108
	}
109
110
	/**
111
	 * @inheritDoc
112
	 */
113
	public function purge():PrototypeNode{
114
115
		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? ( Ignorable by Annotation )

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

115
		while($this->/** @scrutinizer ignore-call */ hasChildNodes()){
Loading history...
116
			$this->firstChild->removeNode();
117
		}
118
119
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type chillerlan\PrototypeDOM\Node\PrototypeNodeTrait which is incompatible with the type-hinted return chillerlan\PrototypeDOM\Node\PrototypeNode.
Loading history...
120
	}
121
122
	/**
123
	 * @inheritDoc
124
	 */
125
	public function importNode(PrototypeNode $newNode):PrototypeNode{
126
		return $this->ownerDocument->importNode($newNode, true);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->ownerDocum...ortNode($newNode, true) returns the type DOMNode which is incompatible with the type-hinted return chillerlan\PrototypeDOM\Node\PrototypeNode.
Loading history...
127
	}
128
129
	/**
130
	 * @inheritDoc
131
	 */
132
	public function match(string $selector):bool{
133
		return $this->ownerDocument->match($this, $selector);
0 ignored issues
show
Bug introduced by
$this of type chillerlan\PrototypeDOM\Node\PrototypeNodeTrait is incompatible with the type DOMNode expected by parameter $element of chillerlan\PrototypeDOM\Document::match(). ( Ignorable by Annotation )

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

133
		return $this->ownerDocument->match(/** @scrutinizer ignore-type */ $this, $selector);
Loading history...
134
	}
135
136
}
137