Skip to content

[DomCrawler] Improve phpdoc annotations #61482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/expected-missing-return-types.diff
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,23 @@ diff --git a/src/Symfony/Component/DependencyInjection/Extension/PrependExtensio
- public function prepend(ContainerBuilder $container);
+ public function prepend(ContainerBuilder $container): void;
}
diff --git a/src/Symfony/Component/DomCrawler/Crawler.php b/src/Symfony/Component/DomCrawler/Crawler.php
--- a/src/Symfony/Component/DomCrawler/Crawler.php
+++ b/src/Symfony/Component/DomCrawler/Crawler.php
@@ -405,5 +405,5 @@ class Crawler implements \Countable, \IteratorAggregate
* @throws \InvalidArgumentException When current node is empty
*/
- public function closest(string $selector): ?self
+ public function closest(string $selector): ?static
{
if (!$this->nodes) {
@@ -646,5 +646,5 @@ class Crawler implements \Countable, \IteratorAggregate
* @return array|static
*/
- public function evaluate(string $xpath): array|self
+ public function evaluate(string $xpath): array|static
{
if (null === $this->document) {
diff --git a/src/Symfony/Component/Emoji/EmojiTransliterator.php b/src/Symfony/Component/Emoji/EmojiTransliterator.php
--- a/src/Symfony/Component/Emoji/EmojiTransliterator.php
+++ b/src/Symfony/Component/Emoji/EmojiTransliterator.php
Expand Down
54 changes: 28 additions & 26 deletions src/Symfony/Component/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@

/**
* A map of cached namespaces.
*
* @var \ArrayObject<string, string|null>
*/
private \ArrayObject $cachedNamespaces;

private ?string $baseHref;

private ?\DOMDocument $document = null;

/**
Expand All @@ -56,7 +59,7 @@
private ?HTML5 $html5Parser = null;

/**
* @param \DOMNodeList|\DOMNode|\DOMNode[]|string|null $node A Node to use as the base for the crawling
* @param \DOMNodeList<\DOMNode>|\DOMNode|\DOMNode[]|string|null $node A Node to use as the base for the crawling

Check failure on line 62 in src/Symfony/Component/DomCrawler/Crawler.php

View workflow job for this annotation

GitHub Actions / Psalm

TooManyTemplateParams

src/Symfony/Component/DomCrawler/Crawler.php:62:15: TooManyTemplateParams: DOMNodeList<DOMNode> has too many template params, expecting 0 (see https://psalm.dev/184)

Check failure on line 62 in src/Symfony/Component/DomCrawler/Crawler.php

View workflow job for this annotation

GitHub Actions / Psalm

TooManyTemplateParams

src/Symfony/Component/DomCrawler/Crawler.php:62:15: TooManyTemplateParams: DOMNodeList<DOMNode> has too many template params, expecting 0 (see https://psalm.dev/184)
*/
public function __construct(
\DOMNodeList|\DOMNode|array|string|null $node = null,
Expand Down Expand Up @@ -107,9 +110,7 @@
* This method uses the appropriate specialized add*() method based
* on the type of the argument.
*
* @param \DOMNodeList|\DOMNode|\DOMNode[]|string|null $node A node
*
* @throws \InvalidArgumentException when node is not the expected type
* @param \DOMNodeList<\DOMNode>|\DOMNode|\DOMNode[]|string|null $node

Check failure on line 113 in src/Symfony/Component/DomCrawler/Crawler.php

View workflow job for this annotation

GitHub Actions / Psalm

TooManyTemplateParams

src/Symfony/Component/DomCrawler/Crawler.php:113:15: TooManyTemplateParams: DOMNodeList<DOMNode> has too many template params, expecting 0 (see https://psalm.dev/184)

Check failure on line 113 in src/Symfony/Component/DomCrawler/Crawler.php

View workflow job for this annotation

GitHub Actions / Psalm

TooManyTemplateParams

src/Symfony/Component/DomCrawler/Crawler.php:113:15: TooManyTemplateParams: DOMNodeList<DOMNode> has too many template params, expecting 0 (see https://psalm.dev/184)
*/
public function add(\DOMNodeList|\DOMNode|array|string|null $node): void
{
Expand All @@ -121,8 +122,6 @@
$this->addNodes($node);
} elseif (\is_string($node)) {
$this->addContent($node);
} elseif (null !== $node) {
throw new \InvalidArgumentException(\sprintf('Expecting a DOMNodeList or DOMNode instance, an array, a string, or null, but got "%s".', get_debug_type($node)));
}
}

Expand Down Expand Up @@ -232,8 +231,6 @@

/**
* Adds a \DOMDocument to the list of nodes.
*
* @param \DOMDocument $dom A \DOMDocument instance
*/
public function addDocument(\DOMDocument $dom): void
{
Expand All @@ -245,7 +242,7 @@
/**
* Adds a \DOMNodeList to the list of nodes.
*
* @param \DOMNodeList $nodes A \DOMNodeList instance
* @param \DOMNodeList<\DOMNode> $nodes

Check failure on line 245 in src/Symfony/Component/DomCrawler/Crawler.php

View workflow job for this annotation

GitHub Actions / Psalm

TooManyTemplateParams

src/Symfony/Component/DomCrawler/Crawler.php:245:15: TooManyTemplateParams: DOMNodeList<DOMNode> has too many template params, expecting 0 (see https://psalm.dev/184)

Check failure on line 245 in src/Symfony/Component/DomCrawler/Crawler.php

View workflow job for this annotation

GitHub Actions / Psalm

TooManyTemplateParams

src/Symfony/Component/DomCrawler/Crawler.php:245:15: TooManyTemplateParams: DOMNodeList<DOMNode> has too many template params, expecting 0 (see https://psalm.dev/184)
*/
public function addNodeList(\DOMNodeList $nodes): void
{
Expand All @@ -259,7 +256,7 @@
/**
* Adds an array of \DOMNode instances to the list of nodes.
*
* @param \DOMNode[] $nodes An array of \DOMNode instances
* @param \DOMNode[] $nodes
*/
public function addNodes(array $nodes): void
{
Expand All @@ -270,8 +267,6 @@

/**
* Adds a \DOMNode instance to the list of nodes.
*
* @param \DOMNode $node A \DOMNode instance
*/
public function addNode(\DOMNode $node): void
{
Expand Down Expand Up @@ -313,13 +308,13 @@
*
* Example:
*
* $crawler->filter('h1')->each(function ($node, $i) {
* return $node->text();
* });
* $crawler->filter('h1')->each(fn ($node, $i) => $node->text());
*
* @template R of mixed
*
* @param \Closure $closure An anonymous function
* @param \Closure(static, int):R $closure
*
* @return array An array of values returned by the anonymous function
* @return list<R> An array of values returned by the anonymous function
*/
public function each(\Closure $closure): array
{
Expand All @@ -344,7 +339,7 @@
*
* To remove a node from the list, the anonymous function must return false.
*
* @param \Closure $closure An anonymous function
* @param \Closure(static, int):bool $closure
*/
public function reduce(\Closure $closure): static
{
Expand Down Expand Up @@ -377,7 +372,7 @@
/**
* Returns the siblings nodes of the current selection.
*
* @throws \InvalidArgumentException When current node is empty
* @throws \InvalidArgumentException When the current node is empty
*/
public function siblings(): static
{
Expand Down Expand Up @@ -405,6 +400,8 @@
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/Element/closest#Polyfill
*
* @return ?static
*
* @throws \InvalidArgumentException When current node is empty
*/
public function closest(string $selector): ?self
Expand Down Expand Up @@ -444,7 +441,7 @@
/**
* Returns the previous sibling nodes of the current selection.
*
* @throws \InvalidArgumentException
* @throws \InvalidArgumentException When current node is empty
*/
public function previousAll(): static
{
Expand Down Expand Up @@ -481,7 +478,7 @@
/**
* Returns the children nodes of the current selection.
*
* @throws \InvalidArgumentException When current node is empty
* @throws \InvalidArgumentException When the current node is empty
* @throws \RuntimeException If the CssSelector Component is not available and $selector is provided
*/
public function children(?string $selector = null): static
Expand Down Expand Up @@ -527,7 +524,7 @@
/**
* Returns the node name of the first node of the list.
*
* @throws \InvalidArgumentException When current node is empty
* @throws \InvalidArgumentException When the current node is empty
*/
public function nodeName(): string
{
Expand Down Expand Up @@ -594,7 +591,7 @@
*
* @param string|null $default When not null: the value to return when the current node is empty
*
* @throws \InvalidArgumentException When current node is empty
* @throws \InvalidArgumentException When the current node is empty
*/
public function html(?string $default = null): string
{
Expand All @@ -621,6 +618,9 @@
return $html;
}

/**
* @throws \InvalidArgumentException When the current node is empty
*/
public function outerHtml(): string
{
if (!\count($this)) {
Expand All @@ -642,6 +642,8 @@
*
* Since an XPath expression might evaluate to either a simple type or a \DOMNodeList,
* this method will return either an array of simple types or a new Crawler instance.
*
* @return array|static
*/
public function evaluate(string $xpath): array|self
{
Expand Down Expand Up @@ -1078,7 +1080,7 @@
{
try {
return '' === @mb_convert_encoding('', $encoding, 'UTF-8');
} catch (\Throwable $e) {
} catch (\Throwable) {
return false;
}
}
Expand Down Expand Up @@ -1178,7 +1180,7 @@
// ask for one namespace, otherwise we'd get a collection with an item for each node
$namespaces = $domxpath->query(\sprintf('(//namespace::*[name()="%s"])[last()]', $this->defaultNamespacePrefix === $prefix ? '' : $prefix));

return $this->cachedNamespaces[$prefix] = ($node = $namespaces->item(0)) ? $node->nodeValue : null;
return $this->cachedNamespaces[$prefix] = $namespaces->item(0)?->nodeValue;
}

private function findNamespacePrefixes(string $xpath): array
Expand All @@ -1193,7 +1195,7 @@
/**
* Creates a crawler for some subnodes.
*
* @param \DOMNodeList|\DOMNode|\DOMNode[]|string|null $nodes
* @param \DOMNodeList<\DOMNode>|\DOMNode|\DOMNode[]|string|null $nodes

Check failure on line 1198 in src/Symfony/Component/DomCrawler/Crawler.php

View workflow job for this annotation

GitHub Actions / Psalm

TooManyTemplateParams

src/Symfony/Component/DomCrawler/Crawler.php:1198:15: TooManyTemplateParams: DOMNodeList<DOMNode> has too many template params, expecting 0 (see https://psalm.dev/184)

Check failure on line 1198 in src/Symfony/Component/DomCrawler/Crawler.php

View workflow job for this annotation

GitHub Actions / Psalm

TooManyTemplateParams

src/Symfony/Component/DomCrawler/Crawler.php:1198:15: TooManyTemplateParams: DOMNodeList<DOMNode> has too many template params, expecting 0 (see https://psalm.dev/184)
*/
private function createSubCrawler(\DOMNodeList|\DOMNode|array|string|null $nodes): static
{
Expand Down
Loading