Skip to content

Commit 71ec88b

Browse files
committed
Feat: Add OrderedClassElementsFixer with specified order of elements (part of #94)
1 parent 2bf741b commit 71ec88b

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

Diff for: ecs.php

+18
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;
@@ -517,6 +518,23 @@
517518
'use_trait',
518519
],
519520
])
521+
// Elements of classes/interfaces/traits/enums should be in the defined order
522+
->withConfiguredRule(
523+
OrderedClassElementsFixer::class,
524+
[
525+
'order' => [
526+
'use_trait',
527+
'case', // enum values should be before other elements
528+
'constant',
529+
'property',
530+
'construct',
531+
'destruct',
532+
'magic',
533+
'phpunit', // phpunit special methods like setUp should be before test methods
534+
'method',
535+
],
536+
],
537+
)
520538
->withSkip([
521539
// We allow empty catch statements (but they must have comment - see EmptyCatchCommentSniff)
522540
EmptyStatementSniff::class . '.DetectedCatch' => null,

Diff for: tests/Integration/Fixtures/Basic.correct.php.inc

+5
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

Diff for: tests/Integration/Fixtures/Basic.wrong.php.inc

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class Basic
2727
return false; // BlankLineBeforeStatementFixer
2828
}
2929

30+
public const MY_PUBLIC_CONST = 333; // OrderedClassElementsFixer
31+
3032
public function fooBar(mixed $foo): mixed
3133
{
3234
// PhpdocToCommentFixer
@@ -45,4 +47,8 @@ class Basic
4547
// TernaryToElvisOperatorFixer
4648
return ($foo ? $foo : 'not true');
4749
}
50+
51+
protected int $myProperty = 666; // OrderedClassElementsFixer
52+
53+
use SomeUsefulTrait; // OrderedClassElementsFixer
4854
}

0 commit comments

Comments
 (0)