Skip to content

Fix simplexml_import_dom() parameter type on PHP 8.4 - #6290

Merged
ondrejmirtes merged 1 commit into
phpstan:2.2.xfrom
janedbal:simplexml-import-dom-object-php84
Aug 27, 2026
Merged

Fix simplexml_import_dom() parameter type on PHP 8.4#6290
ondrejmirtes merged 1 commit into
phpstan:2.2.xfrom
janedbal:simplexml-import-dom-object-php84

Conversation

@janedbal

@janedbal janedbal commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

PHP 8.4 widened the first parameter of simplexml_import_dom() to object:

// PHP-8.3 ext/simplexml/simplexml.stub.php
function simplexml_import_dom(SimpleXMLElement|DOMNode $node, ?string $class_name = SimpleXMLElement::class): ?SimpleXMLElement {}

// PHP-8.4 ext/simplexml/simplexml.stub.php
function simplexml_import_dom(object $node, ?string $class_name = SimpleXMLElement::class): ?SimpleXMLElement {}

The upstream reasoning, quoted from that commit message:

It needs to be "object". This is because first- and third-party extension can register custom node types using php_libxml_register_export. So we don't know upfront what types can be expected.

resources/functionMap.php still declares DOMNode for every version, so PHPStan reports a false positive for a Dom\Node argument:

$document = Dom\XMLDocument::createFromString('<root><a>1</a></root>');
$node = $document->firstChild;
if ($node !== null) {
    simplexml_import_dom($node);
}
Parameter #1 $node of function simplexml_import_dom expects DOMNode, Dom\Node given.

The code is correct — simplexml_import_dom() accepts a Dom\Node at runtime on PHP 8.4 and 8.5.

This adds the 8.4 signature to functionMap_php84delta.php. Versions below 8.4 keep the previous type, so a non-DOM argument is still reported there.

Verification

  • With phpVersion: 80400, the example above reports the error before the change and no error after it.
  • With phpVersion: 80300, simplexml_import_dom(new stdClass()) is still reported.
  • tests/PHPStan/Reflection/SignatureMap/ passes (2844 tests).

Note

object is deliberately open-ended upstream, so a closed union such as DOMNode|Dom\Node|SimpleXMLElement would reject valid arguments from extensions that register their own node types. That is why this mirrors php-src rather than narrowing.

Separately, the pre-8.4 entry omits SimpleXMLElement, which the 8.0–8.3 stubs allow. I left that alone to keep this change focused; glad to send it as its own PR.

Claude

PHP 8.4 widened the first parameter to `object`, so that the function
accepts the classes of the new DOM API next to `DOMNode`:

    // PHP 8.3
    function simplexml_import_dom(SimpleXMLElement|DOMNode $node, ?string $class_name = SimpleXMLElement::class): ?SimpleXMLElement {}

    // PHP 8.4
    function simplexml_import_dom(object $node, ?string $class_name = SimpleXMLElement::class): ?SimpleXMLElement {}

The map still declares `DOMNode` for every version. As a result, PHPStan
reports a false positive when the argument is a `Dom\Node`:

    Parameter #1 $node of function simplexml_import_dom expects DOMNode, Dom\Node given.

Versions below 8.4 keep the previous type.
@ondrejmirtes
ondrejmirtes merged commit 09b0c99 into phpstan:2.2.x Aug 27, 2026
754 of 766 checks passed
@ondrejmirtes

Copy link
Copy Markdown
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants