From b0d0aea658f666d2ee31d17dca5575b38b20dce1 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Tue, 27 May 2025 16:02:59 +0200 Subject: [PATCH] Fix 13076 --- stubs/dom.stub | 7 ++++-- tests/PHPStan/Analyser/nsrt/bug-13076.php | 26 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 tests/PHPStan/Analyser/nsrt/bug-13076.php 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); + } + } +}