Skip to content

Commit 25b4696

Browse files
authored
ext/xml: Deprecate xml_set_object() and passing non-callable strings as handlers (#15293)
1 parent 6eca783 commit 25b4696

16 files changed

+102
-69
lines changed

NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ PHP NEWS
5555
. Implemented GH-15155 (Stream context is lost when custom stream wrapper is
5656
being filtered). (Quentin Dreyer)
5757

58+
- XML:
59+
. The xml_set_object() function has been deprecated. (Girgias)
60+
. Passing non-callable strings to the xml_set_*_handler() functions is now
61+
deprecated. (Girgias)
62+
5863
01 Aug 2024, PHP 8.4.0alpha4
5964

6065
- GMP:

UPGRADING

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ PHP 8.4 UPGRADE NOTES
189189
This means that xml_set_object() must now always be called prior to setting
190190
method names as callables.
191191
Passing an empty string to disable the handler is still allowed,
192-
but not recommended.
192+
but deprecated.
193193

194194
- XMLReader:
195195
. Passing an invalid character encoding to XMLReader::open() or
@@ -478,6 +478,13 @@ PHP 8.4 UPGRADE NOTES
478478
. Unserializing strings using the uppercase 'S' tag is deprecated.
479479
RFC: https://wiki.php.net/rfc/deprecations_php_8_4
480480

481+
- XML:
482+
. The xml_set_object() function has been deprecated.
483+
RFC: https://wiki.php.net/rfc/deprecations_php_8_4#xml_set_object_and_xml_set_handler_with_string_method_names
484+
. Passing non-callable strings to the xml_set_*_handler() functions is now
485+
deprecated.
486+
RFC: https://wiki.php.net/rfc/deprecations_php_8_4#xml_set_object_and_xml_set_handler_with_string_method_names
487+
481488
========================================
482489
5. Changed Functions
483490
========================================

Zend/tests/bug34617.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ function boom()
1515
}
1616
boom();
1717
?>
18-
--EXPECT--
18+
--EXPECTF--
19+
Deprecated: Function xml_set_object() is deprecated since 8.4, provide a proper method callable to xml_set_*_handler() functions in %s on line %d
1920
ok

ext/xml/tests/bug30266.phpt

Lines changed: 0 additions & 51 deletions
This file was deleted.

ext/xml/tests/bug32001.phpt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ HERE;
100100

101101
$parser = xml_parser_create(NULL);
102102
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
103-
xml_set_object($parser, $this);
104-
xml_set_element_handler($parser, "start_element", "end_element");
103+
xml_set_element_handler($parser, $this->start_element(...), $this->end_element(...));
105104

106105
if ($this->chunk_size == 0) {
107106
$success = @xml_parse($parser, $data, true);

ext/xml/tests/bug72793.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ $xml_parser->free();
3131

3232
?>
3333
===DONE===
34-
--EXPECT--
34+
--EXPECTF--
35+
Deprecated: Function xml_set_object() is deprecated since 8.4, provide a proper method callable to xml_set_*_handler() functions in %s on line %d
3536
===DONE===

ext/xml/tests/set_element_handler_trampoline.phpt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ xml_parse($parser, $xml, true);
5959
xml_parser_free($parser);
6060

6161
?>
62-
--EXPECT--
62+
--EXPECTF--
6363
Both handlers are trampolines:
6464
Trampoline for start_handler
6565
Tag: A
@@ -75,6 +75,10 @@ Trampoline for end_handler
7575
Tag: A
7676

7777
Start handler is trampoline, end handler method string:
78+
79+
Deprecated: Function xml_set_object() is deprecated since 8.4, provide a proper method callable to xml_set_*_handler() functions in %s on line %d
80+
81+
Deprecated: xml_set_element_handler(): Passing non-callable strings is deprecated since 8.4 in %s on line %d
7882
Trampoline for start_handler
7983
Tag: A
8084
Trampoline for start_handler
@@ -86,6 +90,10 @@ Method end handler: C
8690
Method end handler: A
8791

8892
End handler is trampoline, start handler method string:
93+
94+
Deprecated: Function xml_set_object() is deprecated since 8.4, provide a proper method callable to xml_set_*_handler() functions in %s on line %d
95+
96+
Deprecated: xml_set_element_handler(): Passing non-callable strings is deprecated since 8.4 in %s on line %d
8997
Method start handler: A
9098
Method start handler: B
9199
Trampoline for end_handler

ext/xml/tests/set_handler_errors.phpt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,20 @@ try {
4848
}
4949

5050
?>
51-
--EXPECT--
51+
--EXPECTF--
5252
Invalid $parser:
5353
TypeError: xml_set_processing_instruction_handler(): Argument #1 ($parser) must be of type XMLParser, stdClass given
5454
Invalid callable type true:
5555
TypeError: xml_set_processing_instruction_handler(): Argument #2 ($handler) must be of type callable|string|null
5656
Invalid callable type int:
5757
TypeError: xml_set_processing_instruction_handler(): Argument #2 ($handler) must be of type callable|string|null
5858
String not callable and no object set:
59+
60+
Deprecated: xml_set_processing_instruction_handler(): Passing non-callable strings is deprecated since 8.4 in %s on line %d
5961
ValueError: xml_set_processing_instruction_handler(): Argument #2 ($handler) an object must be set via xml_set_object() to be able to lookup method
6062
String non existent method on set object:
63+
64+
Deprecated: Function xml_set_object() is deprecated since 8.4, provide a proper method callable to xml_set_*_handler() functions in %s on line %d
65+
66+
Deprecated: xml_set_processing_instruction_handler(): Passing non-callable strings is deprecated since 8.4 in %s on line %d
6167
ValueError: xml_set_processing_instruction_handler(): Argument #2 ($handler) method stdClass::nonexistent_method() does not exist

ext/xml/tests/xml_set_element_handler_errors.phpt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ try {
7474
}
7575

7676
?>
77-
--EXPECT--
77+
--EXPECTF--
7878
Invalid $parser:
7979
TypeError: xml_set_element_handler(): Argument #1 ($parser) must be of type XMLParser, stdClass given
8080
Invalid start callable type true:
@@ -86,10 +86,22 @@ TypeError: xml_set_element_handler(): Argument #2 ($start_handler) must be of ty
8686
Invalid end callable type int:
8787
TypeError: xml_set_element_handler(): Argument #3 ($end_handler) must be of type callable|string|null
8888
Invalid start callable, no object set and string not callable:
89+
90+
Deprecated: xml_set_element_handler(): Passing non-callable strings is deprecated since 8.4 in %s on line %d
8991
ValueError: xml_set_element_handler(): Argument #2 ($start_handler) an object must be set via xml_set_object() to be able to lookup method
9092
Invalid end callable, no object set and string not callable:
93+
94+
Deprecated: xml_set_element_handler(): Passing non-callable strings is deprecated since 8.4 in %s on line %d
9195
ValueError: xml_set_element_handler(): Argument #3 ($end_handler) an object must be set via xml_set_object() to be able to lookup method
9296
Invalid start callable, string non existent method on set object:
97+
98+
Deprecated: Function xml_set_object() is deprecated since 8.4, provide a proper method callable to xml_set_*_handler() functions in %s on line %d
99+
100+
Deprecated: xml_set_element_handler(): Passing non-callable strings is deprecated since 8.4 in %s on line %d
93101
ValueError: xml_set_element_handler(): Argument #2 ($start_handler) method stdClass::nonexistent_method() does not exist
94102
Invalid end callable, string non existent method on set object:
103+
104+
Deprecated: Function xml_set_object() is deprecated since 8.4, provide a proper method callable to xml_set_*_handler() functions in %s on line %d
105+
106+
Deprecated: xml_set_element_handler(): Passing non-callable strings is deprecated since 8.4 in %s on line %d
95107
ValueError: xml_set_element_handler(): Argument #3 ($end_handler) method stdClass::nonexistent_method() does not exist

ext/xml/tests/xml_set_notation_decl_handler_basic.phpt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ class XML_Parser
2929
function parse($data)
3030
{
3131
$parser = xml_parser_create();
32-
xml_set_object($parser, $this);
33-
xml_set_notation_decl_handler($parser, "notation_decl_handler");
34-
xml_set_unparsed_entity_decl_handler($parser, "unparsed_entity_decl_handler");
32+
xml_set_notation_decl_handler($parser, $this->notation_decl_handler(...));
33+
xml_set_unparsed_entity_decl_handler($parser, $this->unparsed_entity_decl_handler(...));
3534
xml_parse($parser, $data, true);
3635
xml_parser_free($parser);
3736
}

ext/xml/tests/xml_set_object_multiple_times.phpt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ xml_parse($parser, <<<XML
4848
XML);
4949

5050
?>
51-
--EXPECT--
51+
--EXPECTF--
52+
Deprecated: Function xml_set_object() is deprecated since 8.4, provide a proper method callable to xml_set_*_handler() functions in %s on line %d
53+
54+
Deprecated: xml_set_element_handler(): Passing non-callable strings is deprecated since 8.4 in %s on line %d
55+
56+
Deprecated: Function xml_set_object() is deprecated since 8.4, provide a proper method callable to xml_set_*_handler() functions in %s on line %d
5257
A::start_element(CONTAINER)
5358
B::start_element(CHILD)
5459
end_handler(CHILD)

ext/xml/tests/xml_set_object_multiple_times_errors.phpt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ xml_parse($parser, <<<XML
4141
XML);
4242

4343
?>
44-
--EXPECT--
44+
--EXPECTF--
45+
Deprecated: Function xml_set_object() is deprecated since 8.4, provide a proper method callable to xml_set_*_handler() functions in %s on line %d
46+
47+
Deprecated: xml_set_element_handler(): Passing non-callable strings is deprecated since 8.4 in %s on line %d
4548
A::start_element(CONTAINER)
49+
50+
Deprecated: Function xml_set_object() is deprecated since 8.4, provide a proper method callable to xml_set_*_handler() functions in %s on line %d
4651
ValueError: xml_set_object(): Argument #2 ($object) cannot safely swap to object of class B as method "end_element" does not exist, which was set via xml_set_element_handler()

ext/xml/tests/xml_set_processing_instruction_handler_basic.phpt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ class XML_Parser
1616
function parse($data)
1717
{
1818
$parser = xml_parser_create();
19-
xml_set_object($parser, $this);
20-
xml_set_processing_instruction_handler($parser, "PIHandler");
19+
xml_set_processing_instruction_handler($parser, $this->PIHandler(...));
2120
xml_parse($parser, $data, true);
2221
xml_parser_free($parser);
2322
}

ext/xml/xml.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "php.h"
2424

2525
#include "zend_variables.h"
26+
#include "zend_attributes.h"
2627
#include "ext/standard/info.h"
2728
#include "ext/standard/html.h" /* For php_next_utf8_char() */
2829

@@ -1184,6 +1185,13 @@ PHP_FUNCTION(xml_set_element_handler)
11841185
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "OF!S", &pind, xml_parser_ce, &start_fci, &start_fcc, &end_method_name) == SUCCESS) {
11851186
parser = Z_XMLPARSER_P(pind);
11861187

1188+
php_error_docref(NULL, E_DEPRECATED, "Passing non-callable strings is deprecated since 8.4");
1189+
if (UNEXPECTED(EG(exception))) {
1190+
zend_release_fcall_info_cache(&start_fcc);
1191+
zend_release_fcall_info_cache(&end_fcc);
1192+
RETURN_THROWS();
1193+
}
1194+
11871195
bool status = php_xml_check_string_method_arg(3, parser->object, end_method_name, &end_fcc);
11881196
if (status == false) {
11891197
zend_release_fcall_info_cache(&start_fcc);
@@ -1193,6 +1201,13 @@ PHP_FUNCTION(xml_set_element_handler)
11931201
} else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "OSF!", &pind, xml_parser_ce, &start_method_name, &end_fci, &end_fcc) == SUCCESS) {
11941202
parser = Z_XMLPARSER_P(pind);
11951203

1204+
php_error_docref(NULL, E_DEPRECATED, "Passing non-callable strings is deprecated since 8.4");
1205+
if (UNEXPECTED(EG(exception))) {
1206+
zend_release_fcall_info_cache(&start_fcc);
1207+
zend_release_fcall_info_cache(&end_fcc);
1208+
RETURN_THROWS();
1209+
}
1210+
11961211
bool status = php_xml_check_string_method_arg(2, parser->object, start_method_name, &start_fcc);
11971212
if (status == false) {
11981213
zend_release_fcall_info_cache(&start_fcc);
@@ -1203,6 +1218,11 @@ PHP_FUNCTION(xml_set_element_handler)
12031218
zend_release_fcall_info_cache(&start_fcc);
12041219
zend_release_fcall_info_cache(&end_fcc);
12051220

1221+
php_error_docref(NULL, E_DEPRECATED, "Passing non-callable strings is deprecated since 8.4");
1222+
if (UNEXPECTED(EG(exception))) {
1223+
RETURN_THROWS();
1224+
}
1225+
12061226
parser = Z_XMLPARSER_P(pind);
12071227

12081228
bool status = php_xml_check_string_method_arg(2, parser->object, start_method_name, &start_fcc);
@@ -1263,7 +1283,10 @@ static void php_xml_set_handler_parse_callable(
12631283
memcpy(parser_handler_fcc, &handler_fcc, sizeof(zend_fcall_info_cache));
12641284
} else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "OS", &pind, xml_parser_ce, &method_name) == SUCCESS) {
12651285
*parser = Z_XMLPARSER_P(pind);
1266-
1286+
php_error_docref(NULL, E_DEPRECATED, "Passing non-callable strings is deprecated since 8.4");
1287+
if (UNEXPECTED(EG(exception))) {
1288+
RETURN_THROWS();
1289+
}
12671290
bool status = php_xml_check_string_method_arg(2, (*parser)->object, method_name, parser_handler_fcc);
12681291
if (status == false) {
12691292
RETURN_THROWS();

ext/xml/xml.stub.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ function xml_parser_create(?string $encoding = null): XMLParser {}
149149

150150
function xml_parser_create_ns(?string $encoding = null, string $separator = ":"): XMLParser {}
151151

152+
#[\Deprecated(since: '8.4', message: 'provide a proper method callable to xml_set_*_handler() functions')]
152153
function xml_set_object(XMLParser $parser, object $object): true {}
153154

154155
function xml_set_element_handler(XMLParser $parser, callable|string|null $start_handler, callable|string|null $end_handler): true {}

ext/xml/xml_arginfo.h

Lines changed: 15 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)