rectorphp / rector

Instant Upgrades and Automated Refactoring of any PHP 5.3+ code
https://getrector.com
MIT License
8.71k stars 687 forks source link

Incorrect removal of enum #8833

Closed flatcapco closed 1 month ago

flatcapco commented 1 month ago

Bug Report

Subject Details
Rector version 1.2.5

The enum import is being removed from within a Laravel Volt file when running Rector.

Minimal PHP Code Causing Issue


<?php

use App\Enums\DiscountCode\DiscountUserType;

new class extends Component {
    public DiscountCode $discountCode;

    public function mount(): void
    {
        ...
    }
}; ?>

<div>

@if ($discountCode->type === DiscountUserType::SYSTEM)
 Example
@endif

And the enum code:

<?php

namespace App\Enums\DiscountCode;

use App\Traits\Enum\EnumToArray;

enum DiscountUserType: string
{

My rector config:


    ->withSkip([
        __DIR__.'/bootstrap/cache',
        ModelCastsPropertyToCastsMethodRector::class,
    ])
    ->withPhpSets()
    ->withTypeCoverageLevel(37)
    ->withImportNames(importShortClasses: false, removeUnusedImports: true)
    ->withDeadCodeLevel(40)
    ->withSets([
        LaravelSetList::LARAVEL_110,
        LaravelSetList::LARAVEL_CODE_QUALITY,
        LaravelSetList::LARAVEL_ARRAYACCESS_TO_METHOD_CALL,
        LaravelSetList::LARAVEL_ARRAY_STR_FUNCTION_TO_STATIC_CALL,
        LaravelSetList::LARAVEL_COLLECTION,
        LaravelSetList::LARAVEL_IF_HELPERS,
    ]);

Expected Behaviour

I'd expect this import to be respected. Similar to my bug report yesterday I carefully checked and the namespace and case is the same so I believe this is a different false positive issue.

samsonasik commented 1 month ago

Could you create reproducible code at https://getrector.com/demo ? Thank you.

flatcapco commented 1 month ago

Could you create reproducible code at https://getrector.com/demo ? Thank you.

I'm not sure how to make a useful demo for this sorry

samsonasik commented 1 month ago

Just guessing, it seems you're using in template syntax,

@if ($discountCode->type === DiscountUserType::SYSTEM)
 Example
@endif

which is not a native php code, I guess you can just skip the file