diff --git a/doc/list.rst b/doc/list.rst index 51983bba6c2..603f66c6f24 100644 --- a/doc/list.rst +++ b/doc/list.rst @@ -1725,7 +1725,7 @@ List of Available Rules - | ``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_public_abstract_static', 'method_protected_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', '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']`` - | ``sort_algorithm`` | How multiple occurrences of same type statements should be sorted diff --git a/doc/rules/class_notation/ordered_class_elements.rst b/doc/rules/class_notation/ordered_class_elements.rst index 4062e36a738..bb58885c70b 100644 --- a/doc/rules/class_notation/ordered_class_elements.rst +++ b/doc/rules/class_notation/ordered_class_elements.rst @@ -12,7 +12,7 @@ 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_public_abstract_static', 'method_protected_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', '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']`` diff --git a/src/Fixer/ClassNotation/OrderedClassElementsFixer.php b/src/Fixer/ClassNotation/OrderedClassElementsFixer.php index 6c7c594d721..1f1480d64d5 100644 --- a/src/Fixer/ClassNotation/OrderedClassElementsFixer.php +++ b/src/Fixer/ClassNotation/OrderedClassElementsFixer.php @@ -74,8 +74,10 @@ final class OrderedClassElementsFixer extends AbstractFixer implements Configura 'method_private' => ['method', 'private'], 'method_public_abstract' => ['method_abstract', 'method_public'], 'method_protected_abstract' => ['method_abstract', 'method_protected'], + 'method_private_abstract' => ['method_abstract', 'method_private'], 'method_public_abstract_static' => ['method_abstract', 'method_static', 'method_public'], 'method_protected_abstract_static' => ['method_abstract', 'method_static', 'method_protected'], + 'method_private_abstract_static' => ['method_abstract', 'method_static', 'method_private'], 'method_public_static' => ['method_static', 'method_public'], 'method_protected_static' => ['method_static', 'method_protected'], 'method_private_static' => ['method_static', 'method_private'], @@ -105,6 +107,7 @@ public function configure(array $configuration): void $this->typePosition = []; $pos = 0; + foreach ($this->configuration['order'] as $type) { $this->typePosition[$type] = $pos++; } @@ -132,12 +135,13 @@ public function configure(array $configuration): void } $lastPosition = \count($this->configuration['order']); + foreach ($this->typePosition as &$pos) { if (null === $pos) { $pos = $lastPosition; } - // last digit is used by phpunit method ordering - $pos *= 10; + + $pos *= 10; // last digit is used by phpunit method ordering } } @@ -342,6 +346,7 @@ private function getElements(Tokens $tokens, int $startIndex): array } $type = $this->detectElementType($tokens, $i); + if (\is_array($type)) { $element['type'] = $type[0]; $element['name'] = $type[1]; @@ -411,10 +416,7 @@ private function detectElementType(Tokens $tokens, int $index) return ['phpunit', strtolower($nameToken->getContent())]; } - return str_starts_with($nameToken->getContent(), '__') - ? 'magic' - : 'method' - ; + return str_starts_with($nameToken->getContent(), '__') ? 'magic' : 'method'; } private function findElementEnd(Tokens $tokens, int $index): int @@ -458,6 +460,7 @@ private function sortElements(array $elements): array if (\array_key_exists($type, self::$specialTypes)) { if (isset($this->typePosition[$type])) { $element['position'] = $this->typePosition[$type]; + if ('phpunit' === $type) { $element['position'] += $phpunitPositions[$element['name']]; } @@ -470,12 +473,15 @@ private function sortElements(array $elements): array if (\in_array($type, ['constant', 'property', 'method'], true)) { $type .= '_'.$element['visibility']; + if ($element['abstract']) { $type .= '_abstract'; } + if ($element['static']) { $type .= '_static'; } + if ($element['readonly']) { $type .= '_readonly'; } @@ -483,6 +489,7 @@ private function sortElements(array $elements): array $element['position'] = $this->typePosition[$type]; } + unset($element); usort($elements, function (array $a, array $b): int { diff --git a/tests/Fixer/ClassNotation/OrderedClassElementsFixerTest.php b/tests/Fixer/ClassNotation/OrderedClassElementsFixerTest.php index a63f88e715f..7499ef3f12e 100644 --- a/tests/Fixer/ClassNotation/OrderedClassElementsFixerTest.php +++ b/tests/Fixer/ClassNotation/OrderedClassElementsFixerTest.php @@ -1337,6 +1337,27 @@ public function testFix80(string $expected, string $input): void public function provideFix80Cases(): \Generator { + yield [ + '