Skip to content

Commit

Permalink
Deprecate passing null to non-nullable arg of internal function
Browse files Browse the repository at this point in the history
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 phpGH-6475.
  • Loading branch information
nikic committed Feb 11, 2021
1 parent f068954 commit b10416a
Show file tree
Hide file tree
Showing 314 changed files with 888 additions and 6,079 deletions.
10 changes: 10 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ PHP 8.1 UPGRADE NOTES
array is no longer supported. For example, array_pop($GLOBALS) will result
in an error.
RFC: https://wiki.php.net/rfc/restrict_globals_usage
. Passing null to a non-nullable argument of a built-in function is
deprecated. This matches the behavior of user-defined functions, where null
is never accepted by non-nullable arguments.
user-defined functions.

var_dump(str_contains("foobar", null));
// Deprecated: Passing null to parameter #2 ($needle) of type string
// is deprecated

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

- Fileinfo:
. The fileinfo functions now accept and return, respectively, finfo objects
Expand Down
12 changes: 12 additions & 0 deletions Zend/tests/bug43201.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,37 @@ Warning: Undefined variable $ref in %s on line %d

Warning: Undefined variable $undef in %s on line %d

Deprecated: chop(): Passing null to parameter #1 ($string) of type string is deprecated in %s on line %d

Notice: Indirect modification of overloaded property Foo::$arr has no effect in %sbug43201.php on line 17

Warning: Undefined variable $undef in %s on line %d

Deprecated: chop(): Passing null to parameter #1 ($string) of type string is deprecated in %s on line %d

Notice: Indirect modification of overloaded property Foo::$arr has no effect in %sbug43201.php on line 17

Warning: Undefined variable $undef in %s on line %d

Deprecated: chop(): Passing null to parameter #1 ($string) of type string is deprecated in %s on line %d

Notice: Indirect modification of overloaded property Foo::$arr has no effect in %sbug43201.php on line 17

Warning: Undefined variable $undef in %s on line %d

Deprecated: chop(): Passing null to parameter #1 ($string) of type string is deprecated in %s on line %d

Notice: Indirect modification of overloaded property Foo::$arr has no effect in %sbug43201.php on line 17

Warning: Undefined variable $undef in %s on line %d

Deprecated: chop(): Passing null to parameter #1 ($string) of type string is deprecated in %s on line %d

Notice: Indirect modification of overloaded property Foo::$arr has no effect in %sbug43201.php on line 17

Warning: Undefined variable $undef in %s on line %d

Deprecated: chop(): Passing null to parameter #1 ($string) of type string is deprecated in %s on line %d

Notice: Indirect modification of overloaded property Foo::$arr has no effect in %sbug43201.php on line 17
ok
2 changes: 1 addition & 1 deletion Zend/tests/bug64677.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class cat {
}
}
$cat = new cat();
$cat->show_output('Files: ', trim(`cd .`)); // this gives invalid args to shell_exec
$cat->show_output('Files: ', trim((string) `cd .`)); // this gives invalid args to shell_exec
$cat->show_output('Files: ', `cd .`); // this causes a segmentation fault
$cat->show_output(`cd .`); // this causes a segmentation fault

Expand Down
2 changes: 0 additions & 2 deletions Zend/tests/class_exists_002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ class foo {
}

var_dump(class_exists(''));
var_dump(class_exists(NULL));
var_dump(class_exists('FOO'));
var_dump(class_exists('bar'));
var_dump(class_exists(1));

?>
--EXPECT--
bool(false)
bool(false)
bool(true)
bool(false)
bool(false)
2 changes: 1 addition & 1 deletion Zend/tests/exception_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ try {
try {
try {
try {
throw new Exception(NULL);
throw new Exception();
} catch (Exception $e) {
var_dump($e->getMessage());
throw $e;
Expand Down
2 changes: 0 additions & 2 deletions Zend/tests/interface_exists_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ interface foo {

var_dump(interface_exists('foo'));
var_dump(interface_exists(1));
var_dump(interface_exists(NULL));

?>
--EXPECT--
bool(true)
bool(false)
bool(false)
12 changes: 12 additions & 0 deletions Zend/tests/null_to_non_nullable_special_func.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Test null arg behavior for special functions
--FILE--
<?php

$null = null;
var_dump(strlen($null));

?>
--EXPECTF--
Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in %s on line %d
int(0)
9 changes: 9 additions & 0 deletions Zend/tests/nullsafe_operator/013.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,21 @@ dump_error(fn() => array_key_exists('foo', $foo?->foo()));

?>
--EXPECTF--
Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in %s on line %d
int(0)
bool(true)
bool(false)
bool(false)
bool(false)
bool(false)

Deprecated: defined(): Passing null to parameter #1 ($constant_name) of type string is deprecated in %s on line %d
bool(false)

Deprecated: chr(): Passing null to parameter #1 ($codepoint) of type int is deprecated in %s on line %d
string(1) "%s"

Deprecated: ord(): Passing null to parameter #1 ($character) of type string is deprecated in %s on line %d
int(0)
string(98) "call_user_func_array(): Argument #1 ($function) must be a valid callback, no array or string given"
string(77) "call_user_func_array(): Argument #2 ($args) must be of type array, null given"
Expand All @@ -55,6 +62,8 @@ string(4) "NULL"
string(52) "func_num_args() expects exactly 0 arguments, 1 given"
string(52) "func_get_args() expects exactly 0 arguments, 1 given"
string(69) "array_slice(): Argument #1 ($array) must be of type array, null given"

Deprecated: array_slice(): Passing null to parameter #2 ($offset) of type int is deprecated in %s on line %d
array(1) {
[0]=>
string(3) "foo"
Expand Down
4 changes: 3 additions & 1 deletion Zend/tests/str_or_obj_of_class_zpp.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ try {
}

?>
--EXPECT--
--EXPECTF--
string(6) "string"
string(1) "1"

Deprecated: zend_string_or_stdclass(): Passing null to parameter #1 ($param) of type string is deprecated in %s on line %d
string(0) ""
object(stdClass)#1 (0) {
}
Expand Down
4 changes: 3 additions & 1 deletion Zend/tests/str_or_obj_zpp.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ try {
}

?>
--EXPECT--
--EXPECTF--
string(6) "string"
string(1) "1"

Deprecated: zend_string_or_object(): Passing null to parameter #1 ($param) of type object|string is deprecated in %s on line %d
string(0) ""
object(stdClass)#1 (0) {
}
Expand Down
2 changes: 0 additions & 2 deletions Zend/tests/trait_exists_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ trait foo {

var_dump(trait_exists('foo'));
var_dump(trait_exists(1));
var_dump(trait_exists(NULL));

?>
--EXPECT--
bool(true)
bool(false)
bool(false)
Loading

0 comments on commit b10416a

Please sign in to comment.