Skip to content

Commit 6b652b5

Browse files
Throw TypeError exception if segment is not int or string
1 parent c2b7d03 commit 6b652b5

3 files changed

Lines changed: 22 additions & 5 deletions

File tree

ext/standard/array.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6935,7 +6935,8 @@ static zval* array_get_nested(HashTable *ht, HashTable *path)
69356935
} else if (Z_TYPE_P(segment_val) == IS_LONG) {
69366936
current = zend_hash_index_find(current_ht, Z_LVAL_P(segment_val));
69376937
} else {
6938-
/* Invalid segment type */
6938+
/* Invalid segment type - throw TypeError */
6939+
zend_type_error("Path segment must be of type string|int, %s given", zend_zval_value_name(segment_val));
69396940
return NULL;
69406941
}
69416942

@@ -6979,6 +6980,10 @@ PHP_FUNCTION(array_get)
69796980

69806981
zval *result = array_get_nested(Z_ARRVAL_P(array), Z_ARRVAL_P(path));
69816982

6983+
if (EG(exception)) {
6984+
RETURN_THROWS();
6985+
}
6986+
69826987
if (result != NULL) {
69836988
RETURN_COPY_DEREF(result);
69846989
}
@@ -7003,6 +7008,10 @@ PHP_FUNCTION(array_has)
70037008

70047009
zval *result = array_get_nested(Z_ARRVAL_P(array), Z_ARRVAL_P(path));
70057010

7011+
if (EG(exception)) {
7012+
RETURN_THROWS();
7013+
}
7014+
70067015
RETURN_BOOL(result != NULL);
70077016
}
70087017
/* }}} */

ext/standard/tests/array/array_get.phpt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ var_dump(array_get($users, ['users', 1, 'name']));
3030
var_dump(array_get($array, ['products', 'chair', 'price'], 75));
3131

3232
// Test with invalid segment type in array path
33-
var_dump(array_get($array, ['products', new stdClass(), 'price'], 'invalid'));
33+
try {
34+
var_dump(array_get($array, ['products', new stdClass(), 'price'], 'invalid'));
35+
} catch (TypeError $e) {
36+
echo $e->getMessage() . "\n";
37+
}
3438

3539
// Test with references - ensure returned value is a copy, not a reference
3640
$ref_array = ['data' => ['value' => 'original']];
@@ -71,7 +75,7 @@ NULL
7175
string(5) "Alice"
7276
string(3) "Bob"
7377
int(75)
74-
string(7) "invalid"
78+
Path segment must be of type string|int, stdClass given
7579
string(8) "original"
7680
string(8) "original"
7781
string(7) "default"

ext/standard/tests/array/array_has.phpt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ $withNull = ['key' => null];
3232
var_dump(array_has($withNull, ['key']));
3333

3434
// Test with invalid segment type in array path
35-
var_dump(array_has($array, ['product', new stdClass()]));
35+
try {
36+
var_dump(array_has($array, ['product', new stdClass()]));
37+
} catch (TypeError $e) {
38+
echo $e->getMessage() . "\n";
39+
}
3640

3741
// Test with reference to an array in the path
3842
$array2 = ['world'];
@@ -58,7 +62,7 @@ bool(true)
5862
bool(true)
5963
bool(false)
6064
bool(true)
61-
bool(false)
65+
Path segment must be of type string|int, stdClass given
6266
bool(true)
6367
bool(true)
6468
Done

0 commit comments

Comments
 (0)