Skip to content

Commit 1a8936c

Browse files
committed
Check ReflectionReference::fromArrayElement with union types
ReflectionReference::fromArrayElement(array $array, int|string $key): ?ReflectionReference is going to be its official signature for PHP 8.0. Closes GH-5651
1 parent c1823c6 commit 1a8936c

File tree

4 files changed

+15
-16
lines changed

4 files changed

+15
-16
lines changed

ext/reflection/php_reflection.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -6084,20 +6084,20 @@ static zend_bool is_ignorable_reference(HashTable *ht, zval *ref) {
60846084
ZEND_METHOD(ReflectionReference, fromArrayElement)
60856085
{
60866086
HashTable *ht;
6087-
zval *key, *item;
6087+
zval *item;
6088+
zend_string *string_key = NULL;
6089+
zend_long int_key = 0;
60886090
reflection_object *intern;
60896091

6090-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "hz", &ht, &key) == FAILURE) {
6091-
RETURN_THROWS();
6092-
}
6092+
ZEND_PARSE_PARAMETERS_START(2, 2)
6093+
Z_PARAM_ARRAY_HT(ht)
6094+
Z_PARAM_STR_OR_LONG(string_key, int_key)
6095+
ZEND_PARSE_PARAMETERS_END();
60936096

6094-
if (Z_TYPE_P(key) == IS_LONG) {
6095-
item = zend_hash_index_find(ht, Z_LVAL_P(key));
6096-
} else if (Z_TYPE_P(key) == IS_STRING) {
6097-
item = zend_symtable_find(ht, Z_STR_P(key));
6097+
if (string_key) {
6098+
item = zend_hash_find(ht, string_key);
60986099
} else {
6099-
zend_argument_type_error(2, "must be of type string|int, %s given", zend_zval_type_name(key));
6100-
RETURN_THROWS();
6100+
item = zend_hash_index_find(ht, int_key);
61016101
}
61026102

61036103
if (!item) {

ext/reflection/php_reflection.stub.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -643,8 +643,7 @@ public function getCopyright() {}
643643

644644
final class ReflectionReference
645645
{
646-
/** @param int|string $key */
647-
public static function fromArrayElement(array $array, $key): ?ReflectionReference {}
646+
public static function fromArrayElement(array $array, int|string $key): ?ReflectionReference {}
648647

649648
public function getId(): string {}
650649

ext/reflection/php_reflection_arginfo.h

+2-2
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: d698afd338e4bf7c782f0edddfcbe95859eef477 */
2+
* Stub hash: c2bd96bf9b5ca866860f8f3c04937c9fff5c3afa */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Reflection_getModifierNames, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, modifiers, IS_LONG, 0)
@@ -467,7 +467,7 @@ ZEND_END_ARG_INFO()
467467

468468
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_ReflectionReference_fromArrayElement, 0, 2, ReflectionReference, 1)
469469
ZEND_ARG_TYPE_INFO(0, array, IS_ARRAY, 0)
470-
ZEND_ARG_INFO(0, key)
470+
ZEND_ARG_TYPE_MASK(0, key, MAY_BE_LONG|MAY_BE_STRING, NULL)
471471
ZEND_END_ARG_INFO()
472472

473473
#define arginfo_class_ReflectionReference_getId arginfo_class_ReflectionFunction___toString

ext/reflection/tests/ReflectionReference_errors.phpt

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ try {
1616
}
1717

1818
try {
19-
ReflectionReference::fromArrayElement([], 1.5);
19+
ReflectionReference::fromArrayElement([], []);
2020
} catch (TypeError $e) {
2121
echo $e->getMessage(), "\n";
2222
}
@@ -42,7 +42,7 @@ var_dump(unserialize('O:19:"ReflectionReference":0:{}'));
4242
--EXPECTF--
4343
Call to private ReflectionReference::__construct() from global scope
4444
ReflectionReference::fromArrayElement(): Argument #1 ($array) must be of type array, stdClass given
45-
ReflectionReference::fromArrayElement(): Argument #2 ($key) must be of type string|int, float given
45+
ReflectionReference::fromArrayElement(): Argument #2 ($key) must be of type string|int, array given
4646
Array key not found
4747
Serialization of 'ReflectionReference' is not allowed
4848

0 commit comments

Comments
 (0)