Skip to content

Commit b10416a

Browse files
committed
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.
1 parent f068954 commit b10416a

File tree

314 files changed

+888
-6079
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

314 files changed

+888
-6079
lines changed

UPGRADING

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ PHP 8.1 UPGRADE NOTES
2727
array is no longer supported. For example, array_pop($GLOBALS) will result
2828
in an error.
2929
RFC: https://wiki.php.net/rfc/restrict_globals_usage
30+
. Passing null to a non-nullable argument of a built-in function is
31+
deprecated. This matches the behavior of user-defined functions, where null
32+
is never accepted by non-nullable arguments.
33+
user-defined functions.
34+
35+
var_dump(str_contains("foobar", null));
36+
// Deprecated: Passing null to parameter #2 ($needle) of type string
37+
// is deprecated
38+
39+
RFC: https://wiki.php.net/rfc/deprecate_null_to_scalar_internal_arg
3040

3141
- Fileinfo:
3242
. The fileinfo functions now accept and return, respectively, finfo objects

Zend/tests/bug43201.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,37 @@ Warning: Undefined variable $ref in %s on line %d
3030

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

33+
Deprecated: chop(): Passing null to parameter #1 ($string) of type string is deprecated in %s on line %d
34+
3335
Notice: Indirect modification of overloaded property Foo::$arr has no effect in %sbug43201.php on line 17
3436

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

39+
Deprecated: chop(): Passing null to parameter #1 ($string) of type string is deprecated in %s on line %d
40+
3741
Notice: Indirect modification of overloaded property Foo::$arr has no effect in %sbug43201.php on line 17
3842

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

45+
Deprecated: chop(): Passing null to parameter #1 ($string) of type string is deprecated in %s on line %d
46+
4147
Notice: Indirect modification of overloaded property Foo::$arr has no effect in %sbug43201.php on line 17
4248

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

51+
Deprecated: chop(): Passing null to parameter #1 ($string) of type string is deprecated in %s on line %d
52+
4553
Notice: Indirect modification of overloaded property Foo::$arr has no effect in %sbug43201.php on line 17
4654

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

57+
Deprecated: chop(): Passing null to parameter #1 ($string) of type string is deprecated in %s on line %d
58+
4959
Notice: Indirect modification of overloaded property Foo::$arr has no effect in %sbug43201.php on line 17
5060

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

63+
Deprecated: chop(): Passing null to parameter #1 ($string) of type string is deprecated in %s on line %d
64+
5365
Notice: Indirect modification of overloaded property Foo::$arr has no effect in %sbug43201.php on line 17
5466
ok

Zend/tests/bug64677.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class cat {
77
}
88
}
99
$cat = new cat();
10-
$cat->show_output('Files: ', trim(`cd .`)); // this gives invalid args to shell_exec
10+
$cat->show_output('Files: ', trim((string) `cd .`)); // this gives invalid args to shell_exec
1111
$cat->show_output('Files: ', `cd .`); // this causes a segmentation fault
1212
$cat->show_output(`cd .`); // this causes a segmentation fault
1313

Zend/tests/class_exists_002.phpt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ class foo {
88
}
99

1010
var_dump(class_exists(''));
11-
var_dump(class_exists(NULL));
1211
var_dump(class_exists('FOO'));
1312
var_dump(class_exists('bar'));
1413
var_dump(class_exists(1));
1514

1615
?>
1716
--EXPECT--
1817
bool(false)
19-
bool(false)
2018
bool(true)
2119
bool(false)
2220
bool(false)

Zend/tests/exception_001.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ try {
77
try {
88
try {
99
try {
10-
throw new Exception(NULL);
10+
throw new Exception();
1111
} catch (Exception $e) {
1212
var_dump($e->getMessage());
1313
throw $e;

Zend/tests/interface_exists_001.phpt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ interface foo {
88

99
var_dump(interface_exists('foo'));
1010
var_dump(interface_exists(1));
11-
var_dump(interface_exists(NULL));
1211

1312
?>
1413
--EXPECT--
1514
bool(true)
1615
bool(false)
17-
bool(false)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Test null arg behavior for special functions
3+
--FILE--
4+
<?php
5+
6+
$null = null;
7+
var_dump(strlen($null));
8+
9+
?>
10+
--EXPECTF--
11+
Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in %s on line %d
12+
int(0)

Zend/tests/nullsafe_operator/013.phpt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,21 @@ dump_error(fn() => array_key_exists('foo', $foo?->foo()));
3838

3939
?>
4040
--EXPECTF--
41+
Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in %s on line %d
4142
int(0)
4243
bool(true)
4344
bool(false)
4445
bool(false)
4546
bool(false)
4647
bool(false)
48+
49+
Deprecated: defined(): Passing null to parameter #1 ($constant_name) of type string is deprecated in %s on line %d
4750
bool(false)
51+
52+
Deprecated: chr(): Passing null to parameter #1 ($codepoint) of type int is deprecated in %s on line %d
4853
string(1) "%s"
54+
55+
Deprecated: ord(): Passing null to parameter #1 ($character) of type string is deprecated in %s on line %d
4956
int(0)
5057
string(98) "call_user_func_array(): Argument #1 ($function) must be a valid callback, no array or string given"
5158
string(77) "call_user_func_array(): Argument #2 ($args) must be of type array, null given"
@@ -55,6 +62,8 @@ string(4) "NULL"
5562
string(52) "func_num_args() expects exactly 0 arguments, 1 given"
5663
string(52) "func_get_args() expects exactly 0 arguments, 1 given"
5764
string(69) "array_slice(): Argument #1 ($array) must be of type array, null given"
65+
66+
Deprecated: array_slice(): Passing null to parameter #2 ($offset) of type int is deprecated in %s on line %d
5867
array(1) {
5968
[0]=>
6069
string(3) "foo"

Zend/tests/str_or_obj_of_class_zpp.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ try {
5151
}
5252

5353
?>
54-
--EXPECT--
54+
--EXPECTF--
5555
string(6) "string"
5656
string(1) "1"
57+
58+
Deprecated: zend_string_or_stdclass(): Passing null to parameter #1 ($param) of type string is deprecated in %s on line %d
5759
string(0) ""
5860
object(stdClass)#1 (0) {
5961
}

Zend/tests/str_or_obj_zpp.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ try {
3434
}
3535

3636
?>
37-
--EXPECT--
37+
--EXPECTF--
3838
string(6) "string"
3939
string(1) "1"
40+
41+
Deprecated: zend_string_or_object(): Passing null to parameter #1 ($param) of type object|string is deprecated in %s on line %d
4042
string(0) ""
4143
object(stdClass)#1 (0) {
4244
}

Zend/tests/trait_exists_001.phpt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ trait foo {
88

99
var_dump(trait_exists('foo'));
1010
var_dump(trait_exists(1));
11-
var_dump(trait_exists(NULL));
1211

1312
?>
1413
--EXPECT--
1514
bool(true)
1615
bool(false)
17-
bool(false)

0 commit comments

Comments
 (0)