Skip to content

Commit 378ad38

Browse files
committed
Fix "implicitely nullable type" warnings
See https://wiki.php.net/rfc/deprecate-implicitly-nullable-types
1 parent 7464265 commit 378ad38

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

ChangeLog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ XP Compiler ChangeLog
33

44
## ?.?.? / ????-??-??
55

6+
## 8.17.2 / 2024-03-23
7+
8+
* Fixed *implicitely nullable type* warnings for parameters with non-
9+
constant expressions (e.g. `$param= new Handle(0)`) in PHP 8.4, see
10+
https://wiki.php.net/rfc/deprecate-implicitly-nullable-types
11+
(@thekid)
12+
613
## 8.17.1 / 2024-01-06
714

815
* Fixed emitting captures and return types for closures - @thekid

src/main/php/lang/ast/emit/PHP.class.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,24 +297,35 @@ protected function emitArray($result, $array) {
297297
}
298298

299299
protected function emitParameter($result, $parameter) {
300+
$result->locals[$parameter->name]= true;
300301
$parameter->annotations && $this->emitOne($result, $parameter->annotations);
301-
if ($parameter->type && $t= $this->literal($parameter->type)) {
302-
$result->out->write($t.' ');
302+
303+
// If we have a non-constant default and a type, emit a nullable type hint
304+
// to prevent "implicitely nullable type" warnings being raised. See here:
305+
// https://wiki.php.net/rfc/deprecate-implicitly-nullable-types
306+
$type= $parameter->type;
307+
if ($parameter->default) {
308+
$const= $this->isConstant($result, $parameter->default);
309+
if ($type && !$const && !$type instanceof IsNullable) {
310+
$type= new IsNullable($parameter->type);
311+
}
303312
}
313+
if ($type && $t= $this->literal($type)) $result->out->write($t.' ');
314+
304315
if ($parameter->variadic) {
305316
$result->out->write('... $'.$parameter->name);
306317
} else {
307318
$result->out->write(($parameter->reference ? '&' : '').'$'.$parameter->name);
308319
}
320+
309321
if ($parameter->default) {
310-
if ($this->isConstant($result, $parameter->default)) {
322+
if ($const) {
311323
$result->out->write('=');
312324
$this->emitOne($result, $parameter->default);
313325
} else {
314326
$result->out->write('=null');
315327
}
316328
}
317-
$result->locals[$parameter->name]= true;
318329
}
319330

320331
protected function emitSignature($result, $signature, $use= null) {

0 commit comments

Comments
 (0)