safe icon indicating copy to clipboard operation
safe copied to clipboard

[PHP 8.4] Fixes for implicit nullability deprecation

Open Ayesh opened this issue 1 year ago • 4 comments

Fixes all issues that emit deprecation notices on PHP 8.4 for implicit nullable parameter type declarations.

See:

The changes are modified on the generated files. This PR is merely to show the diffs.

Ayesh avatar Mar 18 '24 13:03 Ayesh

just came here to submit a similar PR. would be great to have this one merged :-)

staabm avatar May 25 '24 12:05 staabm

me too... 😅 I should have had a look earlier and it would have saved me an hour of work... (I did this to get rid of the deprecations in PhpMyAdmin)

ophian avatar Aug 26 '24 15:08 ophian

This is my current attempt at updating the generator to handle this case:

--- a/generator/src/Parameter.php
+++ b/generator/src/Parameter.php
@@ -75,7 +75,7 @@ class Parameter

     public function isNullable(): bool
     {
-        return $this->phpStanType->isNullable();
+        return $this->phpStanType->isNullable() || $this->getDefaultValue() === "null";
     }

     /*
diff --git a/generator/src/WritePhpFunction.php b/generator/src/WritePhpFunction.php
index d0775fb..ed4f11a 100644
--- a/generator/src/WritePhpFunction.php
+++ b/generator/src/WritePhpFunction.php
@@ -143,6 +143,9 @@ class WritePhpFunction
             }

             if ($paramAsString !== '') {
+                if ($param->isNullable() && $paramAsString[0] !== "?") {
+                    $paramAsString = "?" . $paramAsString;
+                }
                 $paramAsString .= ' ';
             }

shish avatar Sep 09 '24 19:09 shish

I needed this for my own use, and I don't see it getting merged any time soon, so I've fixed this (and merged a bunch of the other open PRs) and released as shish/safe (flagged as a replacement package so it should be drop-in compatible)

shish avatar Sep 10 '24 10:09 shish