Skip to content

Commit

Permalink
Feat: Add OrderedClassElementsFixer with specified order of elements …
Browse files Browse the repository at this point in the history
…(part of #94)
  • Loading branch information
OndraM committed May 23, 2024
1 parent 6312ff0 commit 837c3f3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
18 changes: 18 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
use PhpCsFixer\Fixer\CastNotation\ShortScalarCastFixer;
use PhpCsFixer\Fixer\ClassNotation\ClassAttributesSeparationFixer;
use PhpCsFixer\Fixer\ClassNotation\NoBlankLinesAfterClassOpeningFixer;
use PhpCsFixer\Fixer\ClassNotation\OrderedClassElementsFixer;
use PhpCsFixer\Fixer\ClassNotation\SelfAccessorFixer;
use PhpCsFixer\Fixer\ClassNotation\SingleTraitInsertPerStatementFixer;
use PhpCsFixer\Fixer\ClassNotation\VisibilityRequiredFixer;
Expand Down Expand Up @@ -519,6 +520,23 @@
'use_trait',
],
])
// Elements of classes/interfaces/traits/enums should be in the defined order
->withConfiguredRule(
OrderedClassElementsFixer::class,
[
'order' => [
'use_trait',
'case', // enum values should be before other elements
'constant',
'property',
'construct',
'destruct',
'magic',
'phpunit', // phpunit special methods like setUp should be before test methods
'method',
],
],
)
->withSkip([
// We allow empty catch statements (but they must have comment - see EmptyCatchCommentSniff)
EmptyStatementSniff::class . '.DetectedCatch' => null,
Expand Down
5 changes: 5 additions & 0 deletions tests/Integration/Fixtures/Basic.correct.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ use Bar\Foo; // NoUnneededImportAliasFixer

class Basic
{
use SomeUsefulTrait; // OrderedClassElementsFixer
public const FOO = 'foo'; // ClassAttributesSeparationFixer

public const MY_PUBLIC_CONST = 333; // OrderedClassElementsFixer

protected int $myProperty = 666; // OrderedClassElementsFixer

public function isEqual($a, ?string $b): ?bool // VisibilityRequiredFixer, CompactNullableTypeDeclarationFixer
{
// TrimArraySpacesFixer
Expand Down
6 changes: 6 additions & 0 deletions tests/Integration/Fixtures/Basic.wrong.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class Basic
return false; // BlankLineBeforeStatementFixer
}

public const MY_PUBLIC_CONST = 333; // OrderedClassElementsFixer

public function fooBar(mixed $foo): mixed
{
// PhpdocToCommentFixer
Expand All @@ -47,4 +49,8 @@ class Basic
// TernaryToElvisOperatorFixer
return ($foo ? $foo : 'not true');
}

protected int $myProperty = 666; // OrderedClassElementsFixer

use SomeUsefulTrait; // OrderedClassElementsFixer
}

0 comments on commit 837c3f3

Please sign in to comment.