Skip to content

Commit 2a7f23e

Browse files
committed
Fix type error on XSLTProcessor::transformToDoc return value with SimpleXML
The return type is wrong. You can also use this method with SimpleXML. In fact, PHP provides a way that even third party libraries can hook into its XML handling. Therefore, we cannot even use the SimpleXML|DOMDocument|false union type as third party extensions may extend the possibilities. Broke in 8.1 in 1b35056. Closes GH-12315.
1 parent b5da98b commit 2a7f23e

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ PHP NEWS
5959
. Fix return type of stub of xml_parse_into_struct(). (nielsdos)
6060
. Fix memory leak when calling xml_parse_into_struct() twice. (nielsdos)
6161

62+
- XSL:
63+
. Fix type error on XSLTProcessor::transformToDoc return value with
64+
SimpleXML. (nielsdos)
65+
6266
- Sockets:
6367
. Fix socket_export_stream() with wrong protocol (twosee)
6468

ext/xsl/php_xsl.stub.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function importStylesheet(object $stylesheet): bool {}
1414
* @param DOMDocument|SimpleXMLElement $document
1515
* @tentative-return-type
1616
*/
17-
public function transformToDoc(object $document, ?string $returnClass = null): DOMDocument|false {}
17+
public function transformToDoc(object $document, ?string $returnClass = null): object|false {}
1818

1919
/**
2020
* @param DOMDocument|SimpleXMLElement $document

ext/xsl/php_xsl_arginfo.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 7c920913c15c9cd663f19f7ec5ad81648d6eddbc */
2+
* Stub hash: 234923f47c0d9e83ba87d765fa7c1c2ea8d9f9b1 */
33

44
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_XSLTProcessor_importStylesheet, 0, 1, _IS_BOOL, 0)
55
ZEND_ARG_TYPE_INFO(0, stylesheet, IS_OBJECT, 0)
66
ZEND_END_ARG_INFO()
77

8-
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_XSLTProcessor_transformToDoc, 0, 1, DOMDocument, MAY_BE_FALSE)
8+
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_MASK_EX(arginfo_class_XSLTProcessor_transformToDoc, 0, 1, MAY_BE_OBJECT|MAY_BE_FALSE)
99
ZEND_ARG_TYPE_INFO(0, document, IS_OBJECT, 0)
1010
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, returnClass, IS_STRING, 1, "null")
1111
ZEND_END_ARG_INFO()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
--TEST--
2+
XSLTProcessor::transformToDoc return value type error with SimpleXML
3+
--EXTENSIONS--
4+
xsl
5+
simplexml
6+
--FILE--
7+
<?php
8+
9+
class AdvancedXMLElement extends SimpleXMLElement {
10+
public function foo() {
11+
return "foo: " . (string) $this;
12+
}
13+
}
14+
15+
$sxe = simplexml_load_file(__DIR__ . '/53965/collection.xml', AdvancedXMLElement::class);
16+
17+
$processor = new XSLTProcessor;
18+
$dom = new DOMDocument;
19+
$dom->load(__DIR__ . '/53965/collection.xsl');
20+
$processor->importStylesheet($dom);
21+
$result = $processor->transformToDoc($sxe, AdvancedXMLElement::class);
22+
23+
var_dump($result);
24+
var_dump($result->h1->foo());
25+
26+
?>
27+
--EXPECT--
28+
object(AdvancedXMLElement)#4 (3) {
29+
["h1"]=>
30+
array(2) {
31+
[0]=>
32+
string(19) "Fight for your mind"
33+
[1]=>
34+
string(17) "Electric Ladyland"
35+
}
36+
["h2"]=>
37+
array(2) {
38+
[0]=>
39+
string(20) "by Ben Harper - 1995"
40+
[1]=>
41+
string(22) "by Jimi Hendrix - 1997"
42+
}
43+
["hr"]=>
44+
array(2) {
45+
[0]=>
46+
object(AdvancedXMLElement)#5 (0) {
47+
}
48+
[1]=>
49+
object(AdvancedXMLElement)#6 (0) {
50+
}
51+
}
52+
}
53+
string(24) "foo: Fight for your mind"

0 commit comments

Comments
 (0)