Passed
Push — master ( 8e4c14...93bf08 )
by smiley
06:58
created

PrototypeNodeTrait::match()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
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
 * @property string                                $nodeName
26
 * @property string                                $nodeValue
27
 * @property int                                   $nodeType
28
 * @property \chillerlan\PrototypeDOM\Node\Element $parentNode
29
 * @property \DOMNodeList                          $childNodes
30
 * @property \chillerlan\PrototypeDOM\Node\Element $firstChild
31
 * @property \chillerlan\PrototypeDOM\Node\Element $lastChild
32
 * @property \chillerlan\PrototypeDOM\Node\Element $previousSibling
33
 * @property \chillerlan\PrototypeDOM\Node\Element $nextSibling
34
 * @property \DOMNamedNodeMap                      $attributes
35
 * @property \chillerlan\PrototypeDOM\Document     $ownerDocument
36
 * @property string                                $namespaceURI
37
 * @property string                                $prefix
38
 * @property string                                $localName
39
 * @property string                                $baseURI
40
 * @property string                                $textContent
41
 */
42
trait PrototypeNodeTrait{
43
44
	/**
45
	 * @inheritDoc
46
	 */
47
	public function recursivelyCollect(string $property, int $maxLength = null, int $nodeType = null):NodeList{
48
		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

48
		return $this->ownerDocument->recursivelyCollect(/** @scrutinizer ignore-type */ $this, $property, $maxLength ?? -1, $nodeType ?? XML_ELEMENT_NODE);
Loading history...
49
	}
50
51
	/**
52
	 * @inheritDoc
53
	 */
54
	public function empty():bool{
55
		return empty(trim($this->nodeValue));
56
	}
57
58
	/**
59
	 * @inheritDoc
60
	 */
61
	public function inspect(bool $xml = null):string{
62
		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

62
		return $this->ownerDocument->inspect(/** @scrutinizer ignore-type */ $this, $xml);
Loading history...
63
	}
64
65
	/**
66
	 * @inheritDoc
67
	 */
68
	public function remove():PrototypeNode{
69
70
		if(!$this->parentNode){
71
			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...
72
		}
73
74
		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 $oldnode of DOMElement::removeChild(). ( Ignorable by Annotation )

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

74
		return $this->parentNode->removeChild(/** @scrutinizer ignore-type */ $this);
Loading history...
75
	}
76
77
	/**
78
	 * @inheritDoc
79
	 */
80
	public function replace(PrototypeNode $newnode):PrototypeNode{
81
82
		if(!$this->parentNode){
83
			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...
84
		}
85
86
		return $this->parentNode->replaceChild($this->importNode($newnode), $this);
0 ignored issues
show
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...
Bug introduced by
$this of type chillerlan\PrototypeDOM\Node\PrototypeNodeTrait is incompatible with the type DOMNode expected by parameter $oldnode of DOMElement::replaceChild(). ( Ignorable by Annotation )

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

86
		return $this->parentNode->replaceChild($this->importNode($newnode), /** @scrutinizer ignore-type */ $this);
Loading history...
87
	}
88
89
	/**
90
	 * @inheritDoc
91
	 */
92
	public function cleanWhitespace():PrototypeNode{
93
		$node = $this->firstChild;
94
95
		while($node){
96
			$nextNode = $node->nextSibling;
97
98
			if($node->nodeType === XML_TEXT_NODE && $node->empty()){
99
				$node->remove();
100
			}
101
102
			$node = $nextNode;
103
		}
104
105
		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...
106
	}
107
108
	/**
109
	 * @inheritDoc
110
	 */
111
	public function purge():PrototypeNode{
112
113
		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

113
		while($this->/** @scrutinizer ignore-call */ hasChildNodes()){
Loading history...
114
			$this->firstChild->remove();
115
		}
116
117
		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...
118
	}
119
120
	/**
121
	 * @inheritDoc
122
	 */
123
	public function importNode(PrototypeNode $newNode):PrototypeNode{
124
		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...
125
	}
126
127
	/**
128
	 * @inheritDoc
129
	 */
130
	public function match(string $selector):bool{
131
		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

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