From 0c6761c8e0e64de03ca8273ce67684dff2c50d1e Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Tue, 27 May 2025 16:02:59 +0200 Subject: [PATCH 1/2] 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); + } + } +} From ed54aff6e93f5479f6ddcb560f5493017a866db8 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Wed, 11 Jun 2025 14:58:58 +0200 Subject: [PATCH 2/2] Try --- stubs/dom.stub | 4 ++-- tests/PHPStan/Analyser/nsrt/bug-13076.php | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/stubs/dom.stub b/stubs/dom.stub index 1126987a1c..074bdd5102 100644 --- a/stubs/dom.stub +++ b/stubs/dom.stub @@ -31,7 +31,7 @@ class DOMNode { /** - * @var DOMNamedNodeMap|null + * @var DOMNamedNodeMap|null */ public $attributes; @@ -46,7 +46,7 @@ class DOMNode class DOMElement extends DOMNode { - /** @var DOMNamedNodeMap */ + /** @var DOMNamedNodeMap */ public $attributes; /** @var DOMDocument */ diff --git a/tests/PHPStan/Analyser/nsrt/bug-13076.php b/tests/PHPStan/Analyser/nsrt/bug-13076.php index 533ca2aa74..dc6d25a8b6 100644 --- a/tests/PHPStan/Analyser/nsrt/bug-13076.php +++ b/tests/PHPStan/Analyser/nsrt/bug-13076.php @@ -9,18 +9,18 @@ class Foo public function test(\DOMNode $node): void { if ($node->hasAttributes()) { - assertType('DOMNamedNodeMap&iterable', $node->attributes); + assertType('DOMNamedNodeMap', $node->attributes); } else { - assertType('(DOMNamedNodeMap&iterable)|null', $node->attributes); + assertType('DOMNamedNodeMap|null', $node->attributes); } } public function testElement(\DOMElement $node): void { if ($node->hasAttributes()) { - assertType('DOMNamedNodeMap&iterable', $node->attributes); + assertType('DOMNamedNodeMap', $node->attributes); } else { - assertType('DOMNamedNodeMap&iterable', $node->attributes); + assertType('DOMNamedNodeMap', $node->attributes); } } }