Skip to content

Commit 29c6eb6

Browse files
committed
Declare Directory properties
Some error handling test changes, as changes to the $handle property are now detected earlier.
1 parent 7e84b1e commit 29c6eb6

File tree

6 files changed

+51
-117
lines changed

6 files changed

+51
-117
lines changed

ext/standard/dir.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ php_dir_globals dir_globals;
5858

5959
static zend_class_entry *dir_class_entry_ptr;
6060

61+
#define Z_DIRECTORY_PATH_P(zv) OBJ_PROP_NUM(Z_OBJ_P(zv), 0)
62+
#define Z_DIRECTORY_HANDLE_P(zv) OBJ_PROP_NUM(Z_OBJ_P(zv), 1)
63+
6164
#define FETCH_DIRP() \
6265
myself = getThis(); \
6366
if (!myself) { \
@@ -80,11 +83,12 @@ static zend_class_entry *dir_class_entry_ptr;
8083
} \
8184
} else { \
8285
ZEND_PARSE_PARAMETERS_NONE(); \
83-
if ((tmp = zend_hash_str_find(Z_OBJPROP_P(myself), "handle", sizeof("handle")-1)) == NULL) { \
86+
zval *handle_zv = Z_DIRECTORY_HANDLE_P(myself); \
87+
if (Z_TYPE_P(handle_zv) != IS_RESOURCE) { \
8488
zend_throw_error(NULL, "Unable to find my handle property"); \
8589
RETURN_THROWS(); \
8690
} \
87-
if ((dirp = (php_stream *)zend_fetch_resource_ex(tmp, "Directory", php_file_le_stream())) == NULL) { \
91+
if ((dirp = (php_stream *)zend_fetch_resource_ex(handle_zv, "Directory", php_file_le_stream())) == NULL) { \
8892
RETURN_THROWS(); \
8993
} \
9094
}
@@ -218,8 +222,8 @@ static void _php_do_opendir(INTERNAL_FUNCTION_PARAMETERS, int createobject)
218222

219223
if (createobject) {
220224
object_init_ex(return_value, dir_class_entry_ptr);
221-
add_property_stringl(return_value, "path", dirname, dir_len);
222-
add_property_resource(return_value, "handle", dirp->res);
225+
ZVAL_STRINGL(Z_DIRECTORY_PATH_P(return_value), dirname, dir_len);
226+
ZVAL_RES(Z_DIRECTORY_HANDLE_P(return_value), dirp->res);
223227
php_stream_auto_cleanup(dirp); /* so we don't get warnings under debug */
224228
} else {
225229
php_stream_to_zval(dirp, return_value);
@@ -244,7 +248,7 @@ PHP_FUNCTION(dir)
244248
/* {{{ Close directory connection identified by the dir_handle */
245249
PHP_FUNCTION(closedir)
246250
{
247-
zval *id = NULL, *tmp, *myself;
251+
zval *id = NULL, *myself;
248252
php_stream *dirp;
249253
zend_resource *res;
250254

@@ -355,7 +359,7 @@ PHP_FUNCTION(getcwd)
355359
/* {{{ Rewind dir_handle back to the start */
356360
PHP_FUNCTION(rewinddir)
357361
{
358-
zval *id = NULL, *tmp, *myself;
362+
zval *id = NULL, *myself;
359363
php_stream *dirp;
360364

361365
FETCH_DIRP();
@@ -372,7 +376,7 @@ PHP_FUNCTION(rewinddir)
372376
/* {{{ Read directory entry from dir_handle */
373377
PHP_FUNCTION(readdir)
374378
{
375-
zval *id = NULL, *tmp, *myself;
379+
zval *id = NULL, *myself;
376380
php_stream *dirp;
377381
php_stream_dirent entry;
378382

ext/standard/dir.stub.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
class Directory
66
{
7+
public readonly string $path;
8+
9+
/** @var resource */
10+
public readonly mixed $handle;
11+
712
/**
813
* @tentative-return-type
914
* @implementation-alias closedir

ext/standard/dir_arginfo.h

Lines changed: 13 additions & 1 deletion
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: e3d46788bb18dc90a0922e5738442b2932dd53f6 */
2+
* Stub hash: b3890e798e609e306027b4717ce0c5e782884087 */
33

44
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_Directory_close, 0, 0, IS_VOID, 0)
55
ZEND_END_ARG_INFO()
@@ -29,5 +29,17 @@ static zend_class_entry *register_class_Directory(void)
2929
INIT_CLASS_ENTRY(ce, "Directory", class_Directory_methods);
3030
class_entry = zend_register_internal_class_ex(&ce, NULL);
3131

32+
zval property_path_default_value;
33+
ZVAL_UNDEF(&property_path_default_value);
34+
zend_string *property_path_name = zend_string_init("path", sizeof("path") - 1, 1);
35+
zend_declare_typed_property(class_entry, property_path_name, &property_path_default_value, ZEND_ACC_PUBLIC|ZEND_ACC_READONLY, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_STRING));
36+
zend_string_release(property_path_name);
37+
38+
zval property_handle_default_value;
39+
ZVAL_UNDEF(&property_handle_default_value);
40+
zend_string *property_handle_name = zend_string_init("handle", sizeof("handle") - 1, 1);
41+
zend_declare_typed_property(class_entry, property_handle_name, &property_handle_default_value, ZEND_ACC_PUBLIC|ZEND_ACC_READONLY, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_ANY));
42+
zend_string_release(property_handle_name);
43+
3244
return class_entry;
3345
}

ext/standard/tests/directory/DirectoryClass_basic_001.phpt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ Class [ <internal%s> class Directory ] {
3535
- Static methods [0] {
3636
}
3737

38-
- Properties [0] {
38+
- Properties [2] {
39+
Property [ public readonly string $path ]
40+
Property [ public readonly mixed $handle ]
3941
}
4042

4143
- Methods [3] {
@@ -63,5 +65,9 @@ Class [ <internal%s> class Directory ] {
6365
}
6466
Cannot instantiate a valid Directory directly:
6567
object(Directory)#%d (0) {
68+
["path"]=>
69+
uninitialized(string)
70+
["handle"]=>
71+
uninitialized(mixed)
6672
}
6773
Unable to find my handle property

ext/standard/tests/directory/DirectoryClass_error_001-mb.phpt

Lines changed: 0 additions & 65 deletions
This file was deleted.
Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,27 @@
11
--TEST--
2-
Directory class behaviour.
2+
Changing Directory::$handle property
33
--FILE--
44
<?php
55

6-
echo "\n--> Try all methods with bad handle:\n";
7-
$d = new Directory(getcwd());
8-
$d->handle = "Havoc!";
6+
$d = dir(getcwd());
97
try {
10-
var_dump($d->read());
11-
} catch (TypeError $e) {
8+
$d->handle = "Havoc!";
9+
} catch (Error $e) {
1210
echo $e->getMessage(), "\n";
1311
}
14-
try {
15-
var_dump($d->rewind());
16-
} catch (TypeError $e) {
17-
echo $e->getMessage(), "\n";
18-
}
19-
try {
20-
var_dump($d->close());
21-
} catch (TypeError $e) {
22-
echo $e->getMessage(), "\n";
23-
}
24-
25-
echo "\n--> Try all methods with no handle:\n";
26-
$d = new Directory(getcwd());
27-
unset($d->handle);
12+
var_dump($d->handle);
2813

14+
$d = dir(getcwd());
2915
try {
30-
var_dump($d->read());
31-
} catch (\Error $e) {
32-
echo $e->getMessage() . "\n";
33-
}
34-
try {
35-
var_dump($d->rewind());
36-
} catch (\Error $e) {
37-
echo $e->getMessage() . "\n";
38-
}
39-
try {
40-
var_dump($d->close());
41-
} catch (\Error $e) {
42-
echo $e->getMessage() . "\n";
16+
unset($d->handle);
17+
} catch (Error $e) {
18+
echo $e->getMessage(), "\n";
4319
}
20+
var_dump($d->handle);
4421

4522
?>
46-
--EXPECT--
47-
--> Try all methods with bad handle:
48-
Directory::read(): supplied argument is not a valid Directory resource
49-
Directory::rewind(): supplied argument is not a valid Directory resource
50-
Directory::close(): supplied argument is not a valid Directory resource
51-
52-
--> Try all methods with no handle:
53-
Unable to find my handle property
54-
Unable to find my handle property
55-
Unable to find my handle property
23+
--EXPECTF--
24+
Cannot modify readonly property Directory::$handle
25+
resource(%d) of type (stream)
26+
Cannot unset readonly property Directory::$handle
27+
resource(%d) of type (stream)

0 commit comments

Comments
 (0)