Skip to content

Commit bc7b51f

Browse files
committed
Promote warnings to exceptions in ext/simplexml
1 parent afde6dc commit bc7b51f

6 files changed

+62
-40
lines changed

ext/simplexml/simplexml.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ static zval *sxe_prop_dim_write(zend_object *object, zval *member, zval *value,
438438
}
439439

440440
if (!Z_STRLEN_P(member)) {
441-
php_error_docref(NULL, E_WARNING, "Cannot write or create unnamed %s", attribs ? "attribute" : "element");
441+
zend_value_error("Cannot create or write %s with an empty name", attribs ? "attributes" : "elements");
442442
if (member == &tmp_zv) {
443443
zval_ptr_dtor_str(&tmp_zv);
444444
}
@@ -466,7 +466,7 @@ static zval *sxe_prop_dim_write(zend_object *object, zval *member, zval *value,
466466
* and could also be E_PARSE, but we use this only during parsing
467467
* and this is during runtime.
468468
*/
469-
zend_throw_error(NULL, "Cannot create unnamed attribute");
469+
zend_value_error("Cannot create or write attributes with an empty name");
470470
return &EG(error_zval);
471471
}
472472
if (attribs && !node && sxe->iter.type == SXE_ITER_ELEMENT) {
@@ -508,7 +508,7 @@ static zval *sxe_prop_dim_write(zend_object *object, zval *member, zval *value,
508508
if (member == &tmp_zv) {
509509
zval_ptr_dtor_str(&tmp_zv);
510510
}
511-
zend_error(E_WARNING, "It is not yet possible to assign complex types to %s", attribs ? "attributes" : "properties");
511+
zend_type_error("It's not possible to assign a complex type to %s, %s given", attribs ? "attributes" : "properties", zend_zval_type_name(value));
512512
return &EG(error_zval);
513513
}
514514
}
@@ -1691,8 +1691,8 @@ SXE_METHOD(addChild)
16911691
}
16921692

16931693
if (qname_len == 0) {
1694-
php_error_docref(NULL, E_WARNING, "Element name is required");
1695-
return;
1694+
zend_argument_value_error(1, "cannot be empty");
1695+
RETURN_THROWS();
16961696
}
16971697

16981698
sxe = Z_SXEOBJ_P(ZEND_THIS);
@@ -1756,8 +1756,8 @@ SXE_METHOD(addAttribute)
17561756
}
17571757

17581758
if (qname_len == 0) {
1759-
php_error_docref(NULL, E_WARNING, "Attribute name is required");
1760-
return;
1759+
zend_argument_value_error(1, "cannot be empty");
1760+
RETURN_THROWS();
17611761
}
17621762

17631763
sxe = Z_SXEOBJ_P(ZEND_THIS);
@@ -2281,8 +2281,8 @@ PHP_FUNCTION(simplexml_load_file)
22812281
}
22822282

22832283
if (ZEND_LONG_EXCEEDS_INT(options)) {
2284-
php_error_docref(NULL, E_WARNING, "Invalid options");
2285-
RETURN_FALSE;
2284+
zend_argument_value_error(3, "is too large");
2285+
RETURN_THROWS();
22862286
}
22872287

22882288
docp = xmlReadFile(filename, NULL, (int)options);
@@ -2326,16 +2326,16 @@ PHP_FUNCTION(simplexml_load_string)
23262326
}
23272327

23282328
if (ZEND_SIZE_T_INT_OVFL(data_len)) {
2329-
php_error_docref(NULL, E_WARNING, "Data is too long");
2330-
RETURN_FALSE;
2329+
zend_argument_value_error(1, "is too long");
2330+
RETURN_THROWS();
23312331
}
23322332
if (ZEND_SIZE_T_INT_OVFL(ns_len)) {
2333-
php_error_docref(NULL, E_WARNING, "Namespace is too long");
2334-
RETURN_FALSE;
2333+
zend_argument_value_error(4, "is too long");
2334+
RETURN_THROWS();
23352335
}
23362336
if (ZEND_LONG_EXCEEDS_INT(options)) {
2337-
php_error_docref(NULL, E_WARNING, "Invalid options");
2338-
RETURN_FALSE;
2337+
zend_argument_value_error(3, "is too large");
2338+
RETURN_THROWS();
23392339
}
23402340

23412341
docp = xmlReadMemory(data, (int)data_len, NULL, NULL, (int)options);

ext/simplexml/tests/012.phpt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ EOF;
1414

1515
$sxe = simplexml_load_string($xml);
1616

17+
try {
18+
$sxe[""] = "warning";
19+
} catch (ValueError $exception) {
20+
echo $exception->getMessage() . "\n";
21+
}
1722

18-
$sxe[""] = "warning";
1923
$sxe["attr"] = "value";
2024

2125
echo $sxe->asXML();
@@ -24,19 +28,19 @@ $sxe["attr"] = "new value";
2428

2529
echo $sxe->asXML();
2630

27-
$sxe[] = "error";
31+
try {
32+
$sxe[] = "error";
33+
} catch (ValueError $exception) {
34+
echo $exception->getMessage() . "\n";
35+
}
2836

2937
__HALT_COMPILER();
3038
?>
3139
===DONE===
32-
--EXPECTF--
33-
Warning: main(): Cannot write or create unnamed attribute in %s012.php on line %d
40+
--EXPECT--
41+
Cannot create or write attributes with an empty name
3442
<?xml version="1.0" encoding="ISO-8859-1"?>
3543
<foo attr="value"/>
3644
<?xml version="1.0" encoding="ISO-8859-1"?>
3745
<foo attr="new value"/>
38-
39-
Fatal error: Uncaught Error: Cannot create unnamed attribute in %s012.php:%d
40-
Stack trace:
41-
#0 {main}
42-
thrown in %s012.php on line %d
46+
Cannot create or write attributes with an empty name

ext/simplexml/tests/SimpleXMLElement_addAttribute_required_attribute_name.phpt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ Havard Eide <[email protected]>
88
--FILE--
99
<?php
1010
$a = new SimpleXMLElement("<php>testfest</php>");
11-
$a->addAttribute( "", "" );
11+
12+
try {
13+
$a->addAttribute( "", "" );
14+
} catch (ValueError $exception) {
15+
echo $exception->getMessage() . "\n";
16+
}
17+
1218
echo $a->asXML();
1319
?>
14-
--EXPECTF--
15-
Warning: SimpleXMLElement::addAttribute(): Attribute name is required in %s on line %d
20+
--EXPECT--
21+
SimpleXMLElement::addAttribute(): Argument #1 ($qualifiedName) cannot be empty
1622
<?xml version="1.0"?>
1723
<php>testfest</php>

ext/simplexml/tests/SimpleXMLElement_xpath_4.phpt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platforms only");
77
?>
88
--FILE--
99
<?php
10-
$xml = simplexml_load_string("XXXXXXX^",$x,0x6000000000000001);
11-
var_dump($xml);
10+
11+
try {
12+
simplexml_load_string("XXXXXXX^", $x, 0x6000000000000001);
13+
} catch (ValueError $exception) {
14+
echo $exception->getMessage() . "\n";
15+
}
16+
1217
?>
1318
--EXPECTF--
1419
Warning: Undefined variable $x in %s on line %d
15-
16-
Warning: simplexml_load_string(): Invalid options in %s on line %d
17-
bool(false)
20+
simplexml_load_string(): Argument #3 ($options) is too large

ext/simplexml/tests/bug37076_1.phpt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@ Bug #37076 (SimpleXML ignores .=) (appending to unnamed attribute)
44
<?php if (!extension_loaded("simplexml")) print "skip"; ?>
55
--FILE--
66
<?php
7+
78
$xml = simplexml_load_string("<root><foo /></root>");
8-
$xml->{""} .= "bar";
9+
10+
try {
11+
$xml->{""} .= "bar";
12+
} catch (ValueError $exception) {
13+
echo $exception->getMessage() . "\n";
14+
}
15+
916
print $xml->asXML();
1017
?>
11-
--EXPECTF--
12-
Warning: main(): Cannot write or create unnamed element in %s on line %d
13-
14-
Warning: main(): Cannot write or create unnamed element in %s on line %d
18+
--EXPECT--
19+
Cannot create or write elements with an empty name
1520
<?xml version="1.0"?>
1621
<root><foo/></root>

ext/simplexml/tests/bug38406.phpt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ $item->otherAttribute = $item->attribute;
1313
var_dump($item->otherAttribute);
1414

1515
$a = array();
16-
$item->$a = new stdclass;
16+
17+
try {
18+
$item->$a = new stdclass;
19+
} catch (TypeError $exception) {
20+
echo $exception->getMessage() . "\n";
21+
}
1722

1823
echo "Done\n";
1924
?>
@@ -28,6 +33,5 @@ object(SimpleXMLElement)#%d (1) {
2833
}
2934

3035
Warning: Array to string conversion in %s on line %d
31-
32-
Warning: It is not yet possible to assign complex types to properties in %s on line %d
36+
It's not possible to assign a complex type to properties, stdClass given
3337
Done

0 commit comments

Comments
 (0)