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

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

We use phpstan to detect type, on phpstan, it is detected as falsy, I think it should be reported there on phpstan side, see https://phpstan.org/r/73565769-6b97-48b7-8b52-e3b8883a5961

samsonasik avatar Oct 14 '24 15:10 samsonasik

I am closing it as it is phpstan bug, and the workaround solution already provided there https://github.com/phpstan/phpstan/issues/11846#issuecomment-2413030997 ;)

Thank you for understanding.

samsonasik avatar Oct 22 '24 12:10 samsonasik

This issue has been automatically locked because it has been closed for 150 days. Please open a new issue if you have a similar problem.

github-actions[bot] avatar Dec 28 '25 03:12 github-actions[bot]