rector icon indicating copy to clipboard operation
rector copied to clipboard

RemoveAlwaysTrueIfConditionRector with array_push is buggy

Open jg-development opened this issue 1 year ago • 1 comments
trafficstars

Bug Report

Subject Details
Rector version 1.2.5

RemoveAlwaysTrueIfConditionRector removes a correct if statement. Code is quite ugly. It seems the problem is with:

  1. array_push .... with "$outerList[$id][] = [];" insetad of "array_push($outerList[$id], []);" it works
  2. an array with at least 2 foreach iterations are needed => with "$idList = [1];" it works
    private function demo(): void
    {
        $outerList = [];
        $idList = [1, 2];

        foreach ($idList as $id) {
            $outerList[$id] = [];
            array_push($outerList[$id], []);
        }

        $resultSet = [];

        foreach ($outerList as $key => $outerElement) {
            $result = false;

            foreach ($outerElement as $innerElement) {
                $result = true;
            }

            if (!$result) {
                array_push($resultSet, $key);
            }
        }
    }

rector remove the if statement:

-            if (!$result) {
-                array_push($resultSet, $key);
-            }
+            array_push($resultSet, $key);

Expected Behaviour

Rule should not be executed/change code

jg-development avatar Oct 14 '24 14:10 jg-development