Repository: prettier/plugin-php
Pull request #475 (2018-07-18 12:31:50)

fix: print return type declaration for closure
Critical bug :smile: 

Also move all `closure` from `function` directory to `closure` directory


Patch:
diff --git a/src/printer.js b/src/printer.js
index 85d411e..789d052 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -1987,6 +1987,23 @@ function printStatement(path, options, print) {
               ])
             )
           : "",
+        node.type
+          ? concat([
+              ": ",
+              hasDanglingComments(node.type)
+                ? concat([
+                    path.call(
+                      typePath =>
+                        comments.printDanglingComments(typePath, options, true),
+                      "type"
+                    ),
+                    " "
+                  ])
+                : "",
+              node.nullable ? "?" : "",
+              path.call(print, "type")
+            ])
+          : "",
         " {",
         indent(
           concat([hasEmptyBody ? "" : hardline, path.call(print, "body")])


Test patch:
diff --git a/tests/closure/__snapshots__/jsfmt.spec.js.snap b/tests/closure/__snapshots__/jsfmt.spec.js.snap
index 4c96514..ffcf91c 100644
--- a/tests/closure/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/closure/__snapshots__/jsfmt.spec.js.snap
@@ -66,6 +66,67 @@ $foo->bar(
 $emptyFunc = function(){};
 
 $emptyFuncWithComment = function(){ /* Comment */ };
+
+$longArgs_noVars = function (
+    $longArgument,
+    $longerArgument,
+    $muchLongerArgument
+) {
+    // body
+};
+
+$noArgs_longVars = function () use (
+    $longVar1,
+    $longerVar2,
+    $muchLongerVar3
+) {
+    // body
+};
+
+$longArgs_longVars = function (
+    $longArgument,
+    $longerArgument,
+    $muchLongerArgument
+) use (
+    $longVar1,
+    $longerVar2,
+    $muchLongerVar3
+) {
+    // body
+};
+
+$longArgs_shortVars = function (
+    $longArgument,
+    $longerArgument,
+    $muchLongerArgument
+) use ($var1) {
+    // body
+};
+
+$shortArgs_longVars = function ($arg) use (
+    $longVar1,
+    $longerVar2,
+    $muchLongerVar3
+) {
+    // body
+};
+
+$rTD = function(array $array): int {
+    return array_sum($array);
+};
+
+$rNTD = function(array $array): ?int {
+    return array_sum($array);
+};
+
+$rTDWU = function(array $array) use ($var1, $var2): int {
+    return array_sum($array);
+};
+
+$rTDWLU = function(array $array) use ($longLongLongLongLongLongLongVar1, $longerLongerLongerLongerVar2, $muchLongerVar3): int {
+    return array_sum($array);
+};
+
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 $closureWithArgs = function ($arg1, $arg2) {
@@ -135,4 +196,60 @@ $emptyFuncWithComment = function () {
     /* Comment */
 };
 
+$longArgs_noVars = function (
+    $longArgument,
+    $longerArgument,
+    $muchLongerArgument
+) {
+    // body
+};
+
+$noArgs_longVars = function () use ($longVar1, $longerVar2, $muchLongerVar3) {
+    // body
+};
+
+$longArgs_longVars = function (
+    $longArgument,
+    $longerArgument,
+    $muchLongerArgument
+) use ($longVar1, $longerVar2, $muchLongerVar3) {
+    // body
+};
+
+$longArgs_shortVars = function (
+    $longArgument,
+    $longerArgument,
+    $muchLongerArgument
+) use ($var1) {
+    // body
+};
+
+$shortArgs_longVars = function ($arg) use (
+    $longVar1,
+    $longerVar2,
+    $muchLongerVar3
+) {
+    // body
+};
+
+$rTD = function (array $array): int {
+    return array_sum($array);
+};
+
+$rNTD = function (array $array): ?int {
+    return array_sum($array);
+};
+
+$rTDWU = function (array $array) use ($var1, $var2): int {
+    return array_sum($array);
+};
+
+$rTDWLU = function (array $array) use (
+    $longLongLongLongLongLongLongVar1,
+    $longerLongerLongerLongerVar2,
+    $muchLongerVar3
+): int {
+    return array_sum($array);
+};
+
 `;
diff --git a/tests/closure/closure.php b/tests/closure/closure.php
index 1508771..0bb1c8c 100644
--- a/tests/closure/closure.php
+++ b/tests/closure/closure.php
@@ -63,3 +63,64 @@ $foo->bar(
 $emptyFunc = function(){};
 
 $emptyFuncWithComment = function(){ /* Comment */ };
+
+$longArgs_noVars = function (
+    $longArgument,
+    $longerArgument,
+    $muchLongerArgument
+) {
+    // body
+};
+
+$noArgs_longVars = function () use (
+    $longVar1,
+    $longerVar2,
+    $muchLongerVar3
+) {
+    // body
+};
+
+$longArgs_longVars = function (
+    $longArgument,
+    $longerArgument,
+    $muchLongerArgument
+) use (
+    $longVar1,
+    $longerVar2,
+    $muchLongerVar3
+) {
+    // body
+};
+
+$longArgs_shortVars = function (
+    $longArgument,
+    $longerArgument,
+    $muchLongerArgument
+) use ($var1) {
+    // body
+};
+
+$shortArgs_longVars = function ($arg) use (
+    $longVar1,
+    $longerVar2,
+    $muchLongerVar3
+) {
+    // body
+};
+
+$rTD = function(array $array): int {
+    return array_sum($array);
+};
+
+$rNTD = function(array $array): ?int {
+    return array_sum($array);
+};
+
+$rTDWU = function(array $array) use ($var1, $var2): int {
+    return array_sum($array);
+};
+
+$rTDWLU = function(array $array) use ($longLongLongLongLongLongLongVar1, $longerLongerLongerLongerVar2, $muchLongerVar3): int {
+    return array_sum($array);
+};
+
diff --git a/tests/functions/__snapshots__/jsfmt.spec.js.snap b/tests/functions/__snapshots__/jsfmt.spec.js.snap
index 8c88c6b..4f1072a 100644
--- a/tests/functions/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/functions/__snapshots__/jsfmt.spec.js.snap
@@ -120,50 +120,6 @@ function the_panel_title(
     return $title;
 }
 
-$longArgs_noVars = function (
-    $longArgument,
-    $longerArgument,
-    $muchLongerArgument
-) {
-    // body
-};
-
-$noArgs_longVars = function () use (
-    $longVar1,
-    $longerVar2,
-    $muchLongerVar3
-) {
-    // body
-};
-
-$longArgs_longVars = function (
-    $longArgument,
-    $longerArgument,
-    $muchLongerArgument
-) use (
-    $longVar1,
-    $longerVar2,
-    $muchLongerVar3
-) {
-    // body
-};
-
-$longArgs_shortVars = function (
-    $longArgument,
-    $longerArgument,
-    $muchLongerArgument
-) use ($var1) {
-    // body
-};
-
-$shortArgs_longVars = function ($arg) use (
-    $longVar1,
-    $longerVar2,
-    $muchLongerVar3
-) {
-    // body
-};
-
 function emptyFunction(){}
 function emptyFunctionWithComment(){ /* Comment */ }
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -312,42 +268,6 @@ function the_panel_title(
     return $title;
 }
 
-$longArgs_noVars = function (
-    $longArgument,
-    $longerArgument,
-    $muchLongerArgument
-) {
-    // body
-};
-
-$noArgs_longVars = function () use ($longVar1, $longerVar2, $muchLongerVar3) {
-    // body
-};
-
-$longArgs_longVars = function (
-    $longArgument,
-    $longerArgument,
-    $muchLongerArgument
-) use ($longVar1, $longerVar2, $muchLongerVar3) {
-    // body
-};
-
-$longArgs_shortVars = function (
-    $longArgument,
-    $longerArgument,
-    $muchLongerArgument
-) use ($var1) {
-    // body
-};
-
-$shortArgs_longVars = function ($arg) use (
-    $longVar1,
-    $longerVar2,
-    $muchLongerVar3
-) {
-    // body
-};
-
 function emptyFunction()
 {
 }
diff --git a/tests/functions/functions.php b/tests/functions/functions.php
index 488f82d..9ee2d6e 100644
--- a/tests/functions/functions.php
+++ b/tests/functions/functions.php
@@ -117,49 +117,5 @@ function the_panel_title(
     return $title;
 }
 
-$longArgs_noVars = function (
-    $longArgument,
-    $longerArgument,
-    $muchLongerArgument
-) {
-    // body
-};
-
-$noArgs_longVars = function () use (
-    $longVar1,
-    $longerVar2,
-    $muchLongerVar3
-) {
-    // body
-};
-
-$longArgs_longVars = function (
-    $longArgument,
-    $longerArgument,
-    $muchLongerArgument
-) use (
-    $longVar1,
-    $longerVar2,
-    $muchLongerVar3
-) {
-    // body
-};
-
-$longArgs_shortVars = function (
-    $longArgument,
-    $longerArgument,
-    $muchLongerArgument
-) use ($var1) {
-    // body
-};
-
-$shortArgs_longVars = function ($arg) use (
-    $longVar1,
-    $longerVar2,
-    $muchLongerVar3
-) {
-    // body
-};
-
 function emptyFunction(){}
 function emptyFunctionWithComment(){ /* Comment */ }