1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace DOMWrap\Collections; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Node List |
7
|
|
|
* |
8
|
|
|
* @package DOMWrap\Collections |
9
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3 Clause |
10
|
|
|
*/ |
11
|
|
|
class NodeCollection implements \Countable, \ArrayAccess, \RecursiveIterator |
12
|
|
|
{ |
13
|
|
|
/** @var array */ |
14
|
|
|
protected $nodes = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param iterable $nodes |
18
|
|
|
*/ |
19
|
138 |
|
public function __construct(iterable $nodes = null) { |
20
|
138 |
|
if (!is_iterable($nodes)) { |
21
|
1 |
|
$nodes = []; |
22
|
|
|
} |
23
|
|
|
|
24
|
138 |
|
foreach ($nodes as $node) { |
25
|
134 |
|
$this->nodes[] = $node; |
26
|
|
|
} |
27
|
138 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @see \Countable::count() |
31
|
|
|
* |
32
|
|
|
* @return int |
33
|
|
|
*/ |
34
|
76 |
|
public function count(): int { |
35
|
76 |
|
return count($this->nodes); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @see \ArrayAccess::offsetExists() |
40
|
|
|
* |
41
|
|
|
* @param mixed $offset |
42
|
|
|
* |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
|
public function offsetExists($offset): bool { |
46
|
|
|
return isset($this->nodes[$offset]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @see \ArrayAccess::offsetGet() |
51
|
|
|
* |
52
|
|
|
* @param mixed $offset |
53
|
|
|
* |
54
|
|
|
* @return mixed |
55
|
|
|
*/ |
56
|
20 |
|
#[\ReturnTypeWillChange] |
|
|
|
|
57
|
20 |
|
public function offsetGet($offset) { |
58
|
|
|
return isset($this->nodes[$offset]) ? $this->nodes[$offset] : null; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @see \ArrayAccess::offsetSet() |
63
|
|
|
* |
64
|
|
|
* @param mixed $offset |
65
|
|
|
* @param mixed $value |
66
|
81 |
|
*/ |
67
|
81 |
|
public function offsetSet($offset, $value): void { |
68
|
81 |
|
if (is_null($offset)) { |
69
|
|
|
$this->nodes[] = $value; |
70
|
|
|
} else { |
71
|
|
|
$this->nodes[$offset] = $value; |
72
|
81 |
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @see \ArrayAccess::offsetUnset() |
77
|
|
|
* |
78
|
|
|
* @param mixed $offset |
79
|
|
|
*/ |
80
|
|
|
public function offsetUnset($offset): void { |
81
|
|
|
unset($this->nodes[$offset]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @see \RecursiveIterator::RecursiveIteratorIterator() |
86
|
|
|
* |
87
|
|
|
* @return \RecursiveIteratorIterator |
88
|
|
|
*/ |
89
|
|
|
public function getRecursiveIterator(): \RecursiveIteratorIterator { |
90
|
|
|
return new \RecursiveIteratorIterator($this, \RecursiveIteratorIterator::SELF_FIRST); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @see \RecursiveIterator::getChildren() |
95
|
|
|
* |
96
|
|
|
* @return \RecursiveIterator |
97
|
|
|
*/ |
98
|
|
|
public function getChildren(): \RecursiveIterator { |
99
|
|
|
$nodes = []; |
100
|
|
|
|
101
|
|
|
if ($this->valid()) { |
102
|
|
|
$nodes = $this->current()->childNodes; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return new static($nodes); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @see \RecursiveIterator::hasChildren() |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
public function hasChildren(): bool { |
114
|
|
|
if ($this->valid()) { |
115
|
|
|
return $this->current()->hasChildNodes(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @see \RecursiveIterator::current() |
123
|
|
|
* @see \Iterator::current() |
124
|
|
|
* |
125
|
|
|
* @return mixed |
126
|
130 |
|
*/ |
127
|
130 |
|
#[\ReturnTypeWillChange] |
|
|
|
|
128
|
|
|
public function current() { |
129
|
|
|
return current($this->nodes); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @see \RecursiveIterator::key() |
134
|
|
|
* @see \Iterator::key() |
135
|
|
|
* |
136
|
1 |
|
* @return mixed |
137
|
1 |
|
*/ |
138
|
|
|
#[\ReturnTypeWillChange] |
|
|
|
|
139
|
|
|
public function key() { |
140
|
|
|
return key($this->nodes); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @see \RecursiveIterator::next() |
145
|
|
|
* @see \Iterator::next() |
146
|
130 |
|
* |
147
|
130 |
|
* @return mixed |
148
|
|
|
*/ |
149
|
|
|
#[\ReturnTypeWillChange] |
|
|
|
|
150
|
|
|
public function next() { |
151
|
|
|
return next($this->nodes); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @see \RecursiveIterator::rewind() |
156
|
133 |
|
* @see \Iterator::rewind() |
157
|
133 |
|
* |
158
|
|
|
* @return mixed |
159
|
|
|
*/ |
160
|
|
|
#[\ReturnTypeWillChange] |
|
|
|
|
161
|
|
|
public function rewind() { |
162
|
|
|
return reset($this->nodes); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
131 |
|
* @see \RecursiveIterator::valid() |
167
|
131 |
|
* @see \Iterator::valid() |
168
|
|
|
* |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
|
|
public function valid(): bool { |
172
|
|
|
return key($this->nodes) !== null; |
173
|
|
|
} |
174
|
|
|
} |
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.