Skip to content

Incorrect behavior of PreparedValueToEarlyReturnRector #9112

@noother

Description

@noother

Bug Report

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

Minimal PHP Code Causing Issue

See https://getrector.com/demo/d4f318da-16f2-47a7-b884-dd2bd07bd430

<?php

final class DemoFile {
    public function preparedValueForeach(): bool {
        $test = true;
        foreach([true, false] as $item) {
        	if(!$item) $test = false;
        }
        
        if(rand(0, 1)) $test = false;
        
        return $test;
    }
}

Responsible rules

  • PreparedValueToEarlyReturnRector

Expected Behavior

Rector should return the result of $test from the foreach() in the last line

Actual Behavior

Rector returns fixed true in the last line and removes the initial $test = true; in the first line, ignoring the state change from the foreach() which breaks code logic.

I'm not sure which result is more fitting, so I'm making 2 pull requests for tests.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions