From 05fe704e1c1865512597ccc6171e02c1120fe2ce Mon Sep 17 00:00:00 2001 From: Gregor Harlan Date: Sun, 20 Feb 2022 12:50:14 +0100 Subject: [PATCH] OrderedClassElementsFixer - handle enums --- doc/list.rst | 6 ++-- .../class_notation/ordered_class_elements.rst | 6 ++-- doc/rules/index.rst | 2 +- .../OrderedClassElementsFixer.php | 16 +++++++--- .../OrderedClassElementsFixerTest.php | 31 +++++++++++++++++++ 5 files changed, 49 insertions(+), 12 deletions(-) diff --git a/doc/list.rst b/doc/list.rst index ba2669e611a..44ce877a489 100644 --- a/doc/list.rst +++ b/doc/list.rst @@ -1745,14 +1745,14 @@ List of Available Rules `Source PhpCsFixer\\Fixer\\Operator\\OperatorLinebreakFixer <./../src/Fixer/Operator/OperatorLinebreakFixer.php>`_ - `ordered_class_elements <./rules/class_notation/ordered_class_elements.rst>`_ - Orders the elements of classes/interfaces/traits. + Orders the elements of classes/interfaces/traits/enums. Configuration options: - | ``order`` | List of strings defining order of elements. - | Allowed values: a subset of ``['use_trait', 'public', 'protected', 'private', 'constant', 'constant_public', 'constant_protected', 'constant_private', 'property', 'property_static', 'property_public', 'property_protected', 'property_private', 'property_public_readonly', 'property_protected_readonly', 'property_private_readonly', 'property_public_static', 'property_protected_static', 'property_private_static', 'method', 'method_abstract', 'method_static', 'method_public', 'method_protected', 'method_private', 'method_public_abstract', 'method_protected_abstract', 'method_private_abstract', 'method_public_abstract_static', 'method_protected_abstract_static', 'method_private_abstract_static', 'method_public_static', 'method_protected_static', 'method_private_static', 'construct', 'destruct', 'magic', 'phpunit']`` - | Default value: ``['use_trait', 'constant_public', 'constant_protected', 'constant_private', 'property_public', 'property_protected', 'property_private', 'construct', 'destruct', 'magic', 'phpunit', 'method_public', 'method_protected', 'method_private']`` + | Allowed values: a subset of ``['use_trait', 'public', 'protected', 'private', 'case', 'constant', 'constant_public', 'constant_protected', 'constant_private', 'property', 'property_static', 'property_public', 'property_protected', 'property_private', 'property_public_readonly', 'property_protected_readonly', 'property_private_readonly', 'property_public_static', 'property_protected_static', 'property_private_static', 'method', 'method_abstract', 'method_static', 'method_public', 'method_protected', 'method_private', 'method_public_abstract', 'method_protected_abstract', 'method_private_abstract', 'method_public_abstract_static', 'method_protected_abstract_static', 'method_private_abstract_static', 'method_public_static', 'method_protected_static', 'method_private_static', 'construct', 'destruct', 'magic', 'phpunit']`` + | Default value: ``['use_trait', 'case', 'constant_public', 'constant_protected', 'constant_private', 'property_public', 'property_protected', 'property_private', 'construct', 'destruct', 'magic', 'phpunit', 'method_public', 'method_protected', 'method_private']`` - | ``sort_algorithm`` | How multiple occurrences of same type statements should be sorted | Allowed values: ``'alpha'``, ``'none'`` diff --git a/doc/rules/class_notation/ordered_class_elements.rst b/doc/rules/class_notation/ordered_class_elements.rst index bb58885c70b..9dabf6a0dc1 100644 --- a/doc/rules/class_notation/ordered_class_elements.rst +++ b/doc/rules/class_notation/ordered_class_elements.rst @@ -2,7 +2,7 @@ Rule ``ordered_class_elements`` =============================== -Orders the elements of classes/interfaces/traits. +Orders the elements of classes/interfaces/traits/enums. Configuration ------------- @@ -12,9 +12,9 @@ Configuration List of strings defining order of elements. -Allowed values: a subset of ``['use_trait', 'public', 'protected', 'private', 'constant', 'constant_public', 'constant_protected', 'constant_private', 'property', 'property_static', 'property_public', 'property_protected', 'property_private', 'property_public_readonly', 'property_protected_readonly', 'property_private_readonly', 'property_public_static', 'property_protected_static', 'property_private_static', 'method', 'method_abstract', 'method_static', 'method_public', 'method_protected', 'method_private', 'method_public_abstract', 'method_protected_abstract', 'method_private_abstract', 'method_public_abstract_static', 'method_protected_abstract_static', 'method_private_abstract_static', 'method_public_static', 'method_protected_static', 'method_private_static', 'construct', 'destruct', 'magic', 'phpunit']`` +Allowed values: a subset of ``['use_trait', 'public', 'protected', 'private', 'case', 'constant', 'constant_public', 'constant_protected', 'constant_private', 'property', 'property_static', 'property_public', 'property_protected', 'property_private', 'property_public_readonly', 'property_protected_readonly', 'property_private_readonly', 'property_public_static', 'property_protected_static', 'property_private_static', 'method', 'method_abstract', 'method_static', 'method_public', 'method_protected', 'method_private', 'method_public_abstract', 'method_protected_abstract', 'method_private_abstract', 'method_public_abstract_static', 'method_protected_abstract_static', 'method_private_abstract_static', 'method_public_static', 'method_protected_static', 'method_private_static', 'construct', 'destruct', 'magic', 'phpunit']`` -Default value: ``['use_trait', 'constant_public', 'constant_protected', 'constant_private', 'property_public', 'property_protected', 'property_private', 'construct', 'destruct', 'magic', 'phpunit', 'method_public', 'method_protected', 'method_private']`` +Default value: ``['use_trait', 'case', 'constant_public', 'constant_protected', 'constant_private', 'property_public', 'property_protected', 'property_private', 'construct', 'destruct', 'magic', 'phpunit', 'method_public', 'method_protected', 'method_private']`` ``sort_algorithm`` ~~~~~~~~~~~~~~~~~~ diff --git a/doc/rules/index.rst b/doc/rules/index.rst index f0acc498b42..f885377bd71 100644 --- a/doc/rules/index.rst +++ b/doc/rules/index.rst @@ -168,7 +168,7 @@ Class Notation Removes ``final`` from methods where possible. - `ordered_class_elements <./class_notation/ordered_class_elements.rst>`_ - Orders the elements of classes/interfaces/traits. + Orders the elements of classes/interfaces/traits/enums. - `ordered_interfaces <./class_notation/ordered_interfaces.rst>`_ *(risky)* Orders the interfaces in an ``implements`` or ``interface extends`` clause. diff --git a/src/Fixer/ClassNotation/OrderedClassElementsFixer.php b/src/Fixer/ClassNotation/OrderedClassElementsFixer.php index dc32cabfd7e..e3c69f72423 100644 --- a/src/Fixer/ClassNotation/OrderedClassElementsFixer.php +++ b/src/Fixer/ClassNotation/OrderedClassElementsFixer.php @@ -51,6 +51,7 @@ final class OrderedClassElementsFixer extends AbstractFixer implements Configura 'public' => null, 'protected' => null, 'private' => null, + 'case' => ['public'], 'constant' => null, 'constant_public' => ['constant', 'public'], 'constant_protected' => ['constant', 'protected'], @@ -150,7 +151,7 @@ public function configure(array $configuration): void */ public function isCandidate(Tokens $tokens): bool { - return $tokens->isAnyTokenKindsFound([T_CLASS, T_TRAIT, T_INTERFACE]); // FIXME use Token::getClassyTokenKinds(false) + return $tokens->isAnyTokenKindsFound(Token::getClassyTokenKinds()); } /** @@ -159,7 +160,7 @@ public function isCandidate(Tokens $tokens): bool public function getDefinition(): FixerDefinitionInterface { return new FixerDefinition( - 'Orders the elements of classes/interfaces/traits.', + 'Orders the elements of classes/interfaces/traits/enums.', [ new CodeSample( 'count(); $i < $count; ++$i) { - if (!$tokens[$i]->isGivenKind([T_CLASS, T_TRAIT, T_INTERFACE])) { // FIXME use "isClassy" + if (!$tokens[$i]->isClassy()) { continue; } @@ -270,6 +271,7 @@ protected function createConfigurationDefinition(): FixerConfigurationResolverIn ->setAllowedValues([new AllowedValueSubset(array_keys(array_merge(self::$typeHierarchy, self::$specialTypes)))]) ->setDefault([ 'use_trait', + 'case', 'constant_public', 'constant_protected', 'constant_private', @@ -297,7 +299,7 @@ protected function createConfigurationDefinition(): FixerConfigurationResolverIn */ private function getElements(Tokens $tokens, int $startIndex): array { - static $elementTokenKinds = [CT::T_USE_TRAIT, T_CONST, T_VARIABLE, T_FUNCTION]; + static $elementTokenKinds = [CT::T_USE_TRAIT, T_CASE, T_CONST, T_VARIABLE, T_FUNCTION]; ++$startIndex; $elements = []; @@ -356,7 +358,7 @@ private function getElements(Tokens $tokens, int $startIndex): array if ('property' === $element['type']) { $element['name'] = $tokens[$i]->getContent(); - } elseif (\in_array($element['type'], ['use_trait', 'constant', 'method', 'magic', 'construct', 'destruct'], true)) { + } elseif (\in_array($element['type'], ['use_trait', 'case', 'constant', 'method', 'magic', 'construct', 'destruct'], true)) { $element['name'] = $tokens[$tokens->getNextMeaningfulToken($i)]->getContent(); } @@ -381,6 +383,10 @@ private function detectElementType(Tokens $tokens, int $index) return 'use_trait'; } + if ($token->isGivenKind(T_CASE)) { + return 'case'; + } + if ($token->isGivenKind(T_CONST)) { return 'constant'; } diff --git a/tests/Fixer/ClassNotation/OrderedClassElementsFixerTest.php b/tests/Fixer/ClassNotation/OrderedClassElementsFixerTest.php index 2dd943a5e59..2f1f70d853f 100644 --- a/tests/Fixer/ClassNotation/OrderedClassElementsFixerTest.php +++ b/tests/Fixer/ClassNotation/OrderedClassElementsFixerTest.php @@ -1443,5 +1443,36 @@ class A ', ['order' => ['property_public_readonly', 'property_public', 'property_protected_readonly', 'property_private_readonly'], 'sort_algorithm' => 'alpha'], ]; + + yield [ + '