Skip to content

Declare typed properties in ext/dom #7013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 28 additions & 31 deletions ext/dom/php_dom.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,47 +265,28 @@ PHP_DOM_EXPORT dom_object *php_dom_object_get_data(xmlNodePtr obj)
}
/* }}} end php_dom_object_get_data */

/* {{{ dom_read_na */
static int dom_read_na(dom_object *obj, zval *retval)
{
zend_throw_error(NULL, "Cannot read property");
return FAILURE;
}
/* }}} */

/* {{{ dom_write_na */
static int dom_write_na(dom_object *obj, zval *newval)
{
zend_throw_error(NULL, "Cannot write property");
return FAILURE;
}
/* }}} */

/* {{{ dom_register_prop_handler */
static void dom_register_prop_handler(HashTable *prop_handler, char *name, size_t name_len, dom_read_t read_func, dom_write_t write_func)
{
dom_prop_handler hnd;
zend_string *str;

hnd.read_func = read_func ? read_func : dom_read_na;
hnd.write_func = write_func ? write_func : dom_write_na;
hnd.read_func = read_func;
hnd.write_func = write_func;
str = zend_string_init_interned(name, name_len, 1);
zend_hash_add_mem(prop_handler, str, &hnd, sizeof(dom_prop_handler));
zend_string_release_ex(str, 1);
}
/* }}} */

static zval *dom_get_property_ptr_ptr(zend_object *object, zend_string *name, int type, void **cache_slot) /* {{{ */
static zval *dom_get_property_ptr_ptr(zend_object *object, zend_string *name, int type, void **cache_slot)
{
dom_object *obj = php_dom_obj_from_obj(object);
zval *retval = NULL;

if (!obj->prop_handler || !zend_hash_exists(obj->prop_handler, name)) {
retval = zend_std_get_property_ptr_ptr(object, name, type, cache_slot);
return zend_std_get_property_ptr_ptr(object, name, type, cache_slot);
}
return retval;

return NULL;
}
/* }}} */

/* {{{ dom_read_property */
zval *dom_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv)
Expand Down Expand Up @@ -337,7 +318,6 @@ zval *dom_read_property(zend_object *object, zend_string *name, int type, void *
}
/* }}} */

/* {{{ dom_write_property */
zval *dom_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot)
{
dom_object *obj = php_dom_obj_from_obj(object);
Expand All @@ -346,15 +326,32 @@ zval *dom_write_property(zend_object *object, zend_string *name, zval *value, vo
if (obj->prop_handler != NULL) {
hnd = zend_hash_find_ptr(obj->prop_handler, name);
}

if (hnd) {
hnd->write_func(obj, value);
} else {
value = zend_std_write_property(object, name, value, cache_slot);
if (!hnd->write_func) {
zend_throw_error(NULL, "Cannot write read-only property %s::$%s", ZSTR_VAL(object->ce->name), ZSTR_VAL(name));
return &EG(error_zval);
}

zend_property_info *prop = zend_get_property_info(object->ce, name, /* silent */ true);
if (prop && ZEND_TYPE_IS_SET(prop->type)) {
zval tmp;
ZVAL_COPY(&tmp, value);
if (!zend_verify_property_type(prop, &tmp, ZEND_CALL_USES_STRICT_TYPES(EG(current_execute_data)))) {
zval_ptr_dtor(&tmp);
return &EG(error_zval);
}
hnd->write_func(obj, &tmp);
zval_ptr_dtor(&tmp);
} else {
hnd->write_func(obj, value);
}

return value;
}

return value;
return zend_std_write_property(object, name, value, cache_slot);
}
/* }}} */

/* {{{ dom_property_exists */
static int dom_property_exists(zend_object *object, zend_string *name, int check_empty, void **cache_slot)
Expand Down
56 changes: 19 additions & 37 deletions ext/dom/php_dom.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ class DOMNode
/** @readonly */
public string $nodeName;

/** @var string|null */
public $nodeValue;
public ?string $nodeValue;

/** @readonly */
public int $nodeType;
Expand Down Expand Up @@ -94,17 +93,15 @@ class DOMNode
/** @readonly */
public ?string $namespaceURI;

/** @var string */
public $prefix = "";
public string $prefix;

/** @readonly */
public ?string $localName;

/** @readonly */
public ?string $baseURI;

/** @var string */
public $textContent = "";
public string $textContent;

/** @return DOMNode|false */
public function appendChild(DOMNode $node) {}
Expand Down Expand Up @@ -415,53 +412,40 @@ class DOMDocument extends DOMNode implements DOMParentNode
*/
public ?string $actualEncoding;

/** @var string|null */
public $encoding;
public ?string $encoding;

/** @readonly */
public ?string $xmlEncoding;

/** @var bool */
public $standalone = false;
public bool $standalone;

/** @var bool */
public $xmlStandalone = false;
public bool $xmlStandalone;

/** @var string|null */
public $version;
public ?string $version;

/** @var string|null */
public $xmlVersion;
public ?string $xmlVersion;

/** @var bool */
public $strictErrorChecking = false;
public bool $strictErrorChecking;

/** @var string|null */
public $documentURI;
public ?string $documentURI;

/**
* @readonly
* @deprecated
*/
public mixed $config = null;

/** @var bool */
public $formatOutput = false;
public bool $formatOutput;

/** @var bool */
public $validateOnParse = false;
public bool $validateOnParse;

/** @var bool */
public $resolveExternals = false;
public bool $resolveExternals;

/** @var bool */
public $preserveWhiteSpace = false;
public bool $preserveWhiteSpace;

/** @var bool */
public $recover = false;
public bool $recover;

/** @var bool */
public $substituteEntities = false;
public bool $substituteEntities;

/** @readonly */
public ?DOMElement $firstElementChild;
Expand Down Expand Up @@ -580,7 +564,7 @@ public function prepend(...$nodes): void {}

final class DOMException extends Exception
{
/** @var int */
/** @var int Intentionally left untyped */
public $code = 0;
}

Expand Down Expand Up @@ -664,8 +648,7 @@ class DOMProcessingInstruction extends DOMNode
/** @readonly */
public string $target;

/** @var string */
public $data = "";
public string $data;

public function __construct(string $name, string $value = "") {}
}
Expand All @@ -676,8 +659,7 @@ class DOMXPath
/** @readonly */
public DOMDocument $document;

/** @var bool */
public $registerNodeNamespaces = false;
public bool $registerNodeNamespaces;

public function __construct(DOMDocument $document, bool $registerNodeNS = true) {}

Expand Down
Loading