Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OrderedClassElementsFixer - handle enums #6300

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions doc/list.rst
Expand Up @@ -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'``
Expand Down
6 changes: 3 additions & 3 deletions doc/rules/class_notation/ordered_class_elements.rst
Expand Up @@ -2,7 +2,7 @@
Rule ``ordered_class_elements``
===============================

Orders the elements of classes/interfaces/traits.
Orders the elements of classes/interfaces/traits/enums.

Configuration
-------------
Expand All @@ -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``
~~~~~~~~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion doc/rules/index.rst
Expand Up @@ -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.
Expand Down
16 changes: 11 additions & 5 deletions src/Fixer/ClassNotation/OrderedClassElementsFixer.php
Expand Up @@ -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'],
Expand Down Expand Up @@ -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());
}

/**
Expand All @@ -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(
'<?php
Expand Down Expand Up @@ -237,7 +238,7 @@ public function getPriority(): int
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
for ($i = 1, $count = $tokens->count(); $i < $count; ++$i) {
if (!$tokens[$i]->isGivenKind([T_CLASS, T_TRAIT, T_INTERFACE])) { // FIXME use "isClassy"
if (!$tokens[$i]->isClassy()) {
continue;
}

Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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();
}

Expand All @@ -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';
}
Expand Down
31 changes: 31 additions & 0 deletions tests/Fixer/ClassNotation/OrderedClassElementsFixerTest.php
Expand Up @@ -1443,5 +1443,36 @@ class A
',
['order' => ['property_public_readonly', 'property_public', 'property_protected_readonly', 'property_private_readonly'], 'sort_algorithm' => 'alpha'],
];

yield [
'<?php

enum A: int
{
case Foo = 1;
case Bar = 2;
private const C1 = 1;
function qux() {
switch (true) {
case 1: break;
}
}
}
',
'<?php

enum A: int
{
private const C1 = 1;
case Foo = 1;
function qux() {
switch (true) {
case 1: break;
}
}
case Bar = 2;
}
',
];
}
}