Skip to content

Make DOMNamedNodeMap generic #4211

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

Open
wants to merge 1 commit into
base: 2.1.x
Choose a base branch
from
Open
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
36 changes: 31 additions & 5 deletions stubs/dom.stub
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ class DOMNode
{

/**
* @var DOMNamedNodeMap|null
* @var DOMNamedNodeMap<DOMAttr>|null
*/
public $attributes;

/**
* @phpstan-assert-if-true =DOMNamedNodeMap $this->attributes
* @phpstan-assert-if-true =DOMNamedNodeMap<DOMAttr> $this->attributes
* @return bool
*/
public function hasAttributes() {}
Expand All @@ -46,7 +46,7 @@ class DOMNode
class DOMElement extends DOMNode
{

/** @var DOMNamedNodeMap */
/** @var DOMNamedNodeMap<DOMAttr> */
public $attributes;

/** @var DOMDocument */
Expand Down Expand Up @@ -96,7 +96,7 @@ class DOMXPath

}

class DOMAttr
class DOMAttr extends DOMNode
{

/** @var DOMDocument */
Expand Down Expand Up @@ -155,11 +155,37 @@ class DOMProcessingInstruction
}

/**
* @template-covariant TNode as DOMNode
* @implements Traversable<int, TNode>
* @implements IteratorAggregate<int, TNode>
*
* @property-read int $length
*/
class DOMNamedNodeMap
class DOMNamedNodeMap implements Traversable, IteratorAggregate, Countable
Copy link
Contributor

@VincentLanglet VincentLanglet Aug 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't need to implements both Traversable and IteratorAggregate in the stub.

See
https://github.com/JetBrains/phpstorm-stubs/blob/6c034ae018ed7ac2cacf2474f824aede73658afa/dom/dom_c.php#L1381

Copy link
Author

@stefanfisk stefanfisk Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@VincentLanglet I just copied it from DOMNodeList at

class DOMNodeList implements Traversable, IteratorAggregate, Countable

Do you want me to fix DOMNodeList as well?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's done this way for DOMNodeList you can just ignore my remark I think

{
/**
* @return Iterator<TNode>
*/
public function getIterator(): Iterator {}

/**
* @param string $qualifiedName
* @return TNode|null
*/
public function getNamedItem($qualifiedName): ?DOMNode {}

/**
* @param string|null $namespace
* @param string $localName
* @return TNode|null
*/
public function getNamedItemNS($namespace, $localName): ?DOMNode {}

/**
* @param int $index
* @return TNode|null
*/
public function item($index): ?DOMNode {}
}

class DOMText
Expand Down
8 changes: 4 additions & 4 deletions tests/PHPStan/Analyser/nsrt/bug-13076.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ class Foo
public function test(\DOMNode $node): void
{
if ($node->hasAttributes()) {
assertType('DOMNamedNodeMap', $node->attributes);
assertType('DOMNamedNodeMap<DOMAttr>', $node->attributes);
} else {
assertType('DOMNamedNodeMap|null', $node->attributes);
assertType('DOMNamedNodeMap<DOMAttr>|null', $node->attributes);
}
}

public function testElement(\DOMElement $node): void
{
if ($node->hasAttributes()) {
assertType('DOMNamedNodeMap', $node->attributes);
assertType('DOMNamedNodeMap<DOMAttr>', $node->attributes);
} else {
assertType('DOMNamedNodeMap', $node->attributes);
assertType('DOMNamedNodeMap<DOMAttr>', $node->attributes);
}
}
}
27 changes: 27 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13365.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types = 1);

namespace Bug13365;

use function PHPStan\Testing\assertType;

class Foo
{
public function test(\DOMElement $element): void
{
$attributes = $element->attributes;

if ($attributes === null) {
return;
}

assertType('DOMNamedNodeMap<DOMAttr>', $attributes);
assertType('Iterator<mixed, DOMAttr>', $attributes->getIterator());
assertType('DOMAttr|null', $attributes->getNamedItem('foo'));
assertType('DOMAttr|null', $attributes->getNamedItemNS('foo', 'bar'));
assertType('DOMAttr|null', $attributes->item(0));

foreach ($element->attributes ?? [] as $attr) {
assertType('DOMAttr', $attr);
}
}
}
Loading