Skip to content

Promote warnings to errors in array_combine() #4584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -6401,7 +6401,7 @@ PHP_FUNCTION(array_chunk)
}
/* }}} */

/* {{{ proto array|false array_combine(array keys, array values)
/* {{{ proto array array_combine(array keys, array values)
Creates an array by using the elements of the first parameter as keys and the elements of the second as the corresponding values */
PHP_FUNCTION(array_combine)
{
Expand All @@ -6419,8 +6419,8 @@ PHP_FUNCTION(array_combine)
num_values = zend_hash_num_elements(values);

if (num_keys != num_values) {
php_error_docref(NULL, E_WARNING, "Both parameters should have an equal number of elements");
RETURN_FALSE;
zend_throw_error(NULL, "Both parameters should have an equal number of elements");
return;
}

if (!num_keys) {
Expand All @@ -6435,13 +6435,11 @@ PHP_FUNCTION(array_combine)
} else if (Z_TYPE(values->arData[pos_values].val) != IS_UNDEF) {
entry_values = &values->arData[pos_values].val;
if (Z_TYPE_P(entry_keys) == IS_LONG) {
entry_values = zend_hash_index_update(Z_ARRVAL_P(return_value),
Z_LVAL_P(entry_keys), entry_values);
entry_values = zend_hash_index_update(Z_ARRVAL_P(return_value), Z_LVAL_P(entry_keys), entry_values);
} else {
zend_string *tmp_key;
zend_string *key = zval_get_tmp_string(entry_keys, &tmp_key);
entry_values = zend_symtable_update(Z_ARRVAL_P(return_value),
key, entry_values);
entry_values = zend_symtable_update(Z_ARRVAL_P(return_value), key, entry_values);
zend_tmp_string_release(tmp_key);
}
zval_add_ref(entry_values);
Expand Down
39 changes: 22 additions & 17 deletions ext/standard/tests/array/array_combine_error2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,42 @@ var_dump( array_combine(array(), array()) );

// Testing array_combine by passing empty array to $keys
echo "\n-- Testing array_combine() function with empty array for \$keys argument --\n";
var_dump( array_combine(array(), array(1, 2)) );
try {
var_dump( array_combine(array(), array(1, 2)) );
} catch (\Error $e) {
echo $e->getMessage();
}

// Testing array_combine by passing empty array to $values
echo "\n-- Testing array_combine() function with empty array for \$values argument --\n";
var_dump( array_combine(array(1, 2), array()) );
try {
var_dump( array_combine(array(1, 2), array()) );
} catch (\Error $e) {
echo $e->getMessage();
}

// Testing array_combine with arrays having unequal number of elements
echo "\n-- Testing array_combine() function by passing array with unequal number of elements --\n";
var_dump( array_combine(array(1, 2), array(1, 2, 3)) );
try {
var_dump( array_combine(array(1, 2), array(1, 2, 3)) );
} catch (\Error $e) {
echo $e->getMessage();
}

echo "Done";
?>
--EXPECTF--

DONE
--EXPECT--
*** Testing array_combine() : error conditions specific to array_combine() ***

-- Testing array_combine() function with empty arrays --
array(0) {
}

-- Testing array_combine() function with empty array for $keys argument --

Warning: array_combine(): Both parameters should have an equal number of elements in %s on line %d
bool(false)

Both parameters should have an equal number of elements
-- Testing array_combine() function with empty array for $values argument --

Warning: array_combine(): Both parameters should have an equal number of elements in %s on line %d
bool(false)

Both parameters should have an equal number of elements
-- Testing array_combine() function by passing array with unequal number of elements --

Warning: array_combine(): Both parameters should have an equal number of elements in %s on line %d
bool(false)
Done
Both parameters should have an equal number of elements
DONE