Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions ext/dom/tests/modern/xml/return_scalars_from_xpath.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
Returning scalar values from Dom\XPath callback
--EXTENSIONS--
dom
--FILE--
<?php
$dom = Dom\XMLDocument::createFromString('<root/>');
$xpath = new Dom\XPath($dom);
$xpath->registerPhpFunctionNs('urn:x', 'get-int', fn(): int => 42);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • you should also test edge cases such as NAN, PHP_INT_MIN/MAX ...
  • Using XSLTProcessor::registerPHPFunctions for returning an int/float to assert proper values even as strings.
  • demonstrating benefits of your changes through an expression, e.g. x:get-int() + 1

$xpath->registerPhpFunctionNs('urn:x', 'get-float', fn(): float => 4.2);
$xpath->registerPhpFunctionNs('urn:x', 'get-string', fn(): string => "test");
$xpath->registerPhpFunctionNs('urn:x', 'get-bool', fn(bool $b): bool => $b);
$xpath->registerNamespace('x', 'urn:x');
var_dump($xpath->evaluate('x:get-int()'));
var_dump($xpath->evaluate('x:get-float()'));
var_dump($xpath->evaluate('x:get-string()'));
var_dump($xpath->evaluate('x:get-bool(1)'));
var_dump($xpath->evaluate('x:get-bool(0)'));
?>
--EXPECT--
float(42)
float(4.2)
string(4) "test"
bool(true)
bool(false)
4 changes: 4 additions & 0 deletions ext/dom/xpath_callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,10 @@ static zend_result php_dom_xpath_callback_dispatch(php_dom_xpath_callbacks *xpat
valuePush(ctxt, xmlXPathNewNodeSet(nodep));
} else if (Z_TYPE(callback_retval) == IS_FALSE || Z_TYPE(callback_retval) == IS_TRUE) {
valuePush(ctxt, xmlXPathNewBoolean(Z_TYPE(callback_retval) == IS_TRUE));
} else if (Z_TYPE(callback_retval) == IS_LONG) {
valuePush(ctxt, xmlXPathNewFloat(Z_LVAL(callback_retval)));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a BC break thus need a good UPGRADING entry.

Copy link
Copy Markdown
Member

@ndossche ndossche May 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will break for numbers > 2**52 on 64-bit systems. Previously, it was represented as a string and could be coerced back to a php int without precision loss. Doubles can't reliably store integers > 2**52 so you would get precision loss.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is what tests are for ;)

} else if (Z_TYPE(callback_retval) == IS_DOUBLE) {
valuePush(ctxt, xmlXPathNewFloat(Z_DVAL(callback_retval)));
} else if (Z_TYPE(callback_retval) == IS_OBJECT) {
zend_type_error("Only objects that are instances of DOM nodes can be converted to an XPath expression");
zval_ptr_dtor(&callback_retval);
Expand Down
Loading