Skip to content

Commit 70e604e

Browse files
committed
Promote warnings to errors in extract()
1 parent 4669c53 commit 70e604e

File tree

5 files changed

+40
-18
lines changed

5 files changed

+40
-18
lines changed

ext/standard/array.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2427,7 +2427,7 @@ static zend_long php_extract_skip(zend_array *arr, zend_array *symbol_table) /*
24272427
}
24282428
/* }}} */
24292429

2430-
/* {{{ proto int|null extract(array var_array [, int extract_type [, string prefix]])
2430+
/* {{{ proto int extract(array var_array [, int extract_type [, string prefix]])
24312431
Imports variables into symbol table from an array */
24322432
PHP_FUNCTION(extract)
24332433
{
@@ -2452,18 +2452,18 @@ PHP_FUNCTION(extract)
24522452
extract_type &= 0xff;
24532453

24542454
if (extract_type < EXTR_OVERWRITE || extract_type > EXTR_IF_EXISTS) {
2455-
php_error_docref(NULL, E_WARNING, "Invalid extract type");
2455+
zend_throw_error(NULL, "Invalid extract type");
24562456
return;
24572457
}
24582458

24592459
if (extract_type > EXTR_SKIP && extract_type <= EXTR_PREFIX_IF_EXISTS && ZEND_NUM_ARGS() < 3) {
2460-
php_error_docref(NULL, E_WARNING, "specified extract type requires the prefix parameter");
2460+
zend_throw_error(NULL, "Specified extract type requires the prefix parameter");
24612461
return;
24622462
}
24632463

24642464
if (prefix) {
24652465
if (ZSTR_LEN(prefix) && !php_valid_var_name(ZSTR_VAL(prefix), ZSTR_LEN(prefix))) {
2466-
php_error_docref(NULL, E_WARNING, "prefix is not a valid identifier");
2466+
zend_throw_error(NULL, "Prefix is not a valid identifier");
24672467
return;
24682468
}
24692469
}

ext/standard/basic_functions.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ function in_array($needle, array $haystack, bool $strict = false): bool {}
139139
function array_search($needle, array $haystack, bool $strict = false) {}
140140

141141
/** @prefer-ref $arg */
142-
function extract(array &$arg, int $extract_type = EXTR_OVERWRITE, string $prefix = ""): ?int {}
142+
function extract(array &$arg, int $extract_type = EXTR_OVERWRITE, string $prefix = ""): int {}
143143

144144
function compact($var_name, ...$var_names): array {}
145145

ext/standard/basic_functions_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_array_search, 0, 0, 2)
150150
ZEND_ARG_TYPE_INFO(0, strict, _IS_BOOL, 0)
151151
ZEND_END_ARG_INFO()
152152

153-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_extract, 0, 1, IS_LONG, 1)
153+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_extract, 0, 1, IS_LONG, 0)
154154
ZEND_ARG_TYPE_INFO(ZEND_SEND_PREFER_REF, arg, IS_ARRAY, 0)
155155
ZEND_ARG_TYPE_INFO(0, extract_type, IS_LONG, 0)
156156
ZEND_ARG_TYPE_INFO(0, prefix, IS_STRING, 0)

ext/standard/tests/array/extract_error.phpt

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,33 @@ echo "*** Testing Error Conditions ***\n";
88

99
/* Invalid second argument ( only 0-6 is valid) */
1010
$arr = array(1);
11-
var_dump( extract($arr, -1 . "wddr") );
12-
var_dump( extract($arr, 7 , "wddr") );
11+
12+
try {
13+
var_dump( extract($arr, -1 . "wddr") );
14+
} catch (\Error $e) {
15+
echo $e->getMessage() . "\n";
16+
}
17+
18+
try {
19+
var_dump( extract($arr, 7 , "wddr") );
20+
} catch (\Error $e) {
21+
echo $e->getMessage() . "\n";
22+
}
1323

1424
/* Two Arguments, second as prefix but without prefix string as third argument */
15-
var_dump( extract($arr,EXTR_PREFIX_IF_EXISTS) );
25+
try {
26+
var_dump( extract($arr,EXTR_PREFIX_IF_EXISTS) );
27+
} catch (\Error $e) {
28+
echo $e->getMessage() . "\n";
29+
}
1630

1731
echo "Done\n";
1832
?>
1933
--EXPECTF--
2034
*** Testing Error Conditions ***
2135

2236
Notice: A non well formed numeric value encountered in %s on line %d
23-
24-
Warning: extract(): Invalid extract type in %s on line %d
25-
NULL
26-
27-
Warning: extract(): Invalid extract type in %s on line %d
28-
NULL
29-
30-
Warning: extract(): specified extract type requires the prefix parameter in %s on line %d
31-
NULL
37+
Invalid extract type
38+
Invalid extract type
39+
Specified extract type requires the prefix parameter
3240
Done
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Test extract() function - error condition - Invalid prefix.
3+
--FILE--
4+
<?php
5+
$a = ["1" => "one", "2" => "two", "3" => "three", "4" => "four", "5" => "five"];
6+
7+
try {
8+
extract($a, EXTR_PREFIX_ALL, '85bogus');
9+
} catch (\Error $e) {
10+
echo $e->getMessage();
11+
}
12+
?>
13+
--EXPECT--
14+
Prefix is not a valid identifier

0 commit comments

Comments
 (0)