1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

Improve error messages for XSLTProcessor::transformToDoc() (#12332)

* Improve error messages for XSLTProcessor::transformToDoc()

Also adds a relevant test file, as these branches were untested before.
This commit is contained in:
Niels Dossche
2023-10-02 19:21:54 +02:00
committed by GitHub
parent 41ea14b154
commit f88368cfc8
2 changed files with 39 additions and 9 deletions

View File

@@ -0,0 +1,32 @@
--TEST--
XSLTProcessor::transformToDoc class exceptions
--EXTENSIONS--
xsl
simplexml
dom
--FILE--
<?php
$sxe = simplexml_load_file(__DIR__ . '/53965/collection.xml');
$processor = new XSLTProcessor;
$dom = new DOMDocument;
$dom->load(__DIR__ . '/53965/collection.xsl');
$processor->importStylesheet($dom);
try {
$processor->transformToDoc($sxe, NonExistent::class);
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
try {
$processor->transformToDoc($sxe, DOMDocument::class);
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
?>
--EXPECT--
XSLTProcessor::transformToDoc(): Argument #2 ($returnClass) must be a valid class name or null, NonExistent given
XSLTProcessor::transformToDoc(): Argument #2 ($returnClass) must be a class name compatible with SimpleXMLElement, DOMDocument given

View File

@@ -497,14 +497,14 @@ PHP_METHOD(XSLTProcessor, transformToDoc)
zval *id, *docp = NULL;
xmlDoc *newdocp;
xsltStylesheetPtr sheetp;
zend_string *ret_class = NULL;
zend_class_entry *ret_class = NULL;
xsl_object *intern;
id = ZEND_THIS;
intern = Z_XSL_P(id);
sheetp = (xsltStylesheetPtr) intern->ptr;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "o|S!", &docp, &ret_class) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "o|C!", &docp, &ret_class) == FAILURE) {
RETURN_THROWS();
}
@@ -513,7 +513,7 @@ PHP_METHOD(XSLTProcessor, transformToDoc)
if (newdocp) {
if (ret_class) {
zend_string *curclass_name;
zend_class_entry *curce, *ce;
zend_class_entry *curce;
php_libxml_node_object *interndoc;
curce = Z_OBJCE_P(docp);
@@ -522,16 +522,15 @@ PHP_METHOD(XSLTProcessor, transformToDoc)
curce = curce->parent;
}
ce = zend_lookup_class(ret_class);
if (ce == NULL || !instanceof_function(ce, curce)) {
if (!instanceof_function(ret_class, curce)) {
xmlFreeDoc(newdocp);
zend_argument_type_error(2, "must be a class name compatible with %s, \"%s\" given",
ZSTR_VAL(curclass_name), ZSTR_VAL(ret_class)
zend_argument_type_error(2, "must be a class name compatible with %s, %s given",
ZSTR_VAL(curclass_name), ZSTR_VAL(ret_class->name)
);
RETURN_THROWS();
}
object_init_ex(return_value, ce);
object_init_ex(return_value, ret_class);
interndoc = Z_LIBXML_NODE_P(return_value);
php_libxml_increment_doc_ref(interndoc, newdocp);
@@ -542,7 +541,6 @@ PHP_METHOD(XSLTProcessor, transformToDoc)
} else {
RETURN_FALSE;
}
}
/* }}} end XSLTProcessor::transformToDoc */