Skip to content

Commit fb71678

Browse files
author
Andrew Scott
committed
Typing improvements
1 parent 929d7f7 commit fb71678

File tree

7 files changed

+113
-110
lines changed

7 files changed

+113
-110
lines changed

src/Collections/NodeCollection.php

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
class NodeCollection implements \Countable, \ArrayAccess, \RecursiveIterator
1212
{
1313
/** @var array */
14-
protected $nodes = [];
14+
protected array $nodes = [];
1515

1616
/**
1717
* @param iterable $nodes
1818
*/
19-
public function __construct(iterable $nodes = null) {
19+
public function __construct(?iterable $nodes = null) {
2020
if (!is_iterable($nodes)) {
2121
$nodes = [];
2222
}
@@ -42,7 +42,7 @@ public function count(): int {
4242
*
4343
* @return bool
4444
*/
45-
public function offsetExists($offset): bool {
45+
public function offsetExists(mixed $offset): bool {
4646
return isset($this->nodes[$offset]);
4747
}
4848

@@ -53,8 +53,7 @@ public function offsetExists($offset): bool {
5353
*
5454
* @return mixed
5555
*/
56-
#[\ReturnTypeWillChange]
57-
public function offsetGet($offset) {
56+
public function offsetGet(mixed $offset): mixed {
5857
return isset($this->nodes[$offset]) ? $this->nodes[$offset] : null;
5958
}
6059

@@ -64,7 +63,7 @@ public function offsetGet($offset) {
6463
* @param mixed $offset
6564
* @param mixed $value
6665
*/
67-
public function offsetSet($offset, $value): void {
66+
public function offsetSet(mixed $offset, mixed $value): void {
6867
if (is_null($offset)) {
6968
$this->nodes[] = $value;
7069
} else {
@@ -77,7 +76,7 @@ public function offsetSet($offset, $value): void {
7776
*
7877
* @param mixed $offset
7978
*/
80-
public function offsetUnset($offset): void {
79+
public function offsetUnset(mixed $offset): void {
8180
unset($this->nodes[$offset]);
8281
}
8382

@@ -124,8 +123,7 @@ public function hasChildren(): bool {
124123
*
125124
* @return mixed
126125
*/
127-
#[\ReturnTypeWillChange]
128-
public function current() {
126+
public function current(): mixed {
129127
return current($this->nodes);
130128
}
131129

@@ -135,31 +133,28 @@ public function current() {
135133
*
136134
* @return mixed
137135
*/
138-
#[\ReturnTypeWillChange]
139-
public function key() {
136+
public function key(): mixed {
140137
return key($this->nodes);
141138
}
142139

143140
/**
144141
* @see \RecursiveIterator::next()
145142
* @see \Iterator::next()
146143
*
147-
* @return mixed
144+
* @return void
148145
*/
149-
#[\ReturnTypeWillChange]
150-
public function next() {
151-
return next($this->nodes);
146+
public function next(): void {
147+
next($this->nodes);
152148
}
153149

154150
/**
155151
* @see \RecursiveIterator::rewind()
156152
* @see \Iterator::rewind()
157153
*
158-
* @return mixed
154+
* @return void
159155
*/
160-
#[\ReturnTypeWillChange]
161-
public function rewind() {
162-
return reset($this->nodes);
156+
public function rewind(): void {
157+
reset($this->nodes);
163158
}
164159

165160
/**

src/Document.php

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function collection(): NodeList {
6868
/**
6969
* {@inheritdoc}
7070
*/
71-
public function result(NodeList $nodeList) {
71+
public function result(NodeList $nodeList): NodeList|\DOMNode|null {
7272
if ($nodeList->count()) {
7373
return $nodeList->first();
7474
}
@@ -79,56 +79,60 @@ public function result(NodeList $nodeList) {
7979
/**
8080
* {@inheritdoc}
8181
*/
82-
public function parent() {
82+
public function parent(string|NodeList|\DOMNode|callable|null $selector = null): Document|Element|NodeList|null {
8383
return null;
8484
}
8585

8686
/**
8787
* {@inheritdoc}
8888
*/
89-
public function parents() {
89+
public function parents(?string $selector = null): NodeList {
9090
return $this->newNodeList();
9191
}
9292

9393
/**
9494
* {@inheritdoc}
9595
*/
96-
public function substituteWith($newNode): self {
97-
$this->replaceChild($newNode, $this);
96+
public function substituteWith(string|NodeList|\DOMNode|callable $input): self {
97+
$this->manipulateNodesWithInput($input, function($node, $newNodes) {
98+
foreach ($newNodes as $newNode) {
99+
$node->replaceChild($newNode, $node);
100+
}
101+
});
98102

99103
return $this;
100104
}
101105

102106
/**
103107
* {@inheritdoc}
104108
*/
105-
public function _clone() {
106-
return null;
109+
public function _clone(): void {
110+
return;
107111
}
108112

109113
/**
110114
* {@inheritdoc}
111115
*/
112-
public function getHtml(): string {
113-
return $this->getOuterHtml();
116+
public function getHtml(bool $isIncludeAll = false): string {
117+
return $this->getOuterHtml($isIncludeAll);
114118
}
115119

116120
/**
117121
* {@inheritdoc}
118122
*/
119-
public function setHtml($html): self {
120-
if (!is_string($html) || trim($html) == '') {
123+
public function setHtml(string|NodeList|\DOMNode|callable $input): self {
124+
if (!is_string($input) || trim($input) == '') {
121125
return $this;
122126
}
123127

124128
$internalErrors = libxml_use_internal_errors(true);
125129
if (\PHP_VERSION_ID < 80000) {
126130
$disableEntities = libxml_disable_entity_loader(true);
127-
$this->composeXmlNode($html);
131+
$this->composeXmlNode($input);
128132
libxml_use_internal_errors($internalErrors);
129133
libxml_disable_entity_loader($disableEntities);
130134
} else {
131-
$this->composeXmlNode($html);
135+
$this->composeXmlNode($input);
132136
libxml_use_internal_errors($internalErrors);
133137
}
134138

@@ -173,8 +177,7 @@ public function loadHTML(string $html, int $options = 0): bool {
173177
*
174178
* @return string|bool
175179
*/
176-
#[\ReturnTypeWillChange]
177-
public function saveHTML(\DOMNode $node = null) {
180+
public function saveHTML(?\DOMNode $node = null): string|false {
178181
$target = $node ?: $this;
179182

180183
// Undo any url encoding of attributes automatically applied by LibXML.
@@ -240,7 +243,7 @@ public function saveHTML(\DOMNode $node = null) {
240243
/*
241244
* @param $encoding string|null
242245
*/
243-
public function setEncoding(string $encoding = null) {
246+
public function setEncoding(?string $encoding = null): void {
244247
$this->documentEncoding = $encoding;
245248
}
246249

@@ -269,7 +272,7 @@ private function getCharset(string $html): ?string {
269272
/*
270273
* @param $html string
271274
*/
272-
private function detectEncoding(string $html) {
275+
private function detectEncoding(string $html): void {
273276
$charset = $this->getEncoding();
274277

275278
if (is_null($charset)) {
@@ -320,10 +323,9 @@ private function convertToUtf8(string $html): string {
320323
}
321324

322325
/**
323-
* @param $html
326+
* @param $html string
324327
*/
325-
private function composeXmlNode($html)
326-
{
328+
private function composeXmlNode(string $html): void {
327329
$this->detectEncoding($html);
328330

329331
$html = $this->convertToUtf8($html);

src/NodeList.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ class NodeList extends NodeCollection
2424
}
2525

2626
/** @var Document */
27-
protected $document;
27+
protected Document $document;
2828

2929
/**
3030
* @param Document $document
3131
* @param iterable $nodes
3232
*/
33-
public function __construct(Document $document = null, iterable $nodes = null) {
33+
public function __construct(Document $document = null, ?iterable $nodes = null) {
3434
parent::__construct($nodes);
3535

3636
$this->document = $document;
@@ -73,7 +73,7 @@ public function document(): ?\DOMDocument {
7373
/**
7474
* {@inheritdoc}
7575
*/
76-
public function result(NodeList $nodeList) {
76+
public function result(NodeList $nodeList): NodeList|\DOMNode|null {
7777
return $nodeList;
7878
}
7979

@@ -89,21 +89,26 @@ public function reverse(): NodeList {
8989
/**
9090
* @return mixed
9191
*/
92-
public function first() {
93-
return !empty($this->nodes) ? $this->rewind() : null;
92+
public function first(): mixed {
93+
if (!empty($this->nodes)) {
94+
$this->rewind();
95+
return $this->current();
96+
}
97+
98+
return null;
9499
}
95100

96101
/**
97102
* @return mixed
98103
*/
99-
public function last() {
104+
public function last(): mixed {
100105
return $this->end();
101106
}
102107

103108
/**
104109
* @return mixed
105110
*/
106-
public function end() {
111+
public function end(): mixed {
107112
return !empty($this->nodes) ? end($this->nodes) : null;
108113
}
109114

@@ -112,7 +117,7 @@ public function end() {
112117
*
113118
* @return mixed
114119
*/
115-
public function get(int $key) {
120+
public function get(int $key): mixed {
116121
if (isset($this->nodes[$key])) {
117122
return $this->nodes[$key];
118123
}
@@ -126,7 +131,7 @@ public function get(int $key) {
126131
*
127132
* @return self
128133
*/
129-
public function set(int $key, $value): self {
134+
public function set(int $key, mixed $value): self {
130135
$this->nodes[$key] = $value;
131136

132137
return $this;
@@ -174,21 +179,21 @@ public function map(callable $function): NodeList {
174179
*
175180
* @return iterable
176181
*/
177-
public function reduce(callable $function, $initial = null) {
182+
public function reduce(callable $function, mixed $initial = null) {
178183
return array_reduce($this->nodes, $function, $initial);
179184
}
180185

181186
/**
182187
* @return array
183188
*/
184-
public function toArray() {
189+
public function toArray(): iterable {
185190
return $this->nodes;
186191
}
187192

188193
/**
189194
* @param iterable $nodes
190195
*/
191-
public function fromArray(iterable $nodes = null) {
196+
public function fromArray(?iterable $nodes = null) {
192197
$this->nodes = [];
193198

194199
if (is_iterable($nodes)) {
@@ -203,7 +208,7 @@ public function fromArray(iterable $nodes = null) {
203208
*
204209
* @return NodeList
205210
*/
206-
public function merge($elements = []): NodeList {
211+
public function merge(NodeList|array $elements = []): NodeList {
207212
if (!is_array($elements)) {
208213
$elements = $elements->toArray();
209214
}
@@ -217,7 +222,7 @@ public function merge($elements = []): NodeList {
217222
*
218223
* @return NodeList
219224
*/
220-
public function slice(int $start, int $end = null): NodeList {
225+
public function slice(int $start, ?int $end = null): NodeList {
221226
$nodeList = array_slice($this->toArray(), $start, $end);
222227

223228
return $this->newNodeList($nodeList);

src/Traits/CommonTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ abstract public function document(): ?\DOMDocument;
2525
/**
2626
* @param NodeList $nodeList
2727
*
28-
* @return NodeList|\DOMNode
28+
* @return NodeList|\DOMNode|null
2929
*/
30-
abstract public function result(NodeList $nodeList);
30+
abstract public function result(NodeList $nodeList): NodeList|\DOMNode|null;
3131

3232
/**
3333
* @return bool

0 commit comments

Comments
 (0)