Skip to content

Commit ae66a0d

Browse files
committed
Corrections to return type of loading DOM documents
1 parent b389846 commit ae66a0d

File tree

4 files changed

+77
-89
lines changed

4 files changed

+77
-89
lines changed

UPGRADING

+5
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ PHP 8.3 UPGRADE NOTES
202202

203203
- Dom:
204204
. Changed DOMCharacterData::appendData() tentative return type to true.
205+
. DOMDocument::loadHTML(), DOMDocument::loadHTMLFile(), DOMDocument::loadXML(),
206+
and DOMDocument::loadXMLFile() now have a tentative return type of bool.
207+
Previously, this was documented as having a return type of DOMDocument|bool,
208+
but DOMDocument cannot be returned since PHP 8.0 as it is no longer statically
209+
callable.
205210

206211
- Gd:
207212
. Changed imagerotate signature, removed the `ignore_transparent` argument

ext/dom/document.c

+59-76
Original file line numberDiff line numberDiff line change
@@ -1318,20 +1318,14 @@ static xmlDocPtr dom_document_parser(zval *id, int mode, char *source, size_t so
13181318

13191319
/* {{{ static void dom_parse_document(INTERNAL_FUNCTION_PARAMETERS, int mode) */
13201320
static void dom_parse_document(INTERNAL_FUNCTION_PARAMETERS, int mode) {
1321-
zval *id;
13221321
xmlDoc *docp = NULL, *newdoc;
13231322
dom_doc_propsptr doc_prop;
13241323
dom_object *intern;
13251324
char *source;
13261325
size_t source_len;
1327-
int refcount, ret;
1326+
int refcount;
13281327
zend_long options = 0;
13291328

1330-
id = getThis();
1331-
if (id != NULL && ! instanceof_function(Z_OBJCE_P(id), dom_document_class_entry)) {
1332-
id = NULL;
1333-
}
1334-
13351329
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &source, &source_len, &options) == FAILURE) {
13361330
RETURN_THROWS();
13371331
}
@@ -1349,48 +1343,44 @@ static void dom_parse_document(INTERNAL_FUNCTION_PARAMETERS, int mode) {
13491343
RETURN_FALSE;
13501344
}
13511345

1352-
newdoc = dom_document_parser(id, mode, source, source_len, options);
1346+
newdoc = dom_document_parser(ZEND_THIS, mode, source, source_len, options);
13531347

13541348
if (!newdoc)
13551349
RETURN_FALSE;
13561350

1357-
if (id != NULL) {
1358-
intern = Z_DOMOBJ_P(id);
1359-
size_t old_modification_nr = 0;
1360-
if (intern != NULL) {
1361-
docp = (xmlDocPtr) dom_object_get_node(intern);
1362-
doc_prop = NULL;
1363-
if (docp != NULL) {
1364-
const php_libxml_doc_ptr *doc_ptr = docp->_private;
1365-
ZEND_ASSERT(doc_ptr != NULL); /* Must exist, we have a document */
1366-
old_modification_nr = doc_ptr->cache_tag.modification_nr;
1367-
php_libxml_decrement_node_ptr((php_libxml_node_object *) intern);
1368-
doc_prop = intern->document->doc_props;
1369-
intern->document->doc_props = NULL;
1370-
refcount = php_libxml_decrement_doc_ref((php_libxml_node_object *)intern);
1371-
if (refcount != 0) {
1372-
docp->_private = NULL;
1373-
}
1374-
}
1375-
intern->document = NULL;
1376-
if (php_libxml_increment_doc_ref((php_libxml_node_object *)intern, newdoc) == -1) {
1377-
RETURN_FALSE;
1351+
intern = Z_DOMOBJ_P(ZEND_THIS);
1352+
size_t old_modification_nr = 0;
1353+
if (intern != NULL) {
1354+
docp = (xmlDocPtr) dom_object_get_node(intern);
1355+
doc_prop = NULL;
1356+
if (docp != NULL) {
1357+
const php_libxml_doc_ptr *doc_ptr = docp->_private;
1358+
ZEND_ASSERT(doc_ptr != NULL); /* Must exist, we have a document */
1359+
old_modification_nr = doc_ptr->cache_tag.modification_nr;
1360+
php_libxml_decrement_node_ptr((php_libxml_node_object *) intern);
1361+
doc_prop = intern->document->doc_props;
1362+
intern->document->doc_props = NULL;
1363+
refcount = php_libxml_decrement_doc_ref((php_libxml_node_object *)intern);
1364+
if (refcount != 0) {
1365+
docp->_private = NULL;
13781366
}
1379-
intern->document->doc_props = doc_prop;
13801367
}
1381-
1382-
php_libxml_increment_node_ptr((php_libxml_node_object *)intern, (xmlNodePtr)newdoc, (void *)intern);
1383-
/* Since iterators should invalidate, we need to start the modification number from the old counter */
1384-
if (old_modification_nr != 0) {
1385-
php_libxml_doc_ptr* doc_ptr = (php_libxml_doc_ptr*) ((php_libxml_node_object*) intern)->node; /* downcast */
1386-
doc_ptr->cache_tag.modification_nr = old_modification_nr;
1387-
php_libxml_invalidate_node_list_cache(doc_ptr);
1368+
intern->document = NULL;
1369+
if (php_libxml_increment_doc_ref((php_libxml_node_object *)intern, newdoc) == -1) {
1370+
RETURN_FALSE;
13881371
}
1372+
intern->document->doc_props = doc_prop;
1373+
}
13891374

1390-
RETURN_TRUE;
1391-
} else {
1392-
DOM_RET_OBJ((xmlNodePtr) newdoc, &ret, NULL);
1375+
php_libxml_increment_node_ptr((php_libxml_node_object *)intern, (xmlNodePtr)newdoc, (void *)intern);
1376+
/* Since iterators should invalidate, we need to start the modification number from the old counter */
1377+
if (old_modification_nr != 0) {
1378+
php_libxml_doc_ptr* doc_ptr = (php_libxml_doc_ptr*) ((php_libxml_node_object*) intern)->node; /* downcast */
1379+
doc_ptr->cache_tag.modification_nr = old_modification_nr;
1380+
php_libxml_invalidate_node_list_cache(doc_ptr);
13931381
}
1382+
1383+
RETURN_TRUE;
13941384
}
13951385
/* }}} end dom_parser_document */
13961386

@@ -1865,18 +1855,15 @@ PHP_METHOD(DOMDocument, relaxNGValidateSource)
18651855

18661856
static void dom_load_html(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
18671857
{
1868-
zval *id;
18691858
xmlDoc *docp = NULL, *newdoc;
18701859
dom_object *intern;
18711860
dom_doc_propsptr doc_prop;
18721861
char *source;
18731862
size_t source_len;
1874-
int refcount, ret;
1863+
int refcount;
18751864
zend_long options = 0;
18761865
htmlParserCtxtPtr ctxt;
18771866

1878-
id = getThis();
1879-
18801867
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &source, &source_len, &options) == FAILURE) {
18811868
RETURN_THROWS();
18821869
}
@@ -1926,43 +1913,39 @@ static void dom_load_html(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
19261913
if (!newdoc)
19271914
RETURN_FALSE;
19281915

1929-
if (id != NULL && instanceof_function(Z_OBJCE_P(id), dom_document_class_entry)) {
1930-
intern = Z_DOMOBJ_P(id);
1931-
size_t old_modification_nr = 0;
1932-
if (intern != NULL) {
1933-
docp = (xmlDocPtr) dom_object_get_node(intern);
1934-
doc_prop = NULL;
1935-
if (docp != NULL) {
1936-
const php_libxml_doc_ptr *doc_ptr = docp->_private;
1937-
ZEND_ASSERT(doc_ptr != NULL); /* Must exist, we have a document */
1938-
old_modification_nr = doc_ptr->cache_tag.modification_nr;
1939-
php_libxml_decrement_node_ptr((php_libxml_node_object *) intern);
1940-
doc_prop = intern->document->doc_props;
1941-
intern->document->doc_props = NULL;
1942-
refcount = php_libxml_decrement_doc_ref((php_libxml_node_object *)intern);
1943-
if (refcount != 0) {
1944-
docp->_private = NULL;
1945-
}
1946-
}
1947-
intern->document = NULL;
1948-
if (php_libxml_increment_doc_ref((php_libxml_node_object *)intern, newdoc) == -1) {
1949-
RETURN_FALSE;
1916+
intern = Z_DOMOBJ_P(ZEND_THIS);
1917+
size_t old_modification_nr = 0;
1918+
if (intern != NULL) {
1919+
docp = (xmlDocPtr) dom_object_get_node(intern);
1920+
doc_prop = NULL;
1921+
if (docp != NULL) {
1922+
const php_libxml_doc_ptr *doc_ptr = docp->_private;
1923+
ZEND_ASSERT(doc_ptr != NULL); /* Must exist, we have a document */
1924+
old_modification_nr = doc_ptr->cache_tag.modification_nr;
1925+
php_libxml_decrement_node_ptr((php_libxml_node_object *) intern);
1926+
doc_prop = intern->document->doc_props;
1927+
intern->document->doc_props = NULL;
1928+
refcount = php_libxml_decrement_doc_ref((php_libxml_node_object *)intern);
1929+
if (refcount != 0) {
1930+
docp->_private = NULL;
19501931
}
1951-
intern->document->doc_props = doc_prop;
19521932
}
1953-
1954-
php_libxml_increment_node_ptr((php_libxml_node_object *)intern, (xmlNodePtr)newdoc, (void *)intern);
1955-
/* Since iterators should invalidate, we need to start the modification number from the old counter */
1956-
if (old_modification_nr != 0) {
1957-
php_libxml_doc_ptr* doc_ptr = (php_libxml_doc_ptr*) ((php_libxml_node_object*) intern)->node; /* downcast */
1958-
doc_ptr->cache_tag.modification_nr = old_modification_nr;
1959-
php_libxml_invalidate_node_list_cache(doc_ptr);
1933+
intern->document = NULL;
1934+
if (php_libxml_increment_doc_ref((php_libxml_node_object *)intern, newdoc) == -1) {
1935+
RETURN_FALSE;
19601936
}
1937+
intern->document->doc_props = doc_prop;
1938+
}
19611939

1962-
RETURN_TRUE;
1963-
} else {
1964-
DOM_RET_OBJ((xmlNodePtr) newdoc, &ret, NULL);
1940+
php_libxml_increment_node_ptr((php_libxml_node_object *)intern, (xmlNodePtr)newdoc, (void *)intern);
1941+
/* Since iterators should invalidate, we need to start the modification number from the old counter */
1942+
if (old_modification_nr != 0) {
1943+
php_libxml_doc_ptr* doc_ptr = (php_libxml_doc_ptr*) ((php_libxml_node_object*) intern)->node; /* downcast */
1944+
doc_ptr->cache_tag.modification_nr = old_modification_nr;
1945+
php_libxml_invalidate_node_list_cache(doc_ptr);
19651946
}
1947+
1948+
RETURN_TRUE;
19661949
}
19671950
/* }}} */
19681951

ext/dom/php_dom.stub.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -774,11 +774,11 @@ public function getElementsByTagNameNS(?string $namespace, string $localName): D
774774
/** @return DOMNode|false */
775775
public function importNode(DOMNode $node, bool $deep = false) {}
776776

777-
/** @return DOMDocument|bool */
778-
public function load(string $filename, int $options = 0) {} // TODO return type shouldn't depend on the call scope
777+
/** @tentative-return-type */
778+
public function load(string $filename, int $options = 0): bool {}
779779

780-
/** @return DOMDocument|bool */
781-
public function loadXML(string $source, int $options = 0) {} // TODO return type shouldn't depend on the call scope
780+
/** @tentative-return-type */
781+
public function loadXML(string $source, int $options = 0): bool {}
782782

783783
/** @tentative-return-type */
784784
public function normalizeDocument(): void {}
@@ -790,11 +790,11 @@ public function registerNodeClass(string $baseClass, ?string $extendedClass): bo
790790
public function save(string $filename, int $options = 0): int|false {}
791791

792792
#ifdef LIBXML_HTML_ENABLED
793-
/** @return DOMDocument|bool */
794-
public function loadHTML(string $source, int $options = 0) {} // TODO return type shouldn't depend on the call scope
793+
/** @tentative-return-type */
794+
public function loadHTML(string $source, int $options = 0): bool {}
795795

796-
/** @return DOMDocument|bool */
797-
public function loadHTMLFile(string $filename, int $options = 0) {} // TODO return type shouldn't depend on the call scope
796+
/** @tentative-return-type */
797+
public function loadHTMLFile(string $filename, int $options = 0): bool {}
798798

799799
/** @tentative-return-type */
800800
public function saveHTML(?DOMNode $node = null): string|false {}

ext/dom/php_dom_arginfo.h

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)