Skip to content

Promote warnings to errors in array_flip() #4582

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
3 changes: 2 additions & 1 deletion ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -4444,7 +4444,8 @@ PHP_FUNCTION(array_flip)
}
zend_symtable_update(Z_ARRVAL_P(return_value), Z_STR_P(entry), &data);
} else {
php_error_docref(NULL, E_WARNING, "Can only flip STRING and INTEGER values!");
zend_type_error("Can only flip STRING and INTEGER values!");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also drop the ALLCAPS and the exclamation mark ... this is a very aggressive warning ;)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikic I actually noticed that too and there are more exclamation marks which can be removed. I think a separate PR is better

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I do the same for array_count_values and group both of them in a single PR ?

return;
}
} ZEND_HASH_FOREACH_END();
}
Expand Down
47 changes: 30 additions & 17 deletions ext/standard/tests/array/array_flip.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,39 @@ $trans = array("a" => 1,
0 => "G",
1 => "h",
2 => "i");
$trans = array_flip($trans);

try {
$trans = array_flip($trans);
} catch (\TypeError $e) {
echo $e->getMessage() . "\n";
}
var_dump($trans);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this var_dump can be removed - as well as the EXPECT output. When an exception occurs the array is not modified - this is known behavior and doesn't need testing as part of this change

?>
--EXPECTF--
Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d

Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d

Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d
array(6) {
[1]=>
string(1) "b"
[2]=>
string(1) "c"
[0]=>
string(1) "z"
["G"]=>
int(0)
["h"]=>
DONE
--EXPECT--
Can only flip STRING and INTEGER values!
array(10) {
["a"]=>
int(1)
["i"]=>
["b"]=>
int(1)
["c"]=>
int(2)
["z"]=>
int(0)
["d"]=>
bool(true)
["E"]=>
bool(false)
["F"]=>
NULL
[0]=>
string(1) "G"
[1]=>
string(1) "h"
[2]=>
string(1) "i"
}

DONE
39 changes: 10 additions & 29 deletions ext/standard/tests/array/array_flip_variation4.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -52,39 +52,20 @@ $input = array(
'resource_value' => $fp,
);

var_dump( array_flip($input) );
try {
var_dump( array_flip($input) );
} catch (\TypeError $e) {
echo $e->getMessage() . "\n";
}

// closing resource
fclose($fp);

echo "Done"
?>
--EXPECTF--
*** Testing array_flip() : different invalid values in 'input' array argument ***

Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d

Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d

Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d

Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d

Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d

Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d

Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d

Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d

Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d

Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d

Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d
DONE
--EXPECT--
*** Testing array_flip() : different invalid values in 'input' array argument ***
Can only flip STRING and INTEGER values!

Warning: array_flip(): Can only flip STRING and INTEGER values! in %s on line %d
array(0) {
}
Done
DONE