diff --git a/stubs/dom.stub b/stubs/dom.stub index df03926915..1126987a1c 100644 --- a/stubs/dom.stub +++ b/stubs/dom.stub @@ -31,12 +31,12 @@ class DOMNode { /** - * @var DOMNamedNodeMap|null + * @var DOMNamedNodeMap|null */ public $attributes; /** - * @phpstan-assert-if-true !null $this->attributes + * @phpstan-assert-if-true =DOMNamedNodeMap $this->attributes * @return bool */ public function hasAttributes() {} @@ -46,6 +46,9 @@ class DOMNode class DOMElement extends DOMNode { + /** @var DOMNamedNodeMap */ + public $attributes; + /** @var DOMDocument */ public $ownerDocument; diff --git a/tests/PHPStan/Analyser/nsrt/bug-13076.php b/tests/PHPStan/Analyser/nsrt/bug-13076.php new file mode 100644 index 0000000000..533ca2aa74 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-13076.php @@ -0,0 +1,26 @@ +hasAttributes()) { + assertType('DOMNamedNodeMap&iterable', $node->attributes); + } else { + assertType('(DOMNamedNodeMap&iterable)|null', $node->attributes); + } + } + + public function testElement(\DOMElement $node): void + { + if ($node->hasAttributes()) { + assertType('DOMNamedNodeMap&iterable', $node->attributes); + } else { + assertType('DOMNamedNodeMap&iterable', $node->attributes); + } + } +}