diff --git a/stubs/dom.stub b/stubs/dom.stub index 9f2b78b7da..209e07230c 100644 --- a/stubs/dom.stub +++ b/stubs/dom.stub @@ -49,7 +49,7 @@ class DOMElement extends DOMNode /** * @readonly - * @var DOMNamedNodeMap|null + * @var DOMNamedNodeMap */ public $attributes; @@ -67,9 +67,14 @@ class DOMElement extends DOMNode public function getElementsByTagNameNS ($namespaceURI, $localName) {} /** - * @phpstan-assert-if-true =DOMNamedNodeMap $this->attributes + * @phpstan-assert-if-true non-empty-string $this->getAttribute($qualifiedName) + * @phpstan-assert-if-true DOMAttr|DOMNameSpaceNode $this->getAttributeNode($qualifiedName) */ public function hasAttribute(string $qualifiedName): bool {} + + public function getAttribute(string $qualifiedName): string {} + + public function getAttributeNode(string $qualifiedName): DOMAttr|DOMNameSpaceNode|false {} } /** diff --git a/tests/PHPStan/Analyser/nsrt/DOMLegacyNamedNodeNap.php b/tests/PHPStan/Analyser/nsrt/DOMLegacyNamedNodeNap.php index 69bc94a93e..8d2a406f8c 100644 --- a/tests/PHPStan/Analyser/nsrt/DOMLegacyNamedNodeNap.php +++ b/tests/PHPStan/Analyser/nsrt/DOMLegacyNamedNodeNap.php @@ -22,30 +22,27 @@ public function basic_node(DOMNode $node): void { public function element_node(DOMElement $element): void { - assertType('DOMNamedNodeMap|null', $element->attributes); + assertType('DOMNamedNodeMap', $element->attributes); if ($element->hasAttribute('class')) { - assertType('DOMNamedNodeMap', $element->attributes); $attribute = $element->getAttributeNode('class'); assertType(DOMAttr::class, $attribute); assertType('string', $attribute->value); } else { - assertType('DOMNamedNodeMap|null', $element->attributes); + $attribute = $element->getAttributeNode('class'); + assertType('false', $attribute); } } public function element_node_attribute_fetch_via_attributes_property(DOMElement $element): void { - assertType('DOMNamedNodeMap|null', $element->attributes); + assertType('DOMNamedNodeMap', $element->attributes); if ($element->hasAttribute('class')) { - assertType('DOMNamedNodeMap', $element->attributes); $attribute = $element->attributes->getNamedItem('class'); if ($attribute === null) { return; } assertType(DOMAttr::class, $attribute); assertType('string', $attribute->value); - } else { - assertType('DOMNamedNodeMap|null', $element->attributes); } } } diff --git a/tests/PHPStan/Rules/Properties/data/dom-legacy-ext-template-nodes.php b/tests/PHPStan/Rules/Properties/data/dom-legacy-ext-template-nodes.php index ad39d72ab4..262510c714 100644 --- a/tests/PHPStan/Rules/Properties/data/dom-legacy-ext-template-nodes.php +++ b/tests/PHPStan/Rules/Properties/data/dom-legacy-ext-template-nodes.php @@ -22,3 +22,12 @@ function element_node_attribute_fetch_via_attributes_property(\DOMElement $eleme } echo $attribute->value; } + +function element_node_attribute_fetch_via_getAttributeNode(\DOMElement $element): void +{ + $attribute = $element->getAttributeNode('class'); + if ($attribute === null) { + return; + } + echo $attribute->value; +}