Skip to content

Incorrect behavior of RemoveUnusedVariableAssignRector, RemoveDeadReturnRector, RemoveAlwaysTrueIfConditionRector, RemoveUnreachableStatementRector #9530

@calebdw

Description

@calebdw

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.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions