Skip to content

Use typed properties in ext/zip #6881

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 3 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
25 changes: 21 additions & 4 deletions ext/zip/php_zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -859,10 +859,6 @@ static zval *php_zip_property_reader(ze_zip_object *obj, zip_prop_handler *hnd,
ZVAL_EMPTY_STRING(rv);
}
break;
/* case IS_TRUE */
case IS_FALSE:
ZVAL_BOOL(rv, retint);
break;
case IS_LONG:
ZVAL_LONG(rv, retint);
break;
Expand Down Expand Up @@ -894,6 +890,26 @@ static zval *php_zip_get_property_ptr_ptr(zend_object *object, zend_string *name
}
/* }}} */


static zval *php_zip_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot)
{
ze_zip_object *obj;
zip_prop_handler *hnd = NULL;

obj = php_zip_fetch_object(object);

if (obj->prop_handler != NULL) {
hnd = zend_hash_find_ptr(obj->prop_handler, name);
}

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

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

static zval *php_zip_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv) /* {{{ */
{
ze_zip_object *obj;
Expand Down Expand Up @@ -3029,6 +3045,7 @@ static PHP_MINIT_FUNCTION(zip)
zip_object_handlers.get_properties = php_zip_get_properties;
zip_object_handlers.read_property = php_zip_read_property;
zip_object_handlers.has_property = php_zip_has_property;
zip_object_handlers.write_property = php_zip_write_property;

zip_class_entry = register_class_ZipArchive(zend_ce_countable);
zip_class_entry->create_object = php_zip_object_new;
Expand Down
29 changes: 12 additions & 17 deletions ext/zip/php_zip.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,18 @@ function zip_entry_compressionmethod($zip_entry): string|false {}

class ZipArchive implements Countable
{
/** @var int|null */
public $lastId;

/** @var int|null */
public $status;

/** @var int|null */
public $statusSys;

/** @var int|null */
public $numFiles;

/** @var string|null */
public $filename;

/** @var string|null */
public $comment;
/** @readonly */
public int $lastId;
/** @readonly */
public int $status;
/** @readonly */
public int $statusSys;
/** @readonly */
public int $numFiles;
/** @readonly */
public string $filename;
/** @readonly */
public string $comment;

/** @return bool|int */
public function open(string $filename, int $flags = 0) {}
Expand Down
26 changes: 13 additions & 13 deletions ext/zip/php_zip_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: a50da348df01027594efd7c8ab7427c05ffea39e */
* Stub hash: d0f7d294c92a3d056e38abc92162c19e10329310 */

ZEND_BEGIN_ARG_INFO_EX(arginfo_zip_open, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0)
Expand Down Expand Up @@ -463,39 +463,39 @@ static zend_class_entry *register_class_ZipArchive(zend_class_entry *class_entry
zend_class_implements(class_entry, 1, class_entry_Countable);

zval property_lastId_default_value;
ZVAL_NULL(&property_lastId_default_value);
ZVAL_UNDEF(&property_lastId_default_value);
zend_string *property_lastId_name = zend_string_init("lastId", sizeof("lastId") - 1, 1);
zend_declare_property_ex(class_entry, property_lastId_name, &property_lastId_default_value, ZEND_ACC_PUBLIC, NULL);
zend_declare_typed_property(class_entry, property_lastId_name, &property_lastId_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG));
zend_string_release(property_lastId_name);

zval property_status_default_value;
ZVAL_NULL(&property_status_default_value);
ZVAL_UNDEF(&property_status_default_value);
zend_string *property_status_name = zend_string_init("status", sizeof("status") - 1, 1);
zend_declare_property_ex(class_entry, property_status_name, &property_status_default_value, ZEND_ACC_PUBLIC, NULL);
zend_declare_typed_property(class_entry, property_status_name, &property_status_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG));
zend_string_release(property_status_name);

zval property_statusSys_default_value;
ZVAL_NULL(&property_statusSys_default_value);
ZVAL_UNDEF(&property_statusSys_default_value);
zend_string *property_statusSys_name = zend_string_init("statusSys", sizeof("statusSys") - 1, 1);
zend_declare_property_ex(class_entry, property_statusSys_name, &property_statusSys_default_value, ZEND_ACC_PUBLIC, NULL);
zend_declare_typed_property(class_entry, property_statusSys_name, &property_statusSys_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG));
zend_string_release(property_statusSys_name);

zval property_numFiles_default_value;
ZVAL_NULL(&property_numFiles_default_value);
ZVAL_UNDEF(&property_numFiles_default_value);
zend_string *property_numFiles_name = zend_string_init("numFiles", sizeof("numFiles") - 1, 1);
zend_declare_property_ex(class_entry, property_numFiles_name, &property_numFiles_default_value, ZEND_ACC_PUBLIC, NULL);
zend_declare_typed_property(class_entry, property_numFiles_name, &property_numFiles_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG));
zend_string_release(property_numFiles_name);

zval property_filename_default_value;
ZVAL_NULL(&property_filename_default_value);
ZVAL_UNDEF(&property_filename_default_value);
zend_string *property_filename_name = zend_string_init("filename", sizeof("filename") - 1, 1);
zend_declare_property_ex(class_entry, property_filename_name, &property_filename_default_value, ZEND_ACC_PUBLIC, NULL);
zend_declare_typed_property(class_entry, property_filename_name, &property_filename_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_STRING));
zend_string_release(property_filename_name);

zval property_comment_default_value;
ZVAL_NULL(&property_comment_default_value);
ZVAL_UNDEF(&property_comment_default_value);
zend_string *property_comment_name = zend_string_init("comment", sizeof("comment") - 1, 1);
zend_declare_property_ex(class_entry, property_comment_name, &property_comment_default_value, ZEND_ACC_PUBLIC, NULL);
zend_declare_typed_property(class_entry, property_comment_name, &property_comment_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_STRING));
zend_string_release(property_comment_name);

return class_entry;
Expand Down
78 changes: 78 additions & 0 deletions ext/zip/tests/oo_readonly_properties.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
--TEST--
Test that ZipArchive properties are read-only
--EXTENSIONS--
zip
--FILE--
<?php

$zip = new ZipArchive();

try {
$zip->lastId = 1;
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}

try {
$zip->lastId += 1;
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}

var_dump($zip->lastId);

try {
$zip->status = 1;
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}

var_dump($zip->status);

try {
$zip->statusSys = 1;
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}

var_dump($zip->statusSys);

try {
$zip->numFiles = 1;
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}

var_dump($zip->numFiles);

try {
$zip->filename = "a";
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}

var_dump($zip->filename);

try {
$zip->comment = "a";
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}

var_dump($zip->comment);

?>
--EXPECT--
Cannot write read-only property ZipArchive::$lastId
Cannot write read-only property ZipArchive::$lastId
int(-1)
Cannot write read-only property ZipArchive::$status
int(0)
Cannot write read-only property ZipArchive::$statusSys
int(0)
Cannot write read-only property ZipArchive::$numFiles
int(0)
Cannot write read-only property ZipArchive::$filename
string(0) ""
Cannot write read-only property ZipArchive::$comment
string(0) ""