psalm icon indicating copy to clipboard operation
psalm copied to clipboard

regression: ownerDocument can be null

Open theseer opened this issue 10 months ago • 4 comments

Back in 2020 to resolve issue #3081 a change was made to incorporate the fact that DOMNode::ownerDocument can be null. This was effectively reverted with PR #10619.

This reverting change is technically wrong and not following the actual implementation in PHP.

The example linked in the PR (https://3v4l.org/tWoCc) about PHP enforcing the non-nullable type is misleading/incorrect, as a modified, even simpler, example demonstrates: https://3v4l.org/fGc7t

In other words: The ownerDocument-Property can very much (still) be null. Whether or not this is a desirable behavior is a different story.

theseer avatar Apr 03 '24 12:04 theseer

Hey @theseer, can you reproduce the issue on https://psalm.dev? These will be used as phpunit tests when implementing the feature or fixing this bug.

psalm-github-bot[bot] avatar Apr 03 '24 12:04 psalm-github-bot[bot]

https://psalm.dev/r/cfd852bd7a

theseer avatar Apr 03 '24 12:04 theseer

I found these snippets:

https://psalm.dev/r/cfd852bd7a
<?php

$node = new DOMElement('foo');
assert($node->ownerDocument instanceof DOMDocument);
Psalm output (using commit ef3b018):

ERROR: RedundantCondition - 4:1 - Type DOMDocument for $node->ownerDocument is always DOMDocument

psalm-github-bot[bot] avatar Apr 03 '24 12:04 psalm-github-bot[bot]

The official stubs show that @theseer is right: https://github.com/php/php-src/blob/PHP-8.3.5/ext/dom/php_dom.stub.php#L335

sebastianbergmann avatar Apr 03 '24 12:04 sebastianbergmann