Skip to content

Commit

Permalink
Narrow ArrayIterator::key() return type
Browse files Browse the repository at this point in the history
This can only return string|int|null, not any key type.
  • Loading branch information
nikic committed Jul 15, 2021
1 parent 11f62c9 commit f556a30
Show file tree
Hide file tree
Showing 11 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Zend/tests/foreach_004.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class IT extends ArrayIterator {

function rewind(): void {$this->trap(__FUNCTION__); parent::rewind();}
function valid(): bool {$this->trap(__FUNCTION__); return parent::valid();}
function key(): mixed {$this->trap(__FUNCTION__); return parent::key();}
function key(): string|int|null { $this->trap(__FUNCTION__); return parent::key(); }
function next(): void {$this->trap(__FUNCTION__); parent::next();}
}

Expand Down
2 changes: 1 addition & 1 deletion ext/spl/spl_array.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public function rewind(): void {}
public function current(): mixed {}

/** @tentative-return-type */
public function key(): mixed {}
public function key(): string|int|null {}

/** @tentative-return-type */
public function next(): void {}
Expand Down
5 changes: 3 additions & 2 deletions ext/spl/spl_array_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: d58390328052e8db45a6e388ab1b3fdf882be635 */
* Stub hash: e8e9909c2548a2259ba58ecf9f2ad6fe5add70f4 */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_ArrayObject___construct, 0, 0, 0)
ZEND_ARG_TYPE_MASK(0, array, MAY_BE_ARRAY|MAY_BE_OBJECT, "[]")
Expand Down Expand Up @@ -134,7 +134,8 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_ArrayIterator_current, 0, 0, IS_MIXED, 0)
ZEND_END_ARG_INFO()

#define arginfo_class_ArrayIterator_key arginfo_class_ArrayIterator_current
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_MASK_EX(arginfo_class_ArrayIterator_key, 0, 0, MAY_BE_STRING|MAY_BE_LONG|MAY_BE_NULL)
ZEND_END_ARG_INFO()

#define arginfo_class_ArrayIterator_next arginfo_class_ArrayIterator_rewind

Expand Down
2 changes: 1 addition & 1 deletion ext/spl/tests/arrayObject_getIteratorClass_basic1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class MyIterator extends ArrayIterator {
parent::next();
}

function key(): mixed {
function key(): string|int|null {
$args = func_get_args();
echo " In " . __METHOD__ . "(" . implode(',', $args) . ")\n";
return parent::key();
Expand Down
2 changes: 1 addition & 1 deletion ext/spl/tests/array_020.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ArrayIteratorEx extends ArrayIterator
return ArrayIterator::valid();
}

function key(): mixed
function key(): string|int|null
{
echo __METHOD__ . "\n";
return ArrayIterator::key();
Expand Down
2 changes: 1 addition & 1 deletion ext/spl/tests/bug65328.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class Node extends \RecursiveArrayIterator implements \Countable
/**
* @return scalar
*/
public function key(): mixed
public function key(): string|int|null
{
return $this->index;
}
Expand Down
2 changes: 1 addition & 1 deletion ext/spl/tests/iterator_007.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ArrayIteratorEx extends ArrayIterator
echo __METHOD__ . "\n";
return parent::current();
}
function key(): mixed
function key(): string|int|null
{
echo __METHOD__ . "\n";
return parent::key();
Expand Down
2 changes: 1 addition & 1 deletion ext/spl/tests/iterator_008.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ArrayIteratorEx extends ArrayIterator
echo __METHOD__ . "\n";
return parent::current();
}
function key(): mixed
function key(): string|int|null
{
echo __METHOD__ . "\n";
return parent::key();
Expand Down
2 changes: 1 addition & 1 deletion ext/spl/tests/iterator_041.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class MyArrayIterator extends ArrayIterator
return parent::current();
}

function key(): mixed
function key(): string|int|null
{
self::fail(5, __FUNCTION__);
return parent::key();
Expand Down
2 changes: 1 addition & 1 deletion ext/spl/tests/iterator_041a.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class MyArrayIterator extends ArrayIterator
return parent::current();
}

function key(): mixed
function key(): string|int|null
{
self::fail(5, __FUNCTION__);
return parent::key();
Expand Down
2 changes: 1 addition & 1 deletion ext/spl/tests/iterator_041b.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class MyArrayIterator extends ArrayIterator
return parent::current();
}

function key(): mixed
function key(): string|int|null
{
self::fail(5, __FUNCTION__);
return parent::key();
Expand Down

0 comments on commit f556a30

Please sign in to comment.