Skip to content

Commit b567a9d

Browse files
committed
Use typed properties in ext/zip
Closes GH-6881
1 parent 28a66b3 commit b567a9d

File tree

4 files changed

+124
-34
lines changed

4 files changed

+124
-34
lines changed

ext/zip/php_zip.c

+21-4
Original file line numberDiff line numberDiff line change
@@ -859,10 +859,6 @@ static zval *php_zip_property_reader(ze_zip_object *obj, zip_prop_handler *hnd,
859859
ZVAL_EMPTY_STRING(rv);
860860
}
861861
break;
862-
/* case IS_TRUE */
863-
case IS_FALSE:
864-
ZVAL_BOOL(rv, retint);
865-
break;
866862
case IS_LONG:
867863
ZVAL_LONG(rv, retint);
868864
break;
@@ -894,6 +890,26 @@ static zval *php_zip_get_property_ptr_ptr(zend_object *object, zend_string *name
894890
}
895891
/* }}} */
896892

893+
894+
static zval *php_zip_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot)
895+
{
896+
ze_zip_object *obj;
897+
zip_prop_handler *hnd = NULL;
898+
899+
obj = php_zip_fetch_object(object);
900+
901+
if (obj->prop_handler != NULL) {
902+
hnd = zend_hash_find_ptr(obj->prop_handler, name);
903+
}
904+
905+
if (hnd != NULL) {
906+
zend_throw_error(NULL, "Cannot write read-only property %s::$%s", ZSTR_VAL(object->ce->name), ZSTR_VAL(name));
907+
return &EG(error_zval);
908+
}
909+
910+
return zend_std_write_property(object, name, value, cache_slot);
911+
}
912+
897913
static zval *php_zip_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv) /* {{{ */
898914
{
899915
ze_zip_object *obj;
@@ -3029,6 +3045,7 @@ static PHP_MINIT_FUNCTION(zip)
30293045
zip_object_handlers.get_properties = php_zip_get_properties;
30303046
zip_object_handlers.read_property = php_zip_read_property;
30313047
zip_object_handlers.has_property = php_zip_has_property;
3048+
zip_object_handlers.write_property = php_zip_write_property;
30323049

30333050
zip_class_entry = register_class_ZipArchive(zend_ce_countable);
30343051
zip_class_entry->create_object = php_zip_object_new;

ext/zip/php_zip.stub.php

+12-17
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,18 @@ function zip_entry_compressionmethod($zip_entry): string|false {}
6666

6767
class ZipArchive implements Countable
6868
{
69-
/** @var int|null */
70-
public $lastId;
71-
72-
/** @var int|null */
73-
public $status;
74-
75-
/** @var int|null */
76-
public $statusSys;
77-
78-
/** @var int|null */
79-
public $numFiles;
80-
81-
/** @var string|null */
82-
public $filename;
83-
84-
/** @var string|null */
85-
public $comment;
69+
/** @readonly */
70+
public int $lastId;
71+
/** @readonly */
72+
public int $status;
73+
/** @readonly */
74+
public int $statusSys;
75+
/** @readonly */
76+
public int $numFiles;
77+
/** @readonly */
78+
public string $filename;
79+
/** @readonly */
80+
public string $comment;
8681

8782
/** @return bool|int */
8883
public function open(string $filename, int $flags = 0) {}

ext/zip/php_zip_arginfo.h

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: a50da348df01027594efd7c8ab7427c05ffea39e */
2+
* Stub hash: d0f7d294c92a3d056e38abc92162c19e10329310 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_zip_open, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0)
@@ -463,39 +463,39 @@ static zend_class_entry *register_class_ZipArchive(zend_class_entry *class_entry
463463
zend_class_implements(class_entry, 1, class_entry_Countable);
464464

465465
zval property_lastId_default_value;
466-
ZVAL_NULL(&property_lastId_default_value);
466+
ZVAL_UNDEF(&property_lastId_default_value);
467467
zend_string *property_lastId_name = zend_string_init("lastId", sizeof("lastId") - 1, 1);
468-
zend_declare_property_ex(class_entry, property_lastId_name, &property_lastId_default_value, ZEND_ACC_PUBLIC, NULL);
468+
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));
469469
zend_string_release(property_lastId_name);
470470

471471
zval property_status_default_value;
472-
ZVAL_NULL(&property_status_default_value);
472+
ZVAL_UNDEF(&property_status_default_value);
473473
zend_string *property_status_name = zend_string_init("status", sizeof("status") - 1, 1);
474-
zend_declare_property_ex(class_entry, property_status_name, &property_status_default_value, ZEND_ACC_PUBLIC, NULL);
474+
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));
475475
zend_string_release(property_status_name);
476476

477477
zval property_statusSys_default_value;
478-
ZVAL_NULL(&property_statusSys_default_value);
478+
ZVAL_UNDEF(&property_statusSys_default_value);
479479
zend_string *property_statusSys_name = zend_string_init("statusSys", sizeof("statusSys") - 1, 1);
480-
zend_declare_property_ex(class_entry, property_statusSys_name, &property_statusSys_default_value, ZEND_ACC_PUBLIC, NULL);
480+
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));
481481
zend_string_release(property_statusSys_name);
482482

483483
zval property_numFiles_default_value;
484-
ZVAL_NULL(&property_numFiles_default_value);
484+
ZVAL_UNDEF(&property_numFiles_default_value);
485485
zend_string *property_numFiles_name = zend_string_init("numFiles", sizeof("numFiles") - 1, 1);
486-
zend_declare_property_ex(class_entry, property_numFiles_name, &property_numFiles_default_value, ZEND_ACC_PUBLIC, NULL);
486+
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));
487487
zend_string_release(property_numFiles_name);
488488

489489
zval property_filename_default_value;
490-
ZVAL_NULL(&property_filename_default_value);
490+
ZVAL_UNDEF(&property_filename_default_value);
491491
zend_string *property_filename_name = zend_string_init("filename", sizeof("filename") - 1, 1);
492-
zend_declare_property_ex(class_entry, property_filename_name, &property_filename_default_value, ZEND_ACC_PUBLIC, NULL);
492+
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));
493493
zend_string_release(property_filename_name);
494494

495495
zval property_comment_default_value;
496-
ZVAL_NULL(&property_comment_default_value);
496+
ZVAL_UNDEF(&property_comment_default_value);
497497
zend_string *property_comment_name = zend_string_init("comment", sizeof("comment") - 1, 1);
498-
zend_declare_property_ex(class_entry, property_comment_name, &property_comment_default_value, ZEND_ACC_PUBLIC, NULL);
498+
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));
499499
zend_string_release(property_comment_name);
500500

501501
return class_entry;
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
--TEST--
2+
Test that ZipArchive properties are read-only
3+
--EXTENSIONS--
4+
zip
5+
--FILE--
6+
<?php
7+
8+
$zip = new ZipArchive();
9+
10+
try {
11+
$zip->lastId = 1;
12+
} catch (Error $exception) {
13+
echo $exception->getMessage() . "\n";
14+
}
15+
16+
try {
17+
$zip->lastId += 1;
18+
} catch (Error $exception) {
19+
echo $exception->getMessage() . "\n";
20+
}
21+
22+
var_dump($zip->lastId);
23+
24+
try {
25+
$zip->status = 1;
26+
} catch (Error $exception) {
27+
echo $exception->getMessage() . "\n";
28+
}
29+
30+
var_dump($zip->status);
31+
32+
try {
33+
$zip->statusSys = 1;
34+
} catch (Error $exception) {
35+
echo $exception->getMessage() . "\n";
36+
}
37+
38+
var_dump($zip->statusSys);
39+
40+
try {
41+
$zip->numFiles = 1;
42+
} catch (Error $exception) {
43+
echo $exception->getMessage() . "\n";
44+
}
45+
46+
var_dump($zip->numFiles);
47+
48+
try {
49+
$zip->filename = "a";
50+
} catch (Error $exception) {
51+
echo $exception->getMessage() . "\n";
52+
}
53+
54+
var_dump($zip->filename);
55+
56+
try {
57+
$zip->comment = "a";
58+
} catch (Error $exception) {
59+
echo $exception->getMessage() . "\n";
60+
}
61+
62+
var_dump($zip->comment);
63+
64+
?>
65+
--EXPECT--
66+
Cannot write read-only property ZipArchive::$lastId
67+
Cannot write read-only property ZipArchive::$lastId
68+
int(-1)
69+
Cannot write read-only property ZipArchive::$status
70+
int(0)
71+
Cannot write read-only property ZipArchive::$statusSys
72+
int(0)
73+
Cannot write read-only property ZipArchive::$numFiles
74+
int(0)
75+
Cannot write read-only property ZipArchive::$filename
76+
string(0) ""
77+
Cannot write read-only property ZipArchive::$comment
78+
string(0) ""

0 commit comments

Comments
 (0)