Repository: prettier/plugin-php
Pull request #223 (2018-03-16 10:34:29)

[WIP] feat: improve `new` output
:star: 


Patch:
diff --git a/src/printer.js b/src/printer.js
index 511d960..dcfc75c 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -416,16 +416,18 @@ function printExpression(path, options, print) {
         const parentNode = path.getParentNode();
         const shouldPrintParenthesis =
           parentNode.kind !== "print" && parentNode.kind !== "echo";
+        const isNewNode = node.inner.kind === "new";
+        const printedInner = concat([
+          isNewNode || !shouldPrintParenthesis ? "" : softline,
+          path.call(print, "inner")
+        ]);
         return group(
           concat([
             shouldPrintParenthesis ? "(" : "",
-            indent(
-              concat([
-                shouldPrintParenthesis ? softline : "",
-                path.call(print, "inner")
-              ])
-            ),
-            shouldPrintParenthesis ? concat([softline, ")"]) : ""
+            isNewNode ? printedInner : indent(printedInner),
+            shouldPrintParenthesis
+              ? concat([isNewNode ? "" : softline, ")"])
+              : ""
           ])
         );
       }
@@ -1318,22 +1320,7 @@ function printStatement(path, options, print) {
         concat([
           "new ",
           path.call(print, "what"),
-          isAnonymousClassNode
-            ? ""
-            : concat([
-                "(",
-                indent(
-                  join(
-                    ", ",
-                    path.map(
-                      argument => concat([softline, print(argument)]),
-                      "arguments"
-                    )
-                  )
-                ),
-                node.arguments.length ? softline : "",
-                ")"
-              ])
+          isAnonymousClassNode ? "" : printArgumentsList(path, options, print)
         ])
       );
     }


Test patch:
diff --git a/tests/new/__snapshots__/jsfmt.spec.js.snap b/tests/new/__snapshots__/jsfmt.spec.js.snap
index 63596de..4ef0108 100644
--- a/tests/new/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/new/__snapshots__/jsfmt.spec.js.snap
@@ -20,6 +20,19 @@ abstract class A
     }
 
 }
+
+$class = (new Foo)->veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMethod();
+$class = (new Foo(['VeryVeryVeryVeryVeryVeryVeryVeryVeryLongKey' => 'VeryVeryVeryVeryVeryVeryVeryVeryVeryLongValue']))->veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMethod();
+$class = (new PendingDispatch(new $this->class(...func_get_args())))->chain($this->chain);
+$dumper = in_array(PHP_SAPI, ['cli', 'phpdbg']) ? new CliDumper : new HtmlDumper;
+$class = new static('Error encoding model ['.get_class($model).'] with ID ['.$model->getKey().'] to JSON: '.$message);
+$response = new \\Illuminate\\Http\\JsonResponse(new JsonResponseTestJsonSerializeObject);
+$result = (new Pipeline(new \\Illuminate\\Container\\Container))
+    ->send('foo')
+    ->through([new PipelineTestPipeOne])
+    ->then(function ($piped) {
+        return $piped;
+    });
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 new Foo();
@@ -40,4 +53,31 @@ abstract class A
     }
 }
 
+$class = (new Foo())->veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMethod();
+$class = (new Foo([
+    'VeryVeryVeryVeryVeryVeryVeryVeryVeryLongKey' => 'VeryVeryVeryVeryVeryVeryVeryVeryVeryLongValue'
+]))->veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMethod();
+$class = (new PendingDispatch(
+    new $this->class(...func_get_args())
+))->chain($this->chain);
+$dumper = in_array(PHP_SAPI, ['cli', 'phpdbg'])
+    ? new CliDumper()
+    : new HtmlDumper();
+$class = new static(
+    'Error encoding model [' .
+    get_class($model) .
+    '] with ID [' .
+    $model->getKey() .
+    '] to JSON: ' .
+    $message
+);
+$response = new \\Illuminate\\Http\\JsonResponse(
+    new JsonResponseTestJsonSerializeObject()
+);
+$result = (new Pipeline(new \\Illuminate\\Container\\Container()))->send('foo')
+    ->through([new PipelineTestPipeOne()])
+    ->then(function ($piped) {
+        return $piped;
+    });
+
 `;
diff --git a/tests/new/new.php b/tests/new/new.php
index 85b79e1..35101bf 100644
--- a/tests/new/new.php
+++ b/tests/new/new.php
@@ -17,3 +17,16 @@ abstract class A
     }
 
 }
+
+$class = (new Foo)->veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMethod();
+$class = (new Foo(['VeryVeryVeryVeryVeryVeryVeryVeryVeryLongKey' => 'VeryVeryVeryVeryVeryVeryVeryVeryVeryLongValue']))->veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMethod();
+$class = (new PendingDispatch(new $this->class(...func_get_args())))->chain($this->chain);
+$dumper = in_array(PHP_SAPI, ['cli', 'phpdbg']) ? new CliDumper : new HtmlDumper;
+$class = new static('Error encoding model ['.get_class($model).'] with ID ['.$model->getKey().'] to JSON: '.$message);
+$response = new \Illuminate\Http\JsonResponse(new JsonResponseTestJsonSerializeObject);
+$result = (new Pipeline(new \Illuminate\Container\Container))
+    ->send('foo')
+    ->through([new PipelineTestPipeOne])
+    ->then(function ($piped) {
+        return $piped;
+    });