Skip to content

Commit

Permalink
Add return types to APCUIterator
Browse files Browse the repository at this point in the history
For compatibility with tentative types added in PHP 8.1.
  • Loading branch information
nikic committed Jul 21, 2021
1 parent 6c39c03 commit ddc5a8b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 31 deletions.
10 changes: 4 additions & 6 deletions apc_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,8 @@ PHP_METHOD(APCUIterator, current) {

if (apc_stack_size(iterator->stack) == iterator->stack_idx) {
if (iterator->fetch(iterator) == 0) {
RETURN_FALSE;
zend_throw_error(NULL, "Cannot call current() on invalid iterator");
return;
}
}

Expand All @@ -446,13 +447,10 @@ PHP_METHOD(APCUIterator, key) {
}

ENSURE_INITIALIZED(iterator);
if (apc_stack_size(iterator->stack) == 0) {
RETURN_FALSE;
}

if (apc_stack_size(iterator->stack) == iterator->stack_idx) {
if (iterator->fetch(iterator) == 0) {
RETURN_FALSE;
zend_throw_error(NULL, "Cannot call key() on invalid iterator");
return;
}
}

Expand Down
24 changes: 8 additions & 16 deletions apc_iterator.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,19 @@ public function __construct(
int $chunk_size = 0,
int $list = APC_LIST_ACTIVE);

/** @return void */
public function rewind();
public function rewind(): void;

This comment has been minimized.

Copy link
@GrahamCampbell

GrahamCampbell Oct 25, 2021

isn't this a major breaking change to a non-final class?

This comment has been minimized.

Copy link
@nikic

nikic Oct 25, 2021

Author Collaborator

Happy to mark the class as final.

This comment has been minimized.

Copy link
@GrahamCampbell

GrahamCampbell Oct 25, 2021

that wasn't my point, lol. it was that this was a major breaking change made in a patch release.

This comment has been minimized.

Copy link
@GrahamCampbell

GrahamCampbell Oct 25, 2021

making it final would be cool, but also a major breaking change.


/** @return void */
public function next();
public function next(): void;

/** @return bool */
public function valid();
public function valid(): bool;

/** @return string|int|false */
public function key();
public function key(): string|int;

This comment has been minimized.

Copy link
@GrahamCampbell

GrahamCampbell Oct 25, 2021

requires php 8.0? extension listed as only 7.0?

This comment has been minimized.

Copy link
@nikic

nikic Oct 25, 2021

Author Collaborator

The type is not used on old PHP versions.

This comment has been minimized.

Copy link
@GrahamCampbell

GrahamCampbell Oct 25, 2021

👌


/** @return mixed */
public function current();
public function current(): mixed;

/** @return int */
public function getTotalHits();
public function getTotalHits(): int;

/** @return int */
public function getTotalSize();
public function getTotalSize(): int;

/** @return int */
public function getTotalCount();
public function getTotalCount(): int;
}
20 changes: 12 additions & 8 deletions apc_iterator_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: bb3222265846a53027ddd5c8da8106de9af8b0d5 */
* Stub hash: e8a5a86d5bb9209117834ed0071130889e769c34 */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_APCUIterator___construct, 0, 0, 0)
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, search, "null")
Expand All @@ -8,22 +8,26 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_APCUIterator___construct, 0, 0, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, list, IS_LONG, 0, "APC_LIST_ACTIVE")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_APCUIterator_rewind, 0, 0, 0)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_APCUIterator_rewind, 0, 0, IS_VOID, 0)
ZEND_END_ARG_INFO()

#define arginfo_class_APCUIterator_next arginfo_class_APCUIterator_rewind

#define arginfo_class_APCUIterator_valid arginfo_class_APCUIterator_rewind
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_APCUIterator_valid, 0, 0, _IS_BOOL, 0)
ZEND_END_ARG_INFO()

#define arginfo_class_APCUIterator_key arginfo_class_APCUIterator_rewind
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_APCUIterator_key, 0, 0, MAY_BE_STRING|MAY_BE_LONG)
ZEND_END_ARG_INFO()

#define arginfo_class_APCUIterator_current arginfo_class_APCUIterator_rewind
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_APCUIterator_current, 0, 0, IS_MIXED, 0)
ZEND_END_ARG_INFO()

#define arginfo_class_APCUIterator_getTotalHits arginfo_class_APCUIterator_rewind
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_APCUIterator_getTotalHits, 0, 0, IS_LONG, 0)
ZEND_END_ARG_INFO()

#define arginfo_class_APCUIterator_getTotalSize arginfo_class_APCUIterator_rewind
#define arginfo_class_APCUIterator_getTotalSize arginfo_class_APCUIterator_getTotalHits

#define arginfo_class_APCUIterator_getTotalCount arginfo_class_APCUIterator_rewind
#define arginfo_class_APCUIterator_getTotalCount arginfo_class_APCUIterator_getTotalHits


ZEND_METHOD(APCUIterator, __construct);
Expand Down
2 changes: 1 addition & 1 deletion apc_iterator_legacy_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: bb3222265846a53027ddd5c8da8106de9af8b0d5 */
* Stub hash: e8a5a86d5bb9209117834ed0071130889e769c34 */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_APCUIterator___construct, 0, 0, 0)
ZEND_ARG_INFO(0, search)
Expand Down
37 changes: 37 additions & 0 deletions tests/iterator_011.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
APCUIterator key() and current() on invalid iterator
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--INI--
apc.enabled=1
apc.enable_cli=1
--FILE--
<?php

apcu_store("key1", "value1");

$it = new APCuIterator(null, APC_ITER_VALUE);
var_dump($it->key());
var_dump($it->current());
$it->next();

try {
var_dump($it->key());
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
try {
var_dump($it->current());
} catch (Error $e) {
echo $e->getMessage(), "\n";
}

?>
--EXPECT--
string(4) "key1"
array(1) {
["value"]=>
string(6) "value1"
}
Cannot call key() on invalid iterator
Cannot call current() on invalid iterator

0 comments on commit ddc5a8b

Please sign in to comment.