Skip to content

Commit ddc5a8b

Browse files
committed
Add return types to APCUIterator
For compatibility with tentative types added in PHP 8.1.
1 parent 6c39c03 commit ddc5a8b

5 files changed

+62
-31
lines changed

apc_iterator.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,8 @@ PHP_METHOD(APCUIterator, current) {
429429

430430
if (apc_stack_size(iterator->stack) == iterator->stack_idx) {
431431
if (iterator->fetch(iterator) == 0) {
432-
RETURN_FALSE;
432+
zend_throw_error(NULL, "Cannot call current() on invalid iterator");
433+
return;
433434
}
434435
}
435436

@@ -446,13 +447,10 @@ PHP_METHOD(APCUIterator, key) {
446447
}
447448

448449
ENSURE_INITIALIZED(iterator);
449-
if (apc_stack_size(iterator->stack) == 0) {
450-
RETURN_FALSE;
451-
}
452-
453450
if (apc_stack_size(iterator->stack) == iterator->stack_idx) {
454451
if (iterator->fetch(iterator) == 0) {
455-
RETURN_FALSE;
452+
zend_throw_error(NULL, "Cannot call key() on invalid iterator");
453+
return;
456454
}
457455
}
458456

apc_iterator.stub.php

+8-16
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,19 @@ public function __construct(
1313
int $chunk_size = 0,
1414
int $list = APC_LIST_ACTIVE);
1515

16-
/** @return void */
17-
public function rewind();
16+
public function rewind(): void;
1817

19-
/** @return void */
20-
public function next();
18+
public function next(): void;
2119

22-
/** @return bool */
23-
public function valid();
20+
public function valid(): bool;
2421

25-
/** @return string|int|false */
26-
public function key();
22+
public function key(): string|int;
2723

28-
/** @return mixed */
29-
public function current();
24+
public function current(): mixed;
3025

31-
/** @return int */
32-
public function getTotalHits();
26+
public function getTotalHits(): int;
3327

34-
/** @return int */
35-
public function getTotalSize();
28+
public function getTotalSize(): int;
3629

37-
/** @return int */
38-
public function getTotalCount();
30+
public function getTotalCount(): int;
3931
}

apc_iterator_arginfo.h

+12-8
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: bb3222265846a53027ddd5c8da8106de9af8b0d5 */
2+
* Stub hash: e8a5a86d5bb9209117834ed0071130889e769c34 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_APCUIterator___construct, 0, 0, 0)
55
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, search, "null")
@@ -8,22 +8,26 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_APCUIterator___construct, 0, 0, 0)
88
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, list, IS_LONG, 0, "APC_LIST_ACTIVE")
99
ZEND_END_ARG_INFO()
1010

11-
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_APCUIterator_rewind, 0, 0, 0)
11+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_APCUIterator_rewind, 0, 0, IS_VOID, 0)
1212
ZEND_END_ARG_INFO()
1313

1414
#define arginfo_class_APCUIterator_next arginfo_class_APCUIterator_rewind
1515

16-
#define arginfo_class_APCUIterator_valid arginfo_class_APCUIterator_rewind
16+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_APCUIterator_valid, 0, 0, _IS_BOOL, 0)
17+
ZEND_END_ARG_INFO()
1718

18-
#define arginfo_class_APCUIterator_key arginfo_class_APCUIterator_rewind
19+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_APCUIterator_key, 0, 0, MAY_BE_STRING|MAY_BE_LONG)
20+
ZEND_END_ARG_INFO()
1921

20-
#define arginfo_class_APCUIterator_current arginfo_class_APCUIterator_rewind
22+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_APCUIterator_current, 0, 0, IS_MIXED, 0)
23+
ZEND_END_ARG_INFO()
2124

22-
#define arginfo_class_APCUIterator_getTotalHits arginfo_class_APCUIterator_rewind
25+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_APCUIterator_getTotalHits, 0, 0, IS_LONG, 0)
26+
ZEND_END_ARG_INFO()
2327

24-
#define arginfo_class_APCUIterator_getTotalSize arginfo_class_APCUIterator_rewind
28+
#define arginfo_class_APCUIterator_getTotalSize arginfo_class_APCUIterator_getTotalHits
2529

26-
#define arginfo_class_APCUIterator_getTotalCount arginfo_class_APCUIterator_rewind
30+
#define arginfo_class_APCUIterator_getTotalCount arginfo_class_APCUIterator_getTotalHits
2731

2832

2933
ZEND_METHOD(APCUIterator, __construct);

apc_iterator_legacy_arginfo.h

+1-1
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: bb3222265846a53027ddd5c8da8106de9af8b0d5 */
2+
* Stub hash: e8a5a86d5bb9209117834ed0071130889e769c34 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_APCUIterator___construct, 0, 0, 0)
55
ZEND_ARG_INFO(0, search)

tests/iterator_011.phpt

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
APCUIterator key() and current() on invalid iterator
3+
--SKIPIF--
4+
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
5+
--INI--
6+
apc.enabled=1
7+
apc.enable_cli=1
8+
--FILE--
9+
<?php
10+
11+
apcu_store("key1", "value1");
12+
13+
$it = new APCuIterator(null, APC_ITER_VALUE);
14+
var_dump($it->key());
15+
var_dump($it->current());
16+
$it->next();
17+
18+
try {
19+
var_dump($it->key());
20+
} catch (Error $e) {
21+
echo $e->getMessage(), "\n";
22+
}
23+
try {
24+
var_dump($it->current());
25+
} catch (Error $e) {
26+
echo $e->getMessage(), "\n";
27+
}
28+
29+
?>
30+
--EXPECT--
31+
string(4) "key1"
32+
array(1) {
33+
["value"]=>
34+
string(6) "value1"
35+
}
36+
Cannot call key() on invalid iterator
37+
Cannot call current() on invalid iterator

0 commit comments

Comments
 (0)