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 - PHP8.0 support abstract private methods in traits #6209

Merged
merged 1 commit into from Jan 2, 2022
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
2 changes: 1 addition & 1 deletion doc/list.rst
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion doc/rules/class_notation/ordered_class_elements.rst
Expand Up @@ -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']``

Expand Down
19 changes: 13 additions & 6 deletions src/Fixer/ClassNotation/OrderedClassElementsFixer.php
Expand Up @@ -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'],
Expand Down Expand Up @@ -105,6 +107,7 @@ public function configure(array $configuration): void

$this->typePosition = [];
$pos = 0;

foreach ($this->configuration['order'] as $type) {
$this->typePosition[$type] = $pos++;
}
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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']];
}
Expand All @@ -470,19 +473,23 @@ 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';
}
}

$element['position'] = $this->typePosition[$type];
}

unset($element);

usort($elements, function (array $a, array $b): int {
Expand Down
21 changes: 21 additions & 0 deletions tests/Fixer/ClassNotation/OrderedClassElementsFixerTest.php
Expand Up @@ -1337,6 +1337,27 @@ public function testFix80(string $expected, string $input): void

public function provideFix80Cases(): \Generator
{
yield [
'<?php

trait TestTrait
{
abstract static public function abstractStaticPublic();
abstract private function abstractPrivate();
abstract static private function abstractStaticPrivate();
}
',
'<?php

trait TestTrait
{
abstract private function abstractPrivate();
abstract static private function abstractStaticPrivate();
abstract static public function abstractStaticPublic();
}
',
];

yield [
'<?php class Foo
{
Expand Down