Skip to content

Commit 837c3f3

Browse files
committed
Feat: Add OrderedClassElementsFixer with specified order of elements (part of #94)
1 parent 6312ff0 commit 837c3f3

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

ecs.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
use PhpCsFixer\Fixer\CastNotation\ShortScalarCastFixer;
5656
use PhpCsFixer\Fixer\ClassNotation\ClassAttributesSeparationFixer;
5757
use PhpCsFixer\Fixer\ClassNotation\NoBlankLinesAfterClassOpeningFixer;
58+
use PhpCsFixer\Fixer\ClassNotation\OrderedClassElementsFixer;
5859
use PhpCsFixer\Fixer\ClassNotation\SelfAccessorFixer;
5960
use PhpCsFixer\Fixer\ClassNotation\SingleTraitInsertPerStatementFixer;
6061
use PhpCsFixer\Fixer\ClassNotation\VisibilityRequiredFixer;
@@ -519,6 +520,23 @@
519520
'use_trait',
520521
],
521522
])
523+
// Elements of classes/interfaces/traits/enums should be in the defined order
524+
->withConfiguredRule(
525+
OrderedClassElementsFixer::class,
526+
[
527+
'order' => [
528+
'use_trait',
529+
'case', // enum values should be before other elements
530+
'constant',
531+
'property',
532+
'construct',
533+
'destruct',
534+
'magic',
535+
'phpunit', // phpunit special methods like setUp should be before test methods
536+
'method',
537+
],
538+
],
539+
)
522540
->withSkip([
523541
// We allow empty catch statements (but they must have comment - see EmptyCatchCommentSniff)
524542
EmptyStatementSniff::class . '.DetectedCatch' => null,

tests/Integration/Fixtures/Basic.correct.php.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ use Bar\Foo; // NoUnneededImportAliasFixer
44

55
class Basic
66
{
7+
use SomeUsefulTrait; // OrderedClassElementsFixer
78
public const FOO = 'foo'; // ClassAttributesSeparationFixer
89

10+
public const MY_PUBLIC_CONST = 333; // OrderedClassElementsFixer
11+
12+
protected int $myProperty = 666; // OrderedClassElementsFixer
13+
914
public function isEqual($a, ?string $b): ?bool // VisibilityRequiredFixer, CompactNullableTypeDeclarationFixer
1015
{
1116
// TrimArraySpacesFixer

tests/Integration/Fixtures/Basic.wrong.php.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class Basic
2929
return false; // BlankLineBeforeStatementFixer
3030
}
3131

32+
public const MY_PUBLIC_CONST = 333; // OrderedClassElementsFixer
33+
3234
public function fooBar(mixed $foo): mixed
3335
{
3436
// PhpdocToCommentFixer
@@ -47,4 +49,8 @@ class Basic
4749
// TernaryToElvisOperatorFixer
4850
return ($foo ? $foo : 'not true');
4951
}
52+
53+
protected int $myProperty = 666; // OrderedClassElementsFixer
54+
55+
use SomeUsefulTrait; // OrderedClassElementsFixer
5056
}

0 commit comments

Comments
 (0)