rector icon indicating copy to clipboard operation
rector copied to clipboard

Incorrect behavior of RemoveUnusedVariableAssignRector, RemoveDeadReturnRector, RemoveAlwaysTrueIfConditionRector, RemoveUnreachableStatementRector

Open calebdw opened this issue 1 month ago • 2 comments

Bug Report

Subject Details
Rector version last dev-main
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.com/demo/a70187c7-f59b-4efe-ae5d-137c1003efb7

<?php

function event(...$args): mixed { return null; }

class Model {}

trait HasEvents
{
    /** @var array<string,class-string> */
    protected array $dispatchesEvents = [];
    
    protected function fireResourceEvent(string $event, Model $model, mixed ...$args): void
    {
        $class = $this->dispatchesEvents[$event] ?? null;

        if ($class === null) {
            return;
        }
        
        event(new $class($model, ...$args));
    }   
}

Responsible rules

  • RemoveUnusedVariableAssignRector

  • RemoveDeadReturnRector

  • RemoveAlwaysTrueIfConditionRector

  • RemoveUnreachableStatementRector

Expected Behavior

This shouldn't be removing the event() call which is what is dispatching the actual Laravel event.

calebdw avatar Dec 02 '25 18:12 calebdw

It seems only happen on trait, when test on class, it is skipped

https://getrector.com/demo/0ec3e640-7f26-4c5e-8a61-3441052f3c83

I guest type checking \PHPStan\Type\ErrorType or check inside trait should be added.

Could you try provide a patch, try to narrow to reduce only on specific rules only, for multi rules, add test on tests/Issues directory.

Thank you.

samsonasik avatar Dec 02 '25 18:12 samsonasik

It's also skipped when is_null() or !== null is used---seems to happen only on === null:

https://getrector.com/demo/7dced2cb-7f16-4aa4-9b1e-cb562cd058c7

calebdw avatar Dec 02 '25 18:12 calebdw

The issue seems on RemoveAlwaysTrueIfConditionRector itself, on trait

https://getrector.com/demo/0eeffa93-ebce-4f51-872c-1fb5319ac521

samsonasik avatar Dec 17 '25 09:12 samsonasik