Repository: prettier/plugin-php
Pull request #2269 (2023-10-19 09:32:04)

feat: make format compatible with "PER Coding Style 2.0"
This change allows to use this plugin along with "PER Coding Style 2.0"
formating :
`readonly final class ClassName {}`
to
`final readonly class ClassName {}`

doc:
https://www.php-fig.org/per/coding-style/#46-modifier-keywords


ferchoz: Hi @czosel  can you take a look a this PR?
Thanks!

PedroTroller: I want to make this change, I look at the PRs and I realise that I'm 5 days late... Thanks @ferchoz ;)
You can probably rename your PR to specify that it's not just to make it compatible with php-cs-fixer but above all with the [php-fig.org PER standard](https://www.php-fig.org/per/coding-style/#46-modifier-keywords).
ferchoz: > Hi @ferchoz, thanks for contributing and sorry about the delay! The idea looks good to me, just a minor point for completeness.

Improved!
ferchoz: @czosel can you take a look at it? thanks!!

Patch:
diff --git a/src/printer.mjs b/src/printer.mjs
index 52b940f..c822dce 100644
--- a/src/printer.mjs
+++ b/src/printer.mjs
@@ -1204,10 +1204,6 @@ function printClass(path, options, print) {
   const attrs = printAttrs(path, options, print, { inline: isAnonymousClass });
   const declaration = isAnonymousClass ? [] : [...attrs];
 
-  if (node.isReadonly) {
-    declaration.push("readonly ");
-  }
-
   if (node.isFinal) {
     declaration.push("final ");
   }
@@ -1216,6 +1212,10 @@ function printClass(path, options, print) {
     declaration.push("abstract ");
   }
 
+  if (node.isReadonly) {
+    declaration.push("readonly ");
+  }
+
   // `new` print `class` keyword with arguments
   declaration.push(isAnonymousClass ? "" : node.kind);
 


Test patch:
diff --git a/tests/class/__snapshots__/jsfmt.spec.mjs.snap b/tests/class/__snapshots__/jsfmt.spec.mjs.snap
index 3362755..31a2f9b 100644
--- a/tests/class/__snapshots__/jsfmt.spec.mjs.snap
+++ b/tests/class/__snapshots__/jsfmt.spec.mjs.snap
@@ -901,10 +901,18 @@ final class BaseClass {}
 
 abstract class BaseClass {}
 
+final readonly class BaseClass {}
+
+readonly final class BaseClass {}
+
 final class BaseClass extends MyOtherClass {}
 
 abstract class BaseClass extends MyOtherClass {}
 
+final readonly class BaseClass extends MyOtherClass {}
+
+readonly final class BaseClass extends MyOtherClass {}
+
 final class BaseClass extends MyOtherVeryVeryVeryVeVeryVeryVeryVeryVeryLongClass {}
 
 abstract class BaseClass extends MyOtherVeryVeryVeryVeVeryVeryVeryVVeryLongClass {}
@@ -1398,6 +1406,14 @@ abstract class BaseClass
 {
 }
 
+final readonly class BaseClass
+{
+}
+
+final readonly class BaseClass
+{
+}
+
 final class BaseClass extends MyOtherClass
 {
 }
@@ -1406,6 +1422,14 @@ abstract class BaseClass extends MyOtherClass
 {
 }
 
+final readonly class BaseClass extends MyOtherClass
+{
+}
+
+final readonly class BaseClass extends MyOtherClass
+{
+}
+
 final class BaseClass extends MyOtherVeryVeryVeryVeVeryVeryVeryVeryVeryLongClass
 {
 }
diff --git a/tests/class/class.php b/tests/class/class.php
index b163341..7c64b32 100644
--- a/tests/class/class.php
+++ b/tests/class/class.php
@@ -266,10 +266,18 @@ final class BaseClass {}
 
 abstract class BaseClass {}
 
+final readonly class BaseClass {}
+
+readonly final class BaseClass {}
+
 final class BaseClass extends MyOtherClass {}
 
 abstract class BaseClass extends MyOtherClass {}
 
+final readonly class BaseClass extends MyOtherClass {}
+
+readonly final class BaseClass extends MyOtherClass {}
+
 final class BaseClass extends MyOtherVeryVeryVeryVeVeryVeryVeryVeryVeryLongClass {}
 
 abstract class BaseClass extends MyOtherVeryVeryVeryVeVeryVeryVeryVVeryLongClass {}