Skip to content

Commit

Permalink
Fix "implicitely nullable type" warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Mar 23, 2024
1 parent 7464265 commit 378ad38
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
7 changes: 7 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ XP Compiler ChangeLog

## ?.?.? / ????-??-??

## 8.17.2 / 2024-03-23

* Fixed *implicitely nullable type* warnings for parameters with non-
constant expressions (e.g. `$param= new Handle(0)`) in PHP 8.4, see
https://wiki.php.net/rfc/deprecate-implicitly-nullable-types
(@thekid)

## 8.17.1 / 2024-01-06

* Fixed emitting captures and return types for closures - @thekid
Expand Down
19 changes: 15 additions & 4 deletions src/main/php/lang/ast/emit/PHP.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,24 +297,35 @@ protected function emitArray($result, $array) {
}

protected function emitParameter($result, $parameter) {
$result->locals[$parameter->name]= true;
$parameter->annotations && $this->emitOne($result, $parameter->annotations);
if ($parameter->type && $t= $this->literal($parameter->type)) {
$result->out->write($t.' ');

// If we have a non-constant default and a type, emit a nullable type hint
// to prevent "implicitely nullable type" warnings being raised. See here:
// https://wiki.php.net/rfc/deprecate-implicitly-nullable-types
$type= $parameter->type;
if ($parameter->default) {
$const= $this->isConstant($result, $parameter->default);
if ($type && !$const && !$type instanceof IsNullable) {
$type= new IsNullable($parameter->type);
}
}
if ($type && $t= $this->literal($type)) $result->out->write($t.' ');

if ($parameter->variadic) {
$result->out->write('... $'.$parameter->name);
} else {
$result->out->write(($parameter->reference ? '&' : '').'$'.$parameter->name);
}

if ($parameter->default) {
if ($this->isConstant($result, $parameter->default)) {
if ($const) {
$result->out->write('=');
$this->emitOne($result, $parameter->default);
} else {
$result->out->write('=null');
}
}
$result->locals[$parameter->name]= true;
}

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

0 comments on commit 378ad38

Please sign in to comment.