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

PHP 8.1: Trait use statement is removed in enum #6178

Closed
wsamoht opened this issue Dec 14, 2021 · 3 comments · Fixed by #6181
Closed

PHP 8.1: Trait use statement is removed in enum #6178

wsamoht opened this issue Dec 14, 2021 · 3 comments · Fixed by #6181
Labels

Comments

@wsamoht
Copy link

wsamoht commented Dec 14, 2021

Bug report

When fixing an enum, the use statement for a trait is removed when no_unused_imports = true.

Versions

  • PHP version: 8.1.0
  • PHP CS Fixer version: 3.4.0

Command

php-cs-fixer fix TestEnum.php --config=.php-cs-fixer.php

Configuration

Click to expand
$config = new PhpCsFixer\Config();

return $config
  ->setRules([
    '@PSR2' => true,
    'array_syntax' => ['syntax' => 'short'],
    'combine_consecutive_unsets' => true,
    'class_attributes_separation' => [
      'elements' => [
        'method' => 'one',
      ],
    ],
    'multiline_whitespace_before_semicolons' => true,
    'single_quote' => true,
    'braces' => [
      'allow_single_line_closure' => true,
    ],
    'concat_space' => ['spacing' => 'one'],
    'declare_equal_normalize' => true,
    'function_typehint_space' => true,
    'single_line_comment_style' => true,
    'include' => true,
    'lowercase_cast' => true,
    'no_extra_blank_lines' => [
      'tokens' => [
        'break',
        'continue',
        'curly_brace_block',
        'extra',
        'parenthesis_brace_block',
        'return',
        'square_brace_block',
        'throw',
        'use',
        'use_trait',
        'switch',
        'case',
        'default',
      ],
    ],
    'no_multiline_whitespace_around_double_arrow' => true,
    'no_spaces_around_offset' => true,
    'no_unused_imports' => true,
    'ordered_imports' => ['sort_algorithm' => 'alpha'],
    'no_whitespace_before_comma_in_array' => true,
    'no_whitespace_in_blank_line' => true,
    'object_operator_without_whitespace' => true,
    'return_type_declaration' => true,
    'single_blank_line_before_namespace' => true,
    'ternary_operator_spaces' => true,
    'trailing_comma_in_multiline' => true,
    'trim_array_spaces' => true,
    'unary_operator_spaces' => true,
    'whitespace_after_comma_in_array' => true,
    'binary_operator_spaces' => true,
    'not_operator_with_successor_space' => true,
  ])
  ->setLineEnding("\n");

Code snippet that reproduces the problem

Expected

// TestEnum.php
namespace App\Shared;

enum TestEnum: string
{
    use EnumHasValues;

    case Test1 = 'test1';
    case Test2 = 'test2';
}

// EnumHasValues.php
namespace App\Shared;

trait EnumHasValues
{
    public static function toValues(): array
    {
        return array_map(
            static fn ($enum) => $enum->value,
            static::cases()
        );
    }
}

Actual

namespace App\Shared;

enum TestEnum: string
{
    case Test1 = 'test1';
    case Test2 = 'test2';
}
@wsamoht wsamoht changed the title PHP 8.1: Trait is removed in enum PHP 8.1: Trait use statement is removed in enum Dec 14, 2021
@SpacePossum
Copy link
Contributor

Hi and thanks for reporting,

I've made a PR that aims to fix this bug #6181

@wsamoht
Copy link
Author

wsamoht commented Dec 15, 2021

Awesome! Thank you!

@emtudo
Copy link

emtudo commented Jan 6, 2022

Please add a tag to make this solution available

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants