Skip to content

Commit f88368c

Browse files
authored
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.
1 parent 41ea14b commit f88368c

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
XSLTProcessor::transformToDoc class exceptions
3+
--EXTENSIONS--
4+
xsl
5+
simplexml
6+
dom
7+
--FILE--
8+
<?php
9+
10+
$sxe = simplexml_load_file(__DIR__ . '/53965/collection.xml');
11+
12+
$processor = new XSLTProcessor;
13+
$dom = new DOMDocument;
14+
$dom->load(__DIR__ . '/53965/collection.xsl');
15+
$processor->importStylesheet($dom);
16+
17+
try {
18+
$processor->transformToDoc($sxe, NonExistent::class);
19+
} catch (TypeError $e) {
20+
echo $e->getMessage(), "\n";
21+
}
22+
23+
try {
24+
$processor->transformToDoc($sxe, DOMDocument::class);
25+
} catch (TypeError $e) {
26+
echo $e->getMessage(), "\n";
27+
}
28+
29+
?>
30+
--EXPECT--
31+
XSLTProcessor::transformToDoc(): Argument #2 ($returnClass) must be a valid class name or null, NonExistent given
32+
XSLTProcessor::transformToDoc(): Argument #2 ($returnClass) must be a class name compatible with SimpleXMLElement, DOMDocument given

ext/xsl/xsltprocessor.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -497,14 +497,14 @@ PHP_METHOD(XSLTProcessor, transformToDoc)
497497
zval *id, *docp = NULL;
498498
xmlDoc *newdocp;
499499
xsltStylesheetPtr sheetp;
500-
zend_string *ret_class = NULL;
500+
zend_class_entry *ret_class = NULL;
501501
xsl_object *intern;
502502

503503
id = ZEND_THIS;
504504
intern = Z_XSL_P(id);
505505
sheetp = (xsltStylesheetPtr) intern->ptr;
506506

507-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "o|S!", &docp, &ret_class) == FAILURE) {
507+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "o|C!", &docp, &ret_class) == FAILURE) {
508508
RETURN_THROWS();
509509
}
510510

@@ -513,7 +513,7 @@ PHP_METHOD(XSLTProcessor, transformToDoc)
513513
if (newdocp) {
514514
if (ret_class) {
515515
zend_string *curclass_name;
516-
zend_class_entry *curce, *ce;
516+
zend_class_entry *curce;
517517
php_libxml_node_object *interndoc;
518518

519519
curce = Z_OBJCE_P(docp);
@@ -522,16 +522,15 @@ PHP_METHOD(XSLTProcessor, transformToDoc)
522522
curce = curce->parent;
523523
}
524524

525-
ce = zend_lookup_class(ret_class);
526-
if (ce == NULL || !instanceof_function(ce, curce)) {
525+
if (!instanceof_function(ret_class, curce)) {
527526
xmlFreeDoc(newdocp);
528-
zend_argument_type_error(2, "must be a class name compatible with %s, \"%s\" given",
529-
ZSTR_VAL(curclass_name), ZSTR_VAL(ret_class)
527+
zend_argument_type_error(2, "must be a class name compatible with %s, %s given",
528+
ZSTR_VAL(curclass_name), ZSTR_VAL(ret_class->name)
530529
);
531530
RETURN_THROWS();
532531
}
533532

534-
object_init_ex(return_value, ce);
533+
object_init_ex(return_value, ret_class);
535534

536535
interndoc = Z_LIBXML_NODE_P(return_value);
537536
php_libxml_increment_doc_ref(interndoc, newdocp);
@@ -542,7 +541,6 @@ PHP_METHOD(XSLTProcessor, transformToDoc)
542541
} else {
543542
RETURN_FALSE;
544543
}
545-
546544
}
547545
/* }}} end XSLTProcessor::transformToDoc */
548546

0 commit comments

Comments
 (0)