1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* @filesource NodeList.php |
5
|
|
|
* @created 09.05.2017 |
6
|
|
|
* @package chillerlan\PrototypeDOM |
7
|
|
|
* @author Smiley <[email protected]> |
8
|
|
|
* @copyright 2017 Smiley |
9
|
|
|
* @license MIT |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace chillerlan\PrototypeDOM; |
13
|
|
|
|
14
|
|
|
use DOMNode, DOMNodeList, Iterator, ArrayAccess, Countable; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class NodeList |
18
|
|
|
*/ |
19
|
|
|
class NodeList implements Iterator, ArrayAccess, Countable{ |
20
|
|
|
|
21
|
|
|
protected $nodelist = []; |
22
|
|
|
protected $key = 0; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* NodeList constructor. |
26
|
|
|
* |
27
|
|
|
* @param \DOMNodeList|\chillerlan\PrototypeDOM\NodeList|\chillerlan\PrototypeDOM\Element[] $content |
28
|
|
|
* |
29
|
|
|
* @throws \Exception |
30
|
|
|
*/ |
31
|
40 |
|
public function __construct($content = null){ |
32
|
|
|
|
33
|
40 |
|
if($content instanceof NodeList || $content instanceof DOMNodeList){ |
34
|
39 |
|
$this->nodelist = iterator_to_array($content); |
35
|
|
|
} |
36
|
39 |
|
elseif(is_array($content)){ |
37
|
3 |
|
$this->nodelist = $content; |
38
|
|
|
} |
39
|
|
|
/* else{ |
|
|
|
|
40
|
|
|
throw new \Exception('invalid content'); // @codeCoverageIgnore |
41
|
|
|
}*/ |
42
|
|
|
|
43
|
40 |
|
} |
44
|
|
|
|
45
|
1 |
|
public function match(DOMNode $node):bool{ |
46
|
|
|
|
47
|
1 |
|
foreach($this->nodelist as $element){ |
48
|
|
|
|
49
|
1 |
|
if($element->isSameNode($node)){ |
50
|
1 |
|
return true; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
3 |
|
public function item(int $index) { |
59
|
3 |
|
return $this->nodelist[$index] ?? null; |
60
|
|
|
} |
61
|
|
|
|
62
|
3 |
|
public function reverse():NodeList{ |
63
|
3 |
|
$this->nodelist = array_reverse($this->nodelist); |
64
|
3 |
|
$this->rewind(); |
65
|
|
|
|
66
|
3 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
public function _toArray():array { |
70
|
2 |
|
return $this->nodelist; |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
public function merge(NodeList $nodelist):NodeList{ |
74
|
1 |
|
$this->nodelist = array_merge($this->nodelist, $nodelist->_toArray()); |
75
|
|
|
|
76
|
1 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
39 |
|
public function current():DOMNode{ |
81
|
39 |
|
return $this->nodelist[$this->key]; |
82
|
|
|
} |
83
|
|
|
|
84
|
4 |
|
public function key():int{ |
85
|
4 |
|
return $this->key; |
86
|
|
|
} |
87
|
|
|
|
88
|
39 |
|
public function valid():bool{ |
89
|
39 |
|
return isset($this->nodelist[$this->key]); |
90
|
|
|
} |
91
|
|
|
|
92
|
39 |
|
public function next(){ |
93
|
39 |
|
$this->key++; |
94
|
39 |
|
} |
95
|
|
|
|
96
|
39 |
|
public function rewind(){ |
97
|
39 |
|
$this->key = 0; |
98
|
39 |
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
38 |
|
public function offsetExists($offset):bool{ |
102
|
38 |
|
return isset($this->nodelist[$offset]); |
103
|
|
|
} |
104
|
|
|
|
105
|
38 |
|
public function offsetGet($offset){ |
106
|
38 |
|
return $this->nodelist[$offset] ?? null; |
107
|
|
|
} |
108
|
|
|
|
109
|
38 |
|
public function offsetSet($offset, $value){ |
110
|
|
|
|
111
|
38 |
|
if($this->offsetExists($offset)){ |
112
|
|
|
$this->nodelist[$offset] = $value; |
113
|
|
|
} |
114
|
|
|
else{ |
115
|
38 |
|
$this->nodelist[] = $value; |
116
|
|
|
} |
117
|
38 |
|
} |
118
|
|
|
|
119
|
1 |
|
public function offsetUnset($offset){ |
120
|
1 |
|
unset($this->nodelist[$offset]); |
121
|
1 |
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
5 |
|
public function count():int{ |
125
|
5 |
|
return count($this->nodelist); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.