Skip to content

Commit 0248ab9

Browse files
kocsismateGirgias
andcommitted
Deprecate implicit nullable parameter types
RFC: https://wiki.php.net/rfc/deprecate-implicitly-nullable-types Co-authored-by: Gina Peter Banyard <girgias@php.net>
1 parent 7c63a43 commit 0248ab9

Some content is hidden

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

48 files changed

+170
-126
lines changed

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ PHP 8.4 UPGRADE NOTES
268268
4. Deprecated Functionality
269269
========================================
270270

271+
- Core:
272+
. Implicitly nullable parameter types are now deprecated.
273+
RFC: https://wiki.php.net/rfc/deprecate-implicitly-nullable-types
274+
271275
- Curl:
272276
. The CURLOPT_BINARYTRANSFER constant is deprecated.
273277

Zend/tests/bug63635.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Node {
66
public $parent = NULL;
77
public $children = array();
88

9-
function __construct(Node $parent=NULL) {
9+
function __construct(?Node $parent=NULL) {
1010
if ($parent) {
1111
$parent->children[] = $this;
1212
}

Zend/tests/bug71428.1.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ bug #71428.1: inheritance with null default values
33
--FILE--
44
<?php
55
class A {
6-
public function m(array $a = null) {}
6+
public function m(?array $a = null) {}
77
}
88
class B extends A {
99
public function m(array $a = []) {}

Zend/tests/bug71428.3.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ class B { public function m(A $a = NULL, $n) { echo "B.m";} };
77
class C extends B { public function m(A $a , $n) { echo "C.m";} };
88
?>
99
--EXPECTF--
10+
Deprecated: Implicitly marking parameter $a as nullable is deprecated, the explicit nullable type must be used instead in %s on line %d
11+
1012
Fatal error: Declaration of C::m(A $a, $n) must be compatible with B::m(?A $a, $n) in %sbug71428.3.php on line 4

Zend/tests/bug72119.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Bug #72119 (Interface declaration compatibility regression with default values)
33
--FILE--
44
<?php
55
interface Foo {
6-
public function bar(array $baz = null);
6+
public function bar(?array $baz = null);
77
}
88

99
class Hello implements Foo {

Zend/tests/gh11488.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ function c(
1818
--EXPECTF--
1919
Deprecated: Optional parameter $a declared before required parameter $b is implicitly treated as a required parameter in %s on line %d
2020

21+
Deprecated: Implicitly marking parameter $c as nullable is deprecated, the explicit nullable type must be used instead in %s on line %d
22+
2123
Deprecated: Optional parameter $e declared before required parameter $f is implicitly treated as a required parameter in %s on line %d

Zend/tests/ns_070.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Testing parameter type-hinted with default value inside namespace
66
namespace foo;
77

88
class bar {
9-
public function __construct(\stdclass $x = NULL) {
9+
public function __construct(?\stdclass $x = NULL) {
1010
var_dump($x);
1111
}
1212
}

Zend/tests/ns_071.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Testing parameter type-hinted (array) with default value inside namespace
66
namespace foo;
77

88
class bar {
9-
public function __construct(array $x = NULL) {
9+
public function __construct(?array $x = NULL) {
1010
var_dump($x);
1111
}
1212
}

Zend/tests/ns_072.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface foo {
1010
}
1111

1212
class bar {
13-
public function __construct(foo $x = NULL) {
13+
public function __construct(?foo $x = NULL) {
1414
var_dump($x);
1515
}
1616
}

Zend/tests/ns_073.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ $x(new \stdclass);
1414

1515
?>
1616
--EXPECTF--
17+
Deprecated: Implicitly marking parameter $x as nullable is deprecated, the explicit nullable type must be used instead in %s on line %d
1718
NULL
1819
object(stdClass)#%d (0) {
1920
}

Zend/tests/ns_074.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Testing type-hinted lambda parameter inside namespace
55

66
namespace foo;
77

8-
$x = function (\stdclass $x = NULL) {
8+
$x = function (?\stdclass $x = NULL) {
99
var_dump($x);
1010
};
1111

Zend/tests/required_param_after_optional.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ Deprecated: Optional parameter $testA declared before required parameter $testC
1313

1414
Deprecated: Optional parameter $testB declared before required parameter $testC is implicitly treated as a required parameter in %s on line %d
1515

16+
Deprecated: Implicitly marking parameter $test2A as nullable is deprecated, the explicit nullable type must be used instead in %s on line %d
17+
1618
Deprecated: Optional parameter $test2B declared before required parameter $test2C is implicitly treated as a required parameter in %s on line %d
1719

20+
Deprecated: Implicitly marking parameter $test3A as nullable is deprecated, the explicit nullable type must be used instead in %s on line %d
21+
1822
Deprecated: Optional parameter $test3B declared before required parameter $test3C is implicitly treated as a required parameter in %s on line %d

Zend/tests/traits/bug60717.phpt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,10 @@ namespace HTML
6969
echo 'Done';
7070
}
7171
?>
72-
--EXPECT--
72+
--EXPECTF--
73+
Deprecated: Implicitly marking parameter $attributes as nullable is deprecated, the explicit nullable type must be used instead in %s on line %d
74+
75+
Deprecated: Implicitly marking parameter $attributes as nullable is deprecated, the explicit nullable type must be used instead in %s on line %d
76+
77+
Deprecated: Implicitly marking parameter $attributes as nullable is deprecated, the explicit nullable type must be used instead in %s on line %d
7378
Done

Zend/tests/type_declarations/callable_003.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ function bar(callable $a = null) {
1313
foo("strpos", 123, "strpos");
1414
bar("substr");
1515
?>
16-
--EXPECT--
16+
--EXPECTF--
17+
Deprecated: Implicitly marking parameter $a as nullable is deprecated, the explicit nullable type must be used instead in %s on line %d
1718
string(6) "strpos"
1819
int(123)
1920
string(6) "strpos"

Zend/tests/type_declarations/intersection_types/implicit_nullable_intersection_type.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ function foo(X&Y $foo = null) {
1010
foo(null);
1111

1212
?>
13-
--EXPECT--
13+
--EXPECTF--
14+
Deprecated: Implicitly marking parameter $foo as nullable is deprecated, the explicit nullable type must be used instead in %s on line %d
1415
NULL

Zend/tests/type_declarations/intersection_types/implicit_nullable_intersection_type_error.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ try {
1515

1616
?>
1717
--EXPECTF--
18+
Deprecated: Implicitly marking parameter $foo as nullable is deprecated, the explicit nullable type must be used instead in %s on line %d
1819
foo(): Argument #1 ($foo) must be of type (X&Y)|null, int given, called in %s on line %d

Zend/tests/type_declarations/iterable/iterable_002.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ function baz(iterable $iterable = 1) {
1717

1818
?>
1919
--EXPECTF--
20+
Deprecated: Implicitly marking parameter $iterable as nullable is deprecated, the explicit nullable type must be used instead in %s on line %d
21+
2022
Fatal error: Cannot use int as default value for parameter $iterable of type Traversable|array in %s on line %d

Zend/tests/type_declarations/literal_types/false_standalone_implicit_nullability.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ function test(false $v = null) { return $v; }
88
var_dump(test(false));
99
var_dump(test(null));
1010
?>
11-
--EXPECT--
11+
--EXPECTF--
12+
Deprecated: Implicitly marking parameter $v as nullable is deprecated, the explicit nullable type must be used instead in %s on line %d
1213
bool(false)
1314
NULL

Zend/tests/type_declarations/literal_types/true_standalone_implicit_nullability.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ function test(true $v = null) { return $v; }
88
var_dump(test(true));
99
var_dump(test(null));
1010
?>
11-
--EXPECT--
11+
--EXPECTF--
12+
Deprecated: Implicitly marking parameter $v as nullable is deprecated, the explicit nullable type must be used instead in %s on line %d
1213
bool(true)
1314
NULL

Zend/tests/type_declarations/nullable_null.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ function test(Foo $a = null) {
77
}
88
test(null);
99
?>
10-
--EXPECT--
10+
--EXPECTF--
11+
Deprecated: Implicitly marking parameter $a as nullable is deprecated, the explicit nullable type must be used instead in %s on line %d
1112
ok

Zend/tests/type_declarations/scalar_none.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ $functions = [
88
'float' => function (float $f) { return $f; },
99
'string' => function (string $s) { return $s; },
1010
'bool' => function (bool $b) { return $b; },
11-
'int nullable' => function (int $i = NULL) { return $i; },
12-
'float nullable' => function (float $f = NULL) { return $f; },
13-
'string nullable' => function (string $s = NULL) { return $s; },
14-
'bool nullable' => function (bool $b = NULL) { return $b; }
11+
'int nullable' => function (?int $i = NULL) { return $i; },
12+
'float nullable' => function (?float $f = NULL) { return $f; },
13+
'string nullable' => function (?string $s = NULL) { return $s; },
14+
'bool nullable' => function (?bool $b = NULL) { return $b; }
1515
];
1616

1717
foreach ($functions as $type => $function) {

Zend/tests/type_declarations/scalar_null.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ $functions = [
88
'float' => function (float $f) { return $f; },
99
'string' => function (string $s) { return $s; },
1010
'bool' => function (bool $b) { return $b; },
11-
'int nullable' => function (int $i = NULL) { return $i; },
12-
'float nullable' => function (float $f = NULL) { return $f; },
13-
'string nullable' => function (string $s = NULL) { return $s; },
14-
'bool nullable' => function (bool $b = NULL) { return $b; }
11+
'int nullable' => function (?int $i) { return $i; },
12+
'float nullable' => function (?float $f) { return $f; },
13+
'string nullable' => function (?string $s) { return $s; },
14+
'bool nullable' => function (?bool $b) { return $b; }
1515
];
1616

1717
foreach ($functions as $type => $function) {

Zend/tests/type_declarations/variance/internal_parent/unresolvable_inheritance_check_param.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Test unresolvable inheritance check due to unavailable parameter type when the p
44
<?php
55

66
class Test extends DateTime {
7-
public static function createFromFormat($format, $datetime, Wrong $timezone = null): DateTime|false {}
7+
public static function createFromFormat($format, $datetime, ?Wrong $timezone = null): DateTime|false {}
88
}
99

1010
?>

Zend/tests/variadic/adding_additional_optional_parameter.phpt

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

1010
class MySQL implements DB {
11-
public function query($query, string $extraParam = null, string ...$params) { }
11+
public function query($query, ?string $extraParam = null, string ...$params) { }
1212
}
1313

1414
?>
1515
===DONE===
16-
--EXPECT--
16+
--EXPECTF--
1717
===DONE===

Zend/tests/variadic/adding_additional_optional_parameter_error.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ interface DB {
88
}
99

1010
class MySQL implements DB {
11-
public function query($query, int $extraParam = null, string ...$params) { }
11+
public function query($query, ?int $extraParam = null, string ...$params) { }
1212
}
1313

1414
?>

Zend/zend_compile.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7224,6 +7224,11 @@ static void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32
72247224

72257225
op_array->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
72267226
arg_info->type = zend_compile_typename_ex(type_ast, force_nullable, &forced_allow_nullable);
7227+
if (forced_allow_nullable) {
7228+
zend_error(E_DEPRECATED,
7229+
"Implicitly marking parameter $%s as nullable is deprecated, the explicit nullable type "
7230+
"must be used instead", ZSTR_VAL(name));
7231+
}
72277232

72287233
if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_VOID) {
72297234
zend_error_noreturn(E_COMPILE_ERROR, "void cannot be used as a parameter type");

ext/date/tests/DateTime_extends_basic2.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ echo "*** Testing new DateTime() : with user space __construct magic method ***\
1010

1111
class DateTimeExt extends DateTime
1212
{
13-
public function __construct ($date = null, DateTimeZone $dtz = null)
13+
public function __construct ($date = null, ?DateTimeZone $dtz = null)
1414
{
1515
if($dtz === null)
1616
{

ext/date/tests/bug55407.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ error_reporting=-1
66
<?php namespace melt\core;
77

88
class DateTime extends \DateTime {
9-
public static function createFromFormat($format, $time, \DateTimeZone $timezone = null): DateTime|false {
9+
public static function createFromFormat($format, $time, ?\DateTimeZone $timezone = null): DateTime|false {
1010
return new DateTime(parent::createFromFormat($format, $time, $timezone));
1111
}
1212
}

ext/opcache/tests/bug73746.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class CountryMapping
1111
const CZ = 'CZ';
1212
const EN = 'EN';
1313

14-
public function get(string $countryIsoCode = null) : string // Works correctly if return type is removed
14+
public function get(?string $countryIsoCode = null) : string // Works correctly if return type is removed
1515
{
1616
switch (strtoupper($countryIsoCode)) {
1717
case 'CZ':

ext/opcache/tests/bug74442.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ opcache
77
--FILE--
88
<?php
99
class Schema_Base {
10-
public function addField($typeclass, array $params = null) {
10+
public function addField($typeclass, ?array $params = null) {
1111
$field = new $typeclass($params);
1212
return $field;
1313
}
1414
}
1515

1616
class Field_Base {
17-
public function __construct(array $params = null) {
17+
public function __construct(?array $params = null) {
1818
if (! is_array($params)) {
1919
$params = (array) $params;
2020
}

ext/opcache/tests/invalid_array_key_type.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ opcache
55
--FILE--
66
<?php
77

8-
function test(\SplObjectStorage $definitions = null) {
8+
function test(?\SplObjectStorage $definitions = null) {
99
$argument = new stdClass;
1010
$definitions[$argument] = 1;
1111
$definitions[$argument] += 1;

ext/opcache/tests/jit/cast_001.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ opcache.jit=1205
1010
opcache
1111
--FILE--
1212
<?php
13-
function foo (int $x = null) {
13+
function foo (?int $x = null) {
1414
$a = (array)$x;
1515
$a[] = 42;
1616
var_dump($a);

ext/opcache/tests/jit/send_var_ex_002.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ for ($i = 0; $i < 3; $i++ ) {
1212
var_dump($x);
1313
}
1414

15-
function test(&$a = null, SomeType &$b = null) {
15+
function test(&$a = null, ?SomeType &$b = null) {
1616
$a++;
1717
}
1818
?>

ext/opcache/tests/opt/assign_op_002.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ opcache.enable_cli=1
66
opcache.optimization_level=-1
77
--FILE--
88
<?php
9-
function foo(int $a = null) {
9+
function foo(?int $a = null) {
1010
$a -= 1;
1111
return $a;
1212
}

ext/pdo_pgsql/tests/bug72294.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ abstract class PHPUnit_Framework_TestCase
2727
private $name = null;
2828
private $result;
2929

30-
public function run(PHPUnit_Framework_TestResult $result = null)
30+
public function run(?PHPUnit_Framework_TestResult $result = null)
3131
{
3232
$result->run($this);
3333
}

ext/reflection/tests/ReflectionClass_export_basic1.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Class A {
66
public function privf(Exception $a) {}
77
public function pubf(A $a,
88
$b,
9-
C $c = null,
9+
?C $c = null,
1010
$d = K,
1111
$e = "15 chars long -",
1212
$f = null,
@@ -20,6 +20,7 @@ define('K', "16 chars long --");
2020
echo new ReflectionClass("C"), "\n";
2121
?>
2222
--EXPECTF--
23+
Deprecated: Implicitly marking parameter $h as nullable is deprecated, the explicit nullable type must be used instead in %s on line %d
2324
Class [ <user> class C extends A ] {
2425
@@ %s 14-14
2526

ext/reflection/tests/bug62715.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Bug #62715 (ReflectionParameter::isDefaultValueAvailable() wrong result)
33
--FILE--
44
<?php
55

6-
function test(PDO $a = null, $b = 0, array $c) {}
6+
function test(?PDO $a = null, $b = 0, array $c) {}
77
$r = new ReflectionFunction('test');
88

99
foreach ($r->getParameters() as $p) {
@@ -17,6 +17,8 @@ foreach ($r->getParameters() as $p) {
1717
}
1818
?>
1919
--EXPECTF--
20+
Deprecated: Optional parameter $a declared before required parameter $c is implicitly treated as a required parameter in %s on line %d
21+
2022
Deprecated: Optional parameter $b declared before required parameter $c is implicitly treated as a required parameter in %s on line %d
2123
bool(false)
2224
bool(false)

ext/reflection/tests/parameters_002.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function test($nix, Array $ar, &$ref, stdClass $std,
1111
class test
1212
{
1313
function method($nix, Array $ar, &$ref, stdClass $std,
14-
NonExistingClass $na, stdClass $opt = NULL, $def = "FooBar")
14+
NonExistingClass $na, ?stdClass $opt = NULL, $def = "FooBar")
1515
{
1616
}
1717
}
@@ -73,6 +73,7 @@ check_params(ReflectionMethod::createFromMethodName('test::method'));
7373

7474
?>
7575
--EXPECTF--
76+
Deprecated: Implicitly marking parameter $opt as nullable is deprecated, the explicit nullable type must be used instead in %s on line %d
7677
#####test()#####
7778
===0===
7879
getName: string(3) "nix"

ext/reflection/tests/types/ReflectionType_001.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ $obj->std = new stdClass;
100100
$r = (new ReflectionProperty($obj, 'std'))->getType();
101101
var_dump($r->getName());
102102
?>
103-
--EXPECT--
103+
--EXPECTF--
104+
Deprecated: Implicitly marking parameter $d as nullable is deprecated, the explicit nullable type must be used instead in %s on line %d
104105
*** functions
105106
** Function 0 - Parameter 0
106107
bool(true)

0 commit comments

Comments
 (0)