php-src/ext/standard/tests/array/range_errors.phpt
Nikita Popov b10416a652 Deprecate passing null to non-nullable arg of internal function
This deprecates passing null to non-nullable scale arguments of
internal functions, with the eventual goal of making the behavior
consistent with userland functions, where null is never accepted
for non-nullable arguments.

This change is expected to cause quite a lot of fallout. In most
cases, calling code should be adjusted to avoid passing null. In
some cases, PHP should be adjusted to make some function arguments
nullable. I have already fixed a number of functions before landing
this, but feel free to file a bug if you encounter a function that
doesn't accept null, but probably should. (The rule of thumb for
this to be applicable is that the function must have special behavior
for 0 or "", which is distinct from the natural behavior of the
parameter.)

RFC: https://wiki.php.net/rfc/deprecate_null_to_scalar_internal_arg

Closes GH-6475.
2021-02-11 21:46:13 +01:00

134 lines
3.5 KiB
PHP

--TEST--
Test range() function (errors)
--INI--
precision=14
--FILE--
<?php
echo "\n*** Testing error conditions ***\n";
echo "\n-- Testing ( (low < high) && (step = 0) ) --\n";
try {
var_dump( range(1, 2, 0) );
} catch (\ValueError $e) {
echo $e->getMessage(), "\n";
}
try {
var_dump( range("a", "b", 0) );
} catch (\ValueError $e) {
echo $e->getMessage(), "\n";
}
echo "\n\n-- Testing ( (low > high) && (step = 0) ) --\n";
try {
var_dump( range(2, 1, 0) );
} catch (\ValueError $e) {
echo $e->getMessage(), "\n";
}
try {
var_dump( range("b", "a", 0) );
} catch (\ValueError $e) {
echo $e->getMessage(), "\n";
}
echo "\n\n-- Testing ( (low < high) && (high-low < step) ) --\n";
try {
var_dump( range(1.0, 7.0, 6.5) );
} catch (\ValueError $e) {
echo $e->getMessage(), "\n";
}
echo "\n\n-- Testing ( (low > high) && (low-high < step) ) --\n";
try {
var_dump( range(7.0, 1.0, 6.5) );
} catch (\ValueError $e) {
echo $e->getMessage(), "\n";
}
echo "\n\n-- Testing ( (low < high) && (high-low < step) ) for characters --\n";
try {
var_dump(range('a', 'z', 100));
} catch (\ValueError $e) {
echo $e->getMessage(), "\n";
}
echo "\n\n-- Testing ( (low > high) && (low-high < step) ) for characters --\n";
try {
var_dump(range('z', 'a', 100));
} catch (\ValueError $e) {
echo $e->getMessage(), "\n";
}
echo "\n-- Testing other conditions --\n";
try {
var_dump( range(-1, -2, 2) );
} catch (\ValueError $e) {
echo $e->getMessage(), "\n";
}
try {
var_dump( range("a", "j", "z") );
} catch (\TypeError $e) {
echo $e->getMessage(), "\n";
}
try {
var_dump( range(0, 1, "140962482048819216326.24") );
} catch (\ValueError $e) {
echo $e->getMessage(), "\n";
}
echo "\n-- Testing Invalid steps --\n";
$step_arr = array( "string", NULL, FALSE, "", "\0" );
foreach( $step_arr as $step ) {
try {
var_dump( range( 1, 5, $step ) );
} catch (\TypeError | \ValueError $e) {
echo $e->getMessage(), "\n";
}
}
?>
--EXPECTF--
*** Testing error conditions ***
-- Testing ( (low < high) && (step = 0) ) --
range(): Argument #3 ($step) must not exceed the specified range
range(): Argument #3 ($step) must not exceed the specified range
-- Testing ( (low > high) && (step = 0) ) --
range(): Argument #3 ($step) must not exceed the specified range
range(): Argument #3 ($step) must not exceed the specified range
-- Testing ( (low < high) && (high-low < step) ) --
range(): Argument #3 ($step) must not exceed the specified range
-- Testing ( (low > high) && (low-high < step) ) --
range(): Argument #3 ($step) must not exceed the specified range
-- Testing ( (low < high) && (high-low < step) ) for characters --
range(): Argument #3 ($step) must not exceed the specified range
-- Testing ( (low > high) && (low-high < step) ) for characters --
range(): Argument #3 ($step) must not exceed the specified range
-- Testing other conditions --
range(): Argument #3 ($step) must not exceed the specified range
range(): Argument #3 ($step) must be of type int|float, string given
range(): Argument #3 ($step) must not exceed the specified range
-- Testing Invalid steps --
range(): Argument #3 ($step) must be of type int|float, string given
Deprecated: range(): Passing null to parameter #3 ($step) of type int|float is deprecated in %s on line %d
range(): Argument #3 ($step) must not exceed the specified range
range(): Argument #3 ($step) must not exceed the specified range
range(): Argument #3 ($step) must be of type int|float, string given
range(): Argument #3 ($step) must be of type int|float, string given