@@ -37,10 +37,13 @@ class Crawler implements \Countable, \IteratorAggregate
37
37
38
38
/**
39
39
* A map of cached namespaces.
40
+ *
41
+ * @var \ArrayObject<string, string|null>
40
42
*/
41
43
private \ArrayObject $ cachedNamespaces ;
42
44
43
45
private ?string $ baseHref ;
46
+
44
47
private ?\DOMDocument $ document = null ;
45
48
46
49
/**
@@ -56,7 +59,7 @@ class Crawler implements \Countable, \IteratorAggregate
56
59
private ?HTML5 $ html5Parser = null ;
57
60
58
61
/**
59
- * @param \DOMNodeList|\DOMNode|\DOMNode[]|string|null $node A Node to use as the base for the crawling
62
+ * @param \DOMNodeList<\DOMNode> |\DOMNode|\DOMNode[]|string|null $node A Node to use as the base for the crawling
60
63
*/
61
64
public function __construct (
62
65
\DOMNodeList |\DOMNode |array |string |null $ node = null ,
@@ -107,9 +110,7 @@ public function clear(): void
107
110
* This method uses the appropriate specialized add*() method based
108
111
* on the type of the argument.
109
112
*
110
- * @param \DOMNodeList|\DOMNode|\DOMNode[]|string|null $node A node
111
- *
112
- * @throws \InvalidArgumentException when node is not the expected type
113
+ * @param \DOMNodeList<\DOMNode>|\DOMNode|\DOMNode[]|string|null $node
113
114
*/
114
115
public function add (\DOMNodeList |\DOMNode |array |string |null $ node ): void
115
116
{
@@ -121,8 +122,6 @@ public function add(\DOMNodeList|\DOMNode|array|string|null $node): void
121
122
$ this ->addNodes ($ node );
122
123
} elseif (\is_string ($ node )) {
123
124
$ this ->addContent ($ node );
124
- } elseif (null !== $ node ) {
125
- throw new \InvalidArgumentException (\sprintf ('Expecting a DOMNodeList or DOMNode instance, an array, a string, or null, but got "%s". ' , get_debug_type ($ node )));
126
125
}
127
126
}
128
127
@@ -232,8 +231,6 @@ public function addXmlContent(string $content, string $charset = 'UTF-8', int $o
232
231
233
232
/**
234
233
* Adds a \DOMDocument to the list of nodes.
235
- *
236
- * @param \DOMDocument $dom A \DOMDocument instance
237
234
*/
238
235
public function addDocument (\DOMDocument $ dom ): void
239
236
{
@@ -245,7 +242,7 @@ public function addDocument(\DOMDocument $dom): void
245
242
/**
246
243
* Adds a \DOMNodeList to the list of nodes.
247
244
*
248
- * @param \DOMNodeList $nodes A \DOMNodeList instance
245
+ * @param \DOMNodeList<\DOMNode> $nodes
249
246
*/
250
247
public function addNodeList (\DOMNodeList $ nodes ): void
251
248
{
@@ -259,7 +256,7 @@ public function addNodeList(\DOMNodeList $nodes): void
259
256
/**
260
257
* Adds an array of \DOMNode instances to the list of nodes.
261
258
*
262
- * @param \DOMNode[] $nodes An array of \DOMNode instances
259
+ * @param \DOMNode[] $nodes
263
260
*/
264
261
public function addNodes (array $ nodes ): void
265
262
{
@@ -270,8 +267,6 @@ public function addNodes(array $nodes): void
270
267
271
268
/**
272
269
* Adds a \DOMNode instance to the list of nodes.
273
- *
274
- * @param \DOMNode $node A \DOMNode instance
275
270
*/
276
271
public function addNode (\DOMNode $ node ): void
277
272
{
@@ -313,13 +308,13 @@ public function eq(int $position): static
313
308
*
314
309
* Example:
315
310
*
316
- * $crawler->filter('h1')->each(function ($node, $i) {
317
- * return $node->text();
318
- * });
311
+ * $crawler->filter('h1')->each(fn ($node, $i) => $node->text());
312
+ *
313
+ * @template R of mixed
319
314
*
320
- * @param \Closure $closure An anonymous function
315
+ * @param \Closure(static, int):R $closure
321
316
*
322
- * @return array An array of values returned by the anonymous function
317
+ * @return list<R> An array of values returned by the anonymous function
323
318
*/
324
319
public function each (\Closure $ closure ): array
325
320
{
@@ -344,7 +339,7 @@ public function slice(int $offset = 0, ?int $length = null): static
344
339
*
345
340
* To remove a node from the list, the anonymous function must return false.
346
341
*
347
- * @param \Closure $closure An anonymous function
342
+ * @param \Closure(static, int):bool $closure
348
343
*/
349
344
public function reduce (\Closure $ closure ): static
350
345
{
@@ -377,7 +372,7 @@ public function last(): static
377
372
/**
378
373
* Returns the siblings nodes of the current selection.
379
374
*
380
- * @throws \InvalidArgumentException When current node is empty
375
+ * @throws \InvalidArgumentException When the current node is empty
381
376
*/
382
377
public function siblings (): static
383
378
{
@@ -405,6 +400,8 @@ public function matches(string $selector): bool
405
400
*
406
401
* @see https://developer.mozilla.org/en-US/docs/Web/API/Element/closest#Polyfill
407
402
*
403
+ * @return ?static
404
+ *
408
405
* @throws \InvalidArgumentException When current node is empty
409
406
*/
410
407
public function closest (string $ selector ): ?self
@@ -444,7 +441,7 @@ public function nextAll(): static
444
441
/**
445
442
* Returns the previous sibling nodes of the current selection.
446
443
*
447
- * @throws \InvalidArgumentException
444
+ * @throws \InvalidArgumentException When current node is empty
448
445
*/
449
446
public function previousAll (): static
450
447
{
@@ -481,7 +478,7 @@ public function ancestors(): static
481
478
/**
482
479
* Returns the children nodes of the current selection.
483
480
*
484
- * @throws \InvalidArgumentException When current node is empty
481
+ * @throws \InvalidArgumentException When the current node is empty
485
482
* @throws \RuntimeException If the CssSelector Component is not available and $selector is provided
486
483
*/
487
484
public function children (?string $ selector = null ): static
@@ -527,7 +524,7 @@ public function attr(string $attribute, ?string $default = null): ?string
527
524
/**
528
525
* Returns the node name of the first node of the list.
529
526
*
530
- * @throws \InvalidArgumentException When current node is empty
527
+ * @throws \InvalidArgumentException When the current node is empty
531
528
*/
532
529
public function nodeName (): string
533
530
{
@@ -594,7 +591,7 @@ public function innerText(bool $normalizeWhitespace = true): string
594
591
*
595
592
* @param string|null $default When not null: the value to return when the current node is empty
596
593
*
597
- * @throws \InvalidArgumentException When current node is empty
594
+ * @throws \InvalidArgumentException When the current node is empty
598
595
*/
599
596
public function html (?string $ default = null ): string
600
597
{
@@ -621,6 +618,9 @@ public function html(?string $default = null): string
621
618
return $ html ;
622
619
}
623
620
621
+ /**
622
+ * @throws \InvalidArgumentException When the current node is empty
623
+ */
624
624
public function outerHtml (): string
625
625
{
626
626
if (!\count ($ this )) {
@@ -642,6 +642,8 @@ public function outerHtml(): string
642
642
*
643
643
* Since an XPath expression might evaluate to either a simple type or a \DOMNodeList,
644
644
* this method will return either an array of simple types or a new Crawler instance.
645
+ *
646
+ * @return array|static
645
647
*/
646
648
public function evaluate (string $ xpath ): array |self
647
649
{
@@ -1078,7 +1080,7 @@ private function supportsEncoding(string $encoding): bool
1078
1080
{
1079
1081
try {
1080
1082
return '' === @mb_convert_encoding ('' , $ encoding , 'UTF-8 ' );
1081
- } catch (\Throwable $ e ) {
1083
+ } catch (\Throwable ) {
1082
1084
return false ;
1083
1085
}
1084
1086
}
@@ -1178,7 +1180,7 @@ private function discoverNamespace(\DOMXPath $domxpath, string $prefix): ?string
1178
1180
// ask for one namespace, otherwise we'd get a collection with an item for each node
1179
1181
$ namespaces = $ domxpath ->query (\sprintf ('(//namespace::*[name()="%s"])[last()] ' , $ this ->defaultNamespacePrefix === $ prefix ? '' : $ prefix ));
1180
1182
1181
- return $ this ->cachedNamespaces [$ prefix ] = ( $ node = $ namespaces ->item (0 )) ? $ node ->nodeValue : null ;
1183
+ return $ this ->cachedNamespaces [$ prefix ] = $ namespaces ->item (0 )? ->nodeValue;
1182
1184
}
1183
1185
1184
1186
private function findNamespacePrefixes (string $ xpath ): array
@@ -1193,7 +1195,7 @@ private function findNamespacePrefixes(string $xpath): array
1193
1195
/**
1194
1196
* Creates a crawler for some subnodes.
1195
1197
*
1196
- * @param \DOMNodeList|\DOMNode|\DOMNode[]|string|null $nodes
1198
+ * @param \DOMNodeList<\DOMNode> |\DOMNode|\DOMNode[]|string|null $nodes
1197
1199
*/
1198
1200
private function createSubCrawler (\DOMNodeList |\DOMNode |array |string |null $ nodes ): static
1199
1201
{
0 commit comments