Repository: prettier/plugin-php
Pull request #126 (2018-02-26 15:05:55)

fix: nullable types
fixes https://github.com/prettier/plugin-php/issues/36#issuecomment-367980171


Patch:
diff --git a/src/printer.js b/src/printer.js
index a631789..da3de20 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -389,10 +389,10 @@ function printStatement(path, options, print) {
         const printed = path.call(childrenPath => {
           return printLines(childrenPath, options, print);
         }, "children");
-
+        const hasName = node.name && typeof node.name === "string";
         return concat([
           "namespace ",
-          node.name,
+          hasName ? node.name : "",
           node.withBrackets ? concat([" ", "{"]) : ";",
           // don't know why we need 2 line breaks here, but 1 doesn't work
           node.children.length > 0 && !node.withBrackets
@@ -534,7 +534,9 @@ function printStatement(path, options, print) {
         return printDeclarationBlock({
           declaration: concat(["function ", node.byref ? "&" : "", node.name]),
           argumentsList: path.map(print, "arguments"),
-          returnTypeContents: node.type ? path.call(print, "type") : "",
+          returnTypeContents: node.type
+            ? concat([node.nullable ? "?" : "", path.call(print, "type")])
+            : "",
           bodyContents: node.body ? path.call(print, "body") : ""
         });
       case "method":
@@ -549,11 +551,14 @@ function printStatement(path, options, print) {
             node.name
           ]),
           argumentsList: path.map(print, "arguments"),
-          returnTypeContents: node.type ? path.call(print, "type") : "",
+          returnTypeContents: node.type
+            ? concat([node.nullable ? "?" : "", path.call(print, "type")])
+            : "",
           bodyContents: node.body ? path.call(print, "body") : ""
         });
       case "parameter": {
         const name = concat([
+          node.nullable ? "?" : "",
           node.type ? path.call(print, "type") + " " : "",
           node.variadic ? "..." : "",
           node.byref ? "&" : "",


Test patch:
diff --git a/tests/class/__snapshots__/jsfmt.spec.js.snap b/tests/class/__snapshots__/jsfmt.spec.js.snap
index 424f7e7..7d2855b 100644
--- a/tests/class/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/class/__snapshots__/jsfmt.spec.js.snap
@@ -100,6 +100,11 @@ abstract class ReallyReallyReallyLongClassName extends AbstractModelFactoryResou
 
   public function method3()
     { return 'hi'; }
+
+  public function testReturn(?string $name): ?string
+  {
+        return $name;
+  }
 }
 
 $this->something->method($argument, $this->more->stuff($this->even->more->things->complicatedMethod()));
@@ -235,6 +240,11 @@ abstract class ReallyReallyReallyLongClassName
   {
     return 'hi';
   }
+
+  public function testReturn(?string $name): ?string
+  {
+    return $name;
+  }
 }
 
 $this->something->method(
diff --git a/tests/class/class.php b/tests/class/class.php
index 68805f6..813ba92 100644
--- a/tests/class/class.php
+++ b/tests/class/class.php
@@ -97,6 +97,11 @@ abstract class ReallyReallyReallyLongClassName extends AbstractModelFactoryResou
 
   public function method3()
     { return 'hi'; }
+
+  public function testReturn(?string $name): ?string
+  {
+        return $name;
+  }
 }
 
 $this->something->method($argument, $this->more->stuff($this->even->more->things->complicatedMethod()));
diff --git a/tests/functions/__snapshots__/jsfmt.spec.js.snap b/tests/functions/__snapshots__/jsfmt.spec.js.snap
index ff975fe..bb56c8e 100644
--- a/tests/functions/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/functions/__snapshots__/jsfmt.spec.js.snap
@@ -69,6 +69,11 @@ function gen(): iterable {
     yield 2;
     yield 3;
 }
+
+function testReturn(?string $name): ?string
+{
+    return $name;
+}
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 function hello()
@@ -170,4 +175,9 @@ function gen(): iterable
   yield 3;
 }
 
+function testReturn(?string $name): ?string
+{
+  return $name;
+}
+
 `;
diff --git a/tests/functions/functions.php b/tests/functions/functions.php
index 4f42b4b..834eb27 100644
--- a/tests/functions/functions.php
+++ b/tests/functions/functions.php
@@ -66,3 +66,8 @@ function gen(): iterable {
     yield 2;
     yield 3;
 }
+
+function testReturn(?string $name): ?string
+{
+    return $name;
+}
diff --git a/tests/namespace/__snapshots__/jsfmt.spec.js.snap b/tests/namespace/__snapshots__/jsfmt.spec.js.snap
index a8bc160..7c416fe 100644
--- a/tests/namespace/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/namespace/__snapshots__/jsfmt.spec.js.snap
@@ -44,3 +44,14 @@ namespace Foo;
 namespace Foo;
 
 `;
+
+exports[`without-name.php 1`] = `
+<?php
+namespace {}
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+namespace  {
+
+}
+
+`;
diff --git a/tests/namespace/without-name.php b/tests/namespace/without-name.php
new file mode 100644
index 0000000..d67041b
--- /dev/null
+++ b/tests/namespace/without-name.php
@@ -0,0 +1,2 @@
+<?php
+namespace {}