Repository: prettier/plugin-php
Pull request #704 (2018-10-02 11:59:23)

feat: better print extends for class
In some cases still ugly, will be fixed and refactored in next PRs.

Next:
- Better print extends and implements together
- Refactor
- More tests with comments

evilebottnawi: Merge to continue work

Patch:
diff --git a/src/printer.js b/src/printer.js
index c606253..901227a 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -1030,7 +1030,7 @@ function printLines(path, options, print, childrenAttribute = "children") {
 function printClassPart(path, options, print, part = "extends") {
   const node = path.getValue();
 
-  const printedExtends = lineBreak => {
+  const printParts = lineBreak => {
     return path.map(partPath => {
       // check if any of the implements nodes have comments
       return hasDanglingComments(partPath.getValue())
@@ -1045,15 +1045,15 @@ function printClassPart(path, options, print, part = "extends") {
   };
 
   return conditionalGroup([
-    concat([` ${part}`, group(concat([join(",", printedExtends(" "))]))]),
-    concat([` ${part}`, group(concat([join(",", printedExtends(hardline))]))]),
+    concat([` ${part}`, group(concat([join(",", printParts(" "))]))]),
+    concat([` ${part}`, group(concat([join(",", printParts(hardline))]))]),
     concat([
       line,
       part,
       group(
         indent(
           concat([
-            join(",", printedExtends(node[part].length > 1 ? hardline : " "))
+            join(",", printParts(node[part].length > 1 ? hardline : " "))
           ])
         )
       )
@@ -1098,29 +1098,40 @@ function printClass(path, options, print) {
 
   if (node.extends) {
     if (!Array.isArray(node.extends)) {
+      // Check if the extends node has a comment
+      const printClassComments = lineBreak => {
+        return hasDanglingComments(node.extends)
+          ? concat([
+              hardline,
+              path.call(
+                extendsPath =>
+                  comments.printDanglingComments(extendsPath, options, true),
+                "extends"
+              ),
+              hardline
+            ])
+          : lineBreak;
+      };
+
       partsDeclarationGroup.push(
-        group(
+        conditionalGroup([
           concat([
-            // check if the extends node has a comment
-            hasDanglingComments(node.extends)
-              ? concat([
-                  hardline,
-                  path.call(
-                    extendsPath =>
-                      comments.printDanglingComments(
-                        extendsPath,
-                        options,
-                        true
-                      ),
-                    "extends"
-                  ),
-                  hardline
-                ])
-              : " ",
+            printClassComments(" "),
+            "extends ",
+            path.call(print, "extends")
+          ]),
+          concat([
+            printClassComments(" "),
+            "extends",
+            hardline,
+            path.call(print, "extends")
+          ]),
+          concat([
+            printClassComments(line),
             "extends ",
             path.call(print, "extends")
           ])
-        )
+        ])
       );
     } else {
       partsDeclarationGroup.push(


Test patch:
diff --git a/tests/class/__snapshots__/jsfmt.spec.js.snap b/tests/class/__snapshots__/jsfmt.spec.js.snap
index efc6afc..6a91b29 100644
--- a/tests/class/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/class/__snapshots__/jsfmt.spec.js.snap
@@ -176,10 +176,8 @@ $instance = new class extends \\Foo implements \\HandleableInterface {
 };
 
 // Parenthesis on the next line
-$instance = new class extends \\Foo implements
-    \\ArrayAccess,
-    \\Countable,
-    \\Serializable
+$instance = new class extends
+    \\Foo implements \\ArrayAccess, \\Countable, \\Serializable
 {
     // Class content
 };
@@ -425,6 +423,14 @@ class VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMyClas
 class MyClass implements MyOtherClass, MyOtherClass, MyOtherOtherOtherClass, MyOtherOtherOtherOtherClass {}
 
 class VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMyClass implements MyOtherClass, MyOtherClass, MyOtherOtherOtherClass, MyOtherOtherOtherOtherClass {}
+
+class EmptyClass extends MyOtherClass {}
+
+class EmptyClass extends VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMyClass {}
+
+class VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMyClass extends EmptyClass {}
+
+class VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMyClass extends VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMyClass {}
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 
@@ -446,10 +452,10 @@ class Foo extends Bar implements Baz, Buzz
 
 $test = new Foo();
 
-abstract class ReallyReallyReallyLongClassName extends AbstractModelFactoryResourceController
-    implements
-        TooMuchObjectOrientation,
-        ThisIsMadness
+abstract class ReallyReallyReallyLongClassName extends
+    AbstractModelFactoryResourceController implements
+    TooMuchObjectOrientation,
+    ThisIsMadness
 {
     // variable doc
     public $test;
@@ -654,51 +660,59 @@ class ClassName extends ParentClass implements InterfaceClass
 {
 }
 
-class ClassName extends ParentClass implements
+class ClassName extends
+    ParentClass implements
     VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName1
 {
 }
 
-class ClassName extends ParentClass implements
+class ClassName extends
+    ParentClass implements
     VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName1
 {
 }
 
-class ClassName extends VeryVeryVeryVeryVeryVeryVeryVeryLongFileName1 implements
-    InterfaceClass
+class ClassName extends
+    VeryVeryVeryVeryVeryVeryVeryVeryLongFileName1 implements InterfaceClass
 {
 }
 
-class ClassName extends VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName1
+class ClassName extends
+    VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName1
     implements InterfaceClass
 {
 }
 
-class ClassName extends VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName1
+class ClassName extends
+    VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName1
     implements VeryVeryVeryVeryVeryVeryVeryVeryLongFileName1
 {
 }
 
-class ClassName extends VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName1
+class ClassName extends
+    VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName1
     implements VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName1
 {
 }
 
-class ClassName extends ParentClass implements
+class ClassName extends
+    ParentClass implements
     VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName1,
     VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName2,
     VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName3
 {
 }
 
-class ClassName extends VeryVeryVeryVeryVeryVeryVeryVeryLongFileName1 implements
+class ClassName extends
+    VeryVeryVeryVeryVeryVeryVeryVeryLongFileName1 implements
     VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName1,
     VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName2,
     VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName3
 {
 }
 
-class ClassName extends VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName1
+class ClassName extends
+    VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName1
     implements
         VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName1,
         VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName2,
@@ -706,12 +720,13 @@ class ClassName extends VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVery
 {
 }
 
-class VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName1 extends ParentClass
-    implements InterfaceClass
+class VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName1
+    extends ParentClass implements InterfaceClass
 {
 }
 
-class Custom_Plugin_NotificationPlaceholderSource extends Notification_Manager_DefaultPlaceholderSource
+class Custom_Plugin_NotificationPlaceholderSource extends
+    Notification_Manager_DefaultPlaceholderSource
 {
 }
 
@@ -791,4 +806,23 @@ class VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMyClas
 {
 }
 
+class EmptyClass extends MyOtherClass
+{
+}
+
+class EmptyClass extends
+    VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMyClass
+{
+}
+
+class VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMyClass
+    extends EmptyClass
+{
+}
+
+class VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMyClass
+    extends VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMyClass
+{
+}
+
 `;
diff --git a/tests/class/class.php b/tests/class/class.php
index 704b7ec..11e4e9f 100644
--- a/tests/class/class.php
+++ b/tests/class/class.php
@@ -236,3 +236,11 @@ class VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMyClas
 class MyClass implements MyOtherClass, MyOtherClass, MyOtherOtherOtherClass, MyOtherOtherOtherOtherClass {}
 
 class VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMyClass implements MyOtherClass, MyOtherClass, MyOtherOtherOtherClass, MyOtherOtherOtherOtherClass {}
+
+class EmptyClass extends MyOtherClass {}
+
+class EmptyClass extends VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMyClass {}
+
+class VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMyClass extends EmptyClass {}
+
+class VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMyClass extends VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMyClass {}